我知道有很多关于此的问题,但我看不出我在哪里犯了错误,希望更多的关注能有所帮助。我已经验证 plist 位于我的包中,它也在我的 docs 目录中并且包含数据。下面是应用程序包的屏幕截图,plist 位于顶部:
我从另一个类传递 plist,并验证它是正确的 plist。
这是我的代码:
-(id)init {
if (self = [super init]) {
//set up the appTracker
appTracker = [[OAI_AppTracker alloc] init];
//set up a file manager and error container
fileManger=[NSFileManager defaultManager];
//docs directory path
documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//track event
appTracker.appEvent = @"File Manager initialized";
[appTracker recordEvent];
}
return self;
}
- (NSDictionary* ) readPlist {
NSError* error;
//set up dictionary to hold our app data
NSDictionary* appData;
//set up destination path
NSString* destinationPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", plistToRead]];
if ([fileManger fileExistsAtPath:destinationPath]){
//read plist
appData = [[NSDictionary alloc] initWithContentsOfFile:destinationPath];
} else {
//file doesn't exist so we have to move it to the doc folder
NSString *sourcePath=[[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:plistToWrite];
[fileManger copyItemAtPath:sourcePath toPath:destinationPath error:&error];
//now read the plist
appData = [[NSDictionary alloc] initWithContentsOfFile:destinationPath];
}
NSLog(@"%@", appData);
return appData;
}
我的日志显示 NULL,而不是 plist 中的数据。感谢对我做错了什么的任何帮助。
最佳答案
要阅读您的 plist,请尝试以下操作:
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"PlistFileName" ofType:@"plist"];
NSDictionary *plistData = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
关于iOS plist 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13385712/