iOS plist 为空

标签 ios plist

我知道有很多关于此的问题,但我看不出我在哪里犯了错误,希望更多的关注能有所帮助。我已经验证 plist 位于我的包中,它也在我的 docs 目录中并且包含数据。下面是应用程序包的屏幕截图,plist 位于顶部:

enter image description here

我从另一个类传递 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/

相关文章:

ios - NSLocationWhenInUseUsageDescription 错误一直显示

iphone - 数据存储在plist数据库好还是Sqlite数据库好?

macos - 将项目添加到 Finder 侧边栏

ios - 如何在 iOS 中使用 Google Firebase 向特定用户发送推送通知?

ios - 如何快速交换 NSArray 元素

ios - 如何使用 Autolayout 为通用应用程序调整 UIView 的大小

ios - Swift:遍历 plist

ios - 未调用 UIImagePickerControllerDelegate

ios - 如何限制不支持空投功能的iOS设备?

ios - 从 plist 加载数据到 tableview