A question on how to Get data from plist & how should it be layout

后端 未结 4 999
清歌不尽
清歌不尽 2020-12-21 20:38

This is a follow up question on my first queries regarding retrieving data on plist. Right now i have manage to detect users touches made on my view with random image call o

4条回答
  •  渐次进展
    2020-12-21 21:28

    Here are two methods to read and write values from a plist using an NSDictionary:

    - (NSMutableDictionary*)dictionaryFromPlist {
        NSString *filePath = @"myPlist.plist";
        NSMutableDictionary* propertyListValues = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
        return [propertyListValues autorelease];
    }
    
    - (BOOL)writeDictionaryToPlist:(NSDictionary*)plistDict{
        NSString *filePath = @"myPlist.plist";
        BOOL result = [plistDict writeToFile:filePath atomically:YES];
        return result;
    }
    

    and then in your code block somewhere:

    // Read key from plist dictionary
    NSDictionary *dict = [self dictionaryFromPlist];
    NSString *valueToPrint = [dict objectForKey:@"Executable file"];
    NSLog(@"valueToPrint: %@", valueToPrint);
    
    // Write key to plist dictionary
    NSString *key = @"Icon File";
    NSString *value = @"appIcon.png";
    [dict setValue:value forKey:key];
    
    // Write new plist to file using dictionary
    [self writeDictionaryToPlist:dict];
    

提交回复
热议问题