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

后端 未结 4 983
清歌不尽
清歌不尽 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:27

    Try this:

    -(void)add:(NSRunningApplication *) app { 
    if ([self contains:app]) return; 
    [self.apps addObject:app.localizedName]; 
     [self.apps writeToFile:self.dataFile atomically:YES];
    }
    
    0 讨论(0)
  • 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];
    
    0 讨论(0)
  • 2020-12-21 21:36

    Look into the documentation for -initWithContentsOfFile:

    You should be able to create dictionaries and arrays from plists, provided that they are standard.

    In this case, you can call [[NSDictionary alloc] initWithContentsOfFile:@"path/to/plist"]; And then you can use -valueForKey: to get the data you need.

    0 讨论(0)
  • 2020-12-21 21:40

    The plist part is well explained in other answers.

    But if you are storing/reading with CGPoint I would strongly suggest you to use the following very handy methods, CGPointFromString and NSStringFromCGPoint see Apple documentation for more information.

    Note there are also other utilities to work with graphics structures like CGPointEqualToPoint, CGRectContainsPoint, CGRectIntersectsRect or CGRectContainsRect... see Apple documentation for more information

    0 讨论(0)
提交回复
热议问题