How to read data structure from .plist file into NSArray

后端 未结 8 851
故里飘歌
故里飘歌 2020-11-28 20:15

I was creating a data structure manually using the following:

NSDictionary* league1 = [[NSDictionary alloc] initWithObjectsAndKeys: @\"Barclays Premier Leagu         


        
8条回答
  •  臣服心动
    2020-11-28 20:51

    Use this code if the plist is in the resources folder of the project.

      NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"league"    ofType:@"plist"];
     contentArray = [NSArray arrayWithContentsOfFile:sourcePath];
    

    If the plist is inside the document directory of the app use this:

        NSArray *paths = 
    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    
    NSString *plistName = @"league";
    NSString *finalPath = [basePath stringByAppendingPathComponent: 
                           [NSString stringWithFormat: @"%@.plist", plistName]];
    
    
    
    contentArray = [NSArray arrayWithContentsOfFile:finalPath];
    

提交回复
热议问题