设置 plist 来存储 iPhone 游戏的应用程序数据(而非设置)
我正在编写一款 iPhone 游戏,并让它与硬编码的关卡数据配合良好,但我想将关卡数据存储在 plist 中,以便在启动时加载。我从未使用过 plist,并且无法理解应该如何根据我的数据模型设置 plist。
以下是我现在的硬编码方式:(
在我的 appDelegate 中)
- (void)loadLevels {
//setup NSNumber objects to load into sequences
NSNumber * rID = [[[NSNumber alloc] initWithInt:0] autorelease];
NSNumber * bID = [[[NSNumber alloc] initWithInt:1] autorelease];
NSNumber * gID = [[[NSNumber alloc] initWithInt:2] autorelease];
NSNumber * yID = [[[NSNumber alloc] initWithInt:3] autorelease];
NSNumber * rbID = [[[NSNumber alloc] initWithInt:4] autorelease];
NSNumber * rgID = [[[NSNumber alloc] initWithInt:5] autorelease];
NSNumber * ryID = [[[NSNumber alloc] initWithInt:6] autorelease];
NSNumber * bgID = [[[NSNumber alloc] initWithInt:7] autorelease];
NSNumber * byID = [[[NSNumber alloc] initWithInt:8] autorelease];
NSNumber * gyID = [[[NSNumber alloc] initWithInt:9] autorelease];
//Level One's Sequence
NSMutableArray * aSequence = [[[NSMutableArray alloc] initWithCapacity:1] autorelease];
[aSequence addObject: rID];
[aSequence addObject: bID];
[aSequence addObject: gID];
[aSequence addObject: yID];
[aSequence addObject: rbID];
[aSequence addObject: rgID];
[aSequence addObject: ryID];
[aSequence addObject: bgID];
[aSequence addObject: byID];
[aSequence addObject: gyID];
// Load level One
_levels = [[[NSMutableArray alloc] init] autorelease];
Level *level1 = [[[Level alloc] initWithLevelNum:1 levelSpeed:1.0 levelSequence:aSequence] autorelease];
[_levels addObject:level1];
//do the same thing for subsequent levels//
}
(这就是我实现 Level 类的方式)
#import "Level.h"
@implementation Level
@synthesize levelNum = _levelNum;
@synthesize levelSpeed = _levelSpeed;
@synthesize levelSequence = _levelSequence;
- (id)initWithLevelNum:(int)levelNum levelSpeed:(float)levelSpeed levelSequence:(NSMutableArray *)levelSequence {
if ((self = [super init])) {
self.levelNum = levelNum;
self.levelSpeed = levelSpeed;
self.levelSequence = [[[NSMutableArray alloc] initWithArray:levelSequence] autorelease];
}
return self;
}
- (void)dealloc {
[_levelSequence release];
_levelSequence = nil;
[super dealloc];
}
@end
我只是不知道如何设置 plist 来存储我的数据以匹配我的模型。有人可以给我一些建议吗?
添加: (这是我认为我需要设置 plist 的方式 - 数据模型只是初始化我的级别的三个变量(上面)。如果您查看我当前的 plist,可能会更清楚它是如何设置的,但每个级别都包含包括:关卡编号、关卡速度以及表示该关卡所需序列的数字数组。)
现在,如果我的设置正确,如何将这些值加载到我的程序中?
I am writing an iPhone game and have it working well with my level data hard coded but I would like to store the level data in a plist a load it at launch. I have never used plists and am having trouble understanding how I should set the plist up based on my data model.
Here is how I have it hard coded now:
(in my appDelegate)
- (void)loadLevels {
//setup NSNumber objects to load into sequences
NSNumber * rID = [[[NSNumber alloc] initWithInt:0] autorelease];
NSNumber * bID = [[[NSNumber alloc] initWithInt:1] autorelease];
NSNumber * gID = [[[NSNumber alloc] initWithInt:2] autorelease];
NSNumber * yID = [[[NSNumber alloc] initWithInt:3] autorelease];
NSNumber * rbID = [[[NSNumber alloc] initWithInt:4] autorelease];
NSNumber * rgID = [[[NSNumber alloc] initWithInt:5] autorelease];
NSNumber * ryID = [[[NSNumber alloc] initWithInt:6] autorelease];
NSNumber * bgID = [[[NSNumber alloc] initWithInt:7] autorelease];
NSNumber * byID = [[[NSNumber alloc] initWithInt:8] autorelease];
NSNumber * gyID = [[[NSNumber alloc] initWithInt:9] autorelease];
//Level One's Sequence
NSMutableArray * aSequence = [[[NSMutableArray alloc] initWithCapacity:1] autorelease];
[aSequence addObject: rID];
[aSequence addObject: bID];
[aSequence addObject: gID];
[aSequence addObject: yID];
[aSequence addObject: rbID];
[aSequence addObject: rgID];
[aSequence addObject: ryID];
[aSequence addObject: bgID];
[aSequence addObject: byID];
[aSequence addObject: gyID];
// Load level One
_levels = [[[NSMutableArray alloc] init] autorelease];
Level *level1 = [[[Level alloc] initWithLevelNum:1 levelSpeed:1.0 levelSequence:aSequence] autorelease];
[_levels addObject:level1];
//do the same thing for subsequent levels//
}
(this is how I have my Level Class implemented)
#import "Level.h"
@implementation Level
@synthesize levelNum = _levelNum;
@synthesize levelSpeed = _levelSpeed;
@synthesize levelSequence = _levelSequence;
- (id)initWithLevelNum:(int)levelNum levelSpeed:(float)levelSpeed levelSequence:(NSMutableArray *)levelSequence {
if ((self = [super init])) {
self.levelNum = levelNum;
self.levelSpeed = levelSpeed;
self.levelSequence = [[[NSMutableArray alloc] initWithArray:levelSequence] autorelease];
}
return self;
}
- (void)dealloc {
[_levelSequence release];
_levelSequence = nil;
[super dealloc];
}
@end
I'm just not getting how to set up a plist to store my data to match my model. Can anyone give me some advise please?
ADDITION:
(here is how I think I need to setup the plist - the data model is simply the three variables initializing my level (above). If you look at my current plist it might be more clear how it is setup, but each level is comprised of: a level number, a level speed, and an array of numbers denoting the required sequence for that level.)
Now, if I do have this setup correctly how do I load the values into my program?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将您的 plist 文件添加为项目中的资源文件。比如说,它的名称是 config.plist
获取资源文件的路径。 (不过,请注意 [NSBundle mainBundle],因为它不会在单元测试中返回单元测试包。)
希望有帮助。请注意,我尚未编译代码,因此可能存在一些拼写错误。
Add your plist file as a resource file in your project. Say, it's name is config.plist
Get the path for the resource file. (Though, be careful of [NSBundle mainBundle] as it will not return the unit test bundle in a unit test.)
Hope it helps. Please note that I have not compiled the code, so there might be some typos.
您应该做的是使用您想要的所有适用数据创建一个
NSDictionary
,并将其读取并写入到NSUserDefaults
中。What you should do is create an
NSDictionary
with all of the applicable data that you want, and read it from and write it toNSUserDefaults
.