这是我的方案:我需要保存50行文本,并在应用程序启动后访问每行文本。也就是说,对于控制参数的改变,iPhone上显示的文本应该改变(即指向第二行文本,然后指向第三行......)。我希望将这些数据存储在属性列表中,然后相应地访问数据(不需要更改所需的数据模型),而不是去SQLite。
最初,我想过创建一个NSDictionary实例并存储字符串对象(文本行)。但是,困境是:如何为上述案例创建属性列表,然后在运行时访问数据。
示例代码将非常感激。我一直试图让我的头脑周围的财产清单,但我仍然无法弄明白。感谢。
答案 0 :(得分:2)
如果我理解得很清楚,那么您正试图在应用程序的文档目录中保存plist文件。这是您阅读应用程序包中嵌入的plist文件的方法,然后将其保存在文档目录中:
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
// If your plist file is in the app bundle, and is called "file.plist":
NSString *path = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"plist"];
NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];
// Do something with your array...
// If your plist has to be created or saved at runtime, you can't store it in the
// main bundle, but you can do it in the app's document directory:
NSString *documents = [self documentsDirectory];
path = [documents stringByAppendingPathComponent:@"another_file.plist"];
[array writeToFile:path atomically:YES];
[array release];
// ~/Library/Application Support/iPhone Simulator/User/Applications/[YOUR_APP_ID_HERE]/Documents/another_file.plist
NSLog(@"%@", path);
// You can read the file later again doing initWithContentsOfFile: again
// with the new file path.
[window makeKeyAndVisible];
}
- (NSString *)documentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:0];
}
file.plist文件可能是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<string>test</string>
<string>wetwert</string>
<string>dfdfh</string>
<integer>345634</integer>
</array>
</plist>