我知道关于这个问题有很多问题,但是我无法看到我犯错误的地方,并希望一些额外的眼睛会有所帮助。我已经验证了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中的数据。感谢我做错了什么。
答案 0 :(得分:1)
阅读你的plist尝试这样的事情:
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"PlistFileName" ofType:@"plist"];
NSDictionary *plistData = [[NSDictionary alloc] initWithContentsOfFile:plistPath];