在运行时读取 plist 文件
Read plist file during runtime
我在我的程序中包含了一个 plist 文件并在本地调用它
NSString *plistpath = [[NSBundle mainBundle] pathForResource:@"pli" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:plistpath];
NSArray *cont = [dict allValues];
我希望我的程序在运行时手动(不是通过程序)更新文件、从 plist 文件中添加或删除数据时读取文件,而不必停止执行并启动 运行再次。有什么方法可以实现吗?
请帮忙
您需要了解基于 OS 的 dispatch_source_*
调用 (gcd),以便您可以监控 folder/file 变化。
从这里开始:
https://developer.apple.com/documentation/dispatch/1385630-dispatch_source_create
如果您想查看工作示例,可以查看 https://github.com/tblank555/iMonitorMyFiles
的来源
PS:手动更改 NSBundle 对象是一种不好的做法。看来你真正需要的是一个设置视图,只是说。
这是将数据保存在 .plist 文件中的方法
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"myData.plist"];
NSError *writeError = nil;
NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:yourDictionary format:NSPropertyListXMLFormat_v1_0 options:NSPropertyListImmutable error:&writeError];
if(plistData)
{
[plistData writeToFile:plistPath atomically:YES];
NSLog(@"Data saved sucessfully");
}
else
{
NSLog(@"Error in saveData: %@", writeError.localizedDescription);
}
从包中读取 plist 并从中获取 Root Dictionary
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"myData.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
plistPath = [[NSBundle mainBundle] pathForResource:@"myData" ofType:@"plist"];
}
NSDictionary *resultDic = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
我在我的程序中包含了一个 plist 文件并在本地调用它
NSString *plistpath = [[NSBundle mainBundle] pathForResource:@"pli" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:plistpath];
NSArray *cont = [dict allValues];
我希望我的程序在运行时手动(不是通过程序)更新文件、从 plist 文件中添加或删除数据时读取文件,而不必停止执行并启动 运行再次。有什么方法可以实现吗?
请帮忙
您需要了解基于 OS 的 dispatch_source_*
调用 (gcd),以便您可以监控 folder/file 变化。
从这里开始: https://developer.apple.com/documentation/dispatch/1385630-dispatch_source_create
如果您想查看工作示例,可以查看 https://github.com/tblank555/iMonitorMyFiles
的来源PS:手动更改 NSBundle 对象是一种不好的做法。看来你真正需要的是一个设置视图,只是说。
这是将数据保存在 .plist 文件中的方法
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"myData.plist"];
NSError *writeError = nil;
NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:yourDictionary format:NSPropertyListXMLFormat_v1_0 options:NSPropertyListImmutable error:&writeError];
if(plistData)
{
[plistData writeToFile:plistPath atomically:YES];
NSLog(@"Data saved sucessfully");
}
else
{
NSLog(@"Error in saveData: %@", writeError.localizedDescription);
}
从包中读取 plist 并从中获取 Root Dictionary
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"myData.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
plistPath = [[NSBundle mainBundle] pathForResource:@"myData" ofType:@"plist"];
}
NSDictionary *resultDic = [[NSDictionary alloc] initWithContentsOfFile:plistPath];