开发者

Stop using static NSStrings and move to plist dictionary instead

I am using this code below my imports to define some in开发者_JAVA技巧fo. I would like to instead use a plist to pull this info. How can I switch it over?

static NSUInteger kCGRectY = 520;
static NSUInteger kCGRectH = 340;
static NSString *kImage = @"Cover.png";
static NSString *kMImage = @"IMG_0172.JPG";
static NSString *kNames = @"WHO ELSE WORKED ON IT";
static NSString *kTitle = @"PROJECT DANDELION";
static NSString *kText  = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit";

In the code I use it like:

[pageView setImage:[UIImage imageNamed:kImage]];
UIImage *image = [UIImage imageNamed:kMImage];
CGSize size = [image size];
[colorView setFrame:CGRectMake(0, kCGRectY, size.width, kCGRectH)];

Any ideas?


An easy way would be to use the preprocessor to handle changing all of the code which accesses it, and have an object or class handle loading the information from the plist.

Replace the variable declarations with this code like this for each variable:

#define kImage ([(AppDelegateClass*)[[UIApplication sharedApplication] delegate] getStringForKey:@"kImage"])

Then, in your application delegate, implement the getStringForKey: method.

- (NSString *)getStringForKey:(NSString *)key {
    static NSDictionary *plist = nil;
    if(!plist) plist = [[NSDictionary alloc] initWithContentsOfFile:@"path/to/plist.plist"];
    return [plist objectForKey:key];
}

Note that this code only handles strings, not numbers. If you want your numbers to also come from the plist, you could add them as strings and get the integer value:

#define kCGRectY ([[(AppDelegateClass*)[[UIApplication sharedApplication] delegate] getStringForKey:@"kCGRectY"] intValue])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜