0

我知道在 SO 上有很多关于此的问题,但我看不出我在哪里犯了错误,并希望一些额外的眼睛会有所帮助。我已经验证 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 中的数据。感谢任何关于我做错了什么的帮助。

4

1 回答 1

1

要阅读您的 plist,请尝试以下操作:

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"PlistFileName" ofType:@"plist"];
NSDictionary *plistData = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
于 2012-11-14T19:18:37.110 回答