停止使用静态 NSString 并改为使用 plist 字典

发布于 2024-11-01 15:49:45 字数 672 浏览 2 评论 0原文

我在导入下面使用此代码来定义一些信息。我想改为使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七七 2024-11-08 15:49:45

一种简单的方法是使用预处理器来处理更改访问它的所有代码,并让对象或类处理从 plist 加载信息。

对于每个变量,将变量声明替换为如下代码:

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

然后,在应用程序委托中,实现 getStringForKey: 方法。

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

请注意,此代码仅处理字符串,而不处理数字。如果您希望数字也来自 plist,您可以将它们添加为字符串并获取整数值:

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

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])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文