How can we create our own plist file in a Xcode project?

前端 未结 4 929
一生所求
一生所求 2020-12-09 06:49

I want to create \"UrlPaths.plist\" file in my Application and also a dictionary with 4 or 5 objects. Please help me create a plist file and dictionary. And also read data f

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-09 07:28

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"plist.plist"]; 
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSMutableDictionary *data;
    
    if ([fileManager fileExistsAtPath: path]) {
                data = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
    }
    else {
        // If the file doesn’t exist, create an empty dictionary
        data = [[NSMutableDictionary alloc] init];
    }
    
    //To insert the data into the plist
    data[@"value"] = @(5);
    [data writeToFile: path atomically:YES];
    [data release];
    
    //To retrieve the data from the plist
    NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
    int value1;
    value1 = [savedStock[@"value"] intValue];
    NSLog(@"%i",value1);
    [savedStock release];
    

提交回复
热议问题