xcode 设置 plist 来存储 iPhone 游戏的应用程序数据(不是设置)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4911688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:11:12  来源:igfitidea点击:

Setting up a plist to store application data (not settings) for an iPhone game

iosobjective-ciphonexcodeplist

提问by Mark7777G

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.

我正在编写一个 iPhone 游戏,它可以很好地与我的关卡数据进行硬编码,但我想将关卡数据存储在 plist 中并在启动时加载它。我从未使用过 plist,并且无法理解如何根据我的数据模型设置 plist。

Here is how I have it hard coded now:

这是我现在对其进行硬编码的方式:

(in my appDelegate)

(在我的 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)

(这就是我实现 Level Class 的方式)

#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?

我只是不知道如何设置 plist 来存储我的数据以匹配我的模型。任何人都可以给我一些建议吗?

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.)

附加:(这是我认为我需要设置 plist 的方式 - 数据模型只是初始化我的级别的三个变量(上面)。如果您查看我当前的 plist,可能会更清楚它是如何设置的,但是每个级别包括:级别编号、级别速度和表示该级别所需序列的数字数组。)

My current plist setup

我当前的 plist 设置

Now, if I do have this setup correctly how do I load the values into my program?

现在,如果我的设置正确,我该如何将这些值加载到我的程序中?

回答by taskinoor

  • 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.)

  • 将您的 plist 文件作为资源文件添加到您的项目中。说,它的名字是 config.plist

  • 获取资源文件的路径。(不过,请注意 [NSBundle mainBundle],因为它不会在单元测试中返回单元测试包。)

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"config" ofType:@"plist"];
  • Your root object is an array of levels. Load it in a NSArray.
  • 您的根对象是一个级别数组。将其加载到 NSArray 中。
// this is autoreleased. you can retain it if needed
NSArray *levelArray = [NSArray arrayWithContentsOfFile:plistPath];
  • Now you have the array of levels. Each level is a dictionary. Load the level i (counting from 0).
  • 现在您拥有了一系列级别。每个级别都是一本字典。加载级别 i(从 0 开始计数)。
NSDictionary *level = [levelArray objectAtIndex:i];
  • Now you can get the objects from level dictionary by using objectForKey method. For example, to get the sequence array:
  • 现在您可以使用 objectForKey 方法从级别字典中获取对象。例如,要获取序列数组:
NSArray *seq = [level objectForKey:@"levelSequence"];
  • You can loop through the levelArray to alloc, init and add to levels array for all your levels.
  • 您可以遍历 levelArray 以分配、初始化并添加到所有级别的级别数组。

Hope it helps. Please note that I have not compiled the code, so there might be some typos.

希望能帮助到你。请注意,我还没有编译代码,所以可能会有一些错别字。

回答by Alexsander Akers

What you should do is create an NSDictionarywith all of the applicable data that you want, and read it from and write it to NSUserDefaults.

您应该做的是NSDictionary使用您想要的所有适用数据创建一个,然后从NSUserDefaults.