Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

made er_*Resource use NSBundles URLForResource, added er_stringResource

  • Loading branch information...
commit fd63f69e7f7499963f677a464af5c9312c9924bd 1 parent dabcbee
@bkrpub authored
View
6 EngineRoom/ERUtility/NSBundle_ERExtensions.h
@@ -5,8 +5,14 @@ extern NSString *kERBundleBuildConfiguration;
@interface NSBundle (ResourceExtensions)
+- (NSURL *) er_URLForResource: (NSString *) name withExtension: (NSString *) extension error: (NSError **) outError;
+
- (NSData *) er_dataResource: (NSString *) name ofType: (NSString *) type options: (NSUInteger) dataOptions error: (NSError **) outError;
+- (NSString *) er_stringResource: (NSString *) name ofType: (NSString *) type encoding: (NSStringEncoding) encoding error: (NSError **) outError;
+
+- (NSString *) er_stringResource: (NSString *) name ofType: (NSString *) type error: (NSError **) outError;
+
- (id) er_propertyListResource: (NSString *) name ofType: (NSString *) type options: (NSUInteger) plistOptions error: (NSError **) outError;
@end
View
43 EngineRoom/ERUtility/NSBundle_ERExtensions.m
@@ -9,29 +9,48 @@
@implementation NSBundle (ERResourceExtensions)
+- (NSURL *) er_URLForResource: (NSString *) name withExtension: (NSString *) extension error: (NSError **) outError
+{
+ ER_PRECONDITION_NSERROR_RETURN_NIL(name != nil, outError);
+
+ NSURL *URL = [self URLForResource: name withExtension: extension ?: @""];
+
+ ER_CHECK_NSERROR_REASON_RETURN_NIL(nil != URL, outError, NSPOSIXErrorDomain, ENOENT, @"NO_SUCH_RESOURCE: %@%@%@", name, extension ? @"." : @"", extension ?: @"");
+
+ return URL;
+}
+
- (NSData *) er_dataResource: (NSString *) name ofType: (NSString *) type options: (NSUInteger) dataOptions error: (NSError **) outError
{
- ER_PRECONDITION_NSERROR_RETURN_NIL(name != nil, outError);
-
- NSURL *resourceURL = [self resourceURL];
-
- NSURL *URL = [resourceURL URLByAppendingPathComponent: name];
-
- if( nil != type ) {
- URL = [URL URLByAppendingPathExtension: type];
- }
+ NSURL *URL = [self er_URLForResource: name withExtension: type error: outError];
+
+ if( nil == URL ) {
+ return nil;
+ }
return [NSData dataWithContentsOfURL: URL options: dataOptions error: outError];
}
- (id) er_propertyListResource: (NSString *) name ofType: (NSString *) type options: (NSUInteger) plistOptions error: (NSError **) outError
-{
- ER_PRECONDITION_NSERROR_RETURN_NIL(name != nil, outError);
-
+{
NSData *data = [self er_dataResource: name ofType: type options: 0 error: outError];
return data ? [NSPropertyListSerialization er_propertyListWithData: data options: plistOptions error: outError] : nil;
}
+- (NSString *) er_stringResource: (NSString *) name ofType: (NSString *) type encoding: (NSStringEncoding) encoding error: (NSError **) outError
+{
+ NSURL *URL = [self er_URLForResource: name withExtension: type error: outError];
+ NSString *string = URL ? [NSString stringWithContentsOfURL: URL encoding: encoding error: outError] : nil;
+ return string;
+}
+
+- (NSString *) er_stringResource: (NSString *) name ofType: (NSString *) type error: (NSError **) outError
+{
+ NSURL *URL = [self er_URLForResource: name withExtension: type error: outError];
+ NSString *string = URL ? [NSString stringWithContentsOfURL: URL usedEncoding: nil error: outError] : nil;
+ return string;
+}
+
@end
@implementation NSBundle (ERInfoDictionaryExtensions)
Please sign in to comment.
Something went wrong with that request. Please try again.