停止使用静态 NSString 并改为使用 plist 字典
我在导入下面使用此代码来定义一些信息。我想改为使用 plist 来提取此信息。我怎样才能切换它?
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";
在代码中我使用它:
[pageView setImage:[UIImage imageNamed:kImage]];
UIImage *image = [UIImage imageNamed:kMImage];
CGSize size = [image size];
[colorView setFrame:CGRectMake(0, kCGRectY, size.width, kCGRectH)];
有什么想法吗?
I am using this code below my imports to define some info. 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种简单的方法是使用预处理器来处理更改访问它的所有代码,并让对象或类处理从 plist 加载信息。
对于每个变量,将变量声明替换为如下代码:
然后,在应用程序委托中,实现
getStringForKey:
方法。请注意,此代码仅处理字符串,而不处理数字。如果您希望数字也来自 plist,您可以将它们添加为字符串并获取整数值:
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:
Then, in your application delegate, implement the
getStringForKey:
method.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: