[iPhone] Property Lists and Key-Valued Coding

744阅读 0评论2011-09-12 web_surf
分类:嵌入式

  1. Docs
  2. Summary
    Mac OS X applications use XML propertylists (or plists) for storing things such as your
    default preferences, application settings, and configuration information.
  3. Read/Write
    • If your objects are of type  NSString , NSDictionary , NSArray , NSDate , NSData , or NSNumber , you can use the  writeToFile:atomically: method implemented in these
      classes to write your data to a file.

      Sample:
      Write to file:
      NSDictionary  *glossary;
      ....
      if ([glossary writeToFile: @ "glossary" atomically: YES] == NO)
          NSLog (@ "Save to file failed!");

      Read from file:
      NSDictionary *glossary;
      glossary = [NSDictionary dictionaryWithContentsOfFile: @ "glossary "];
      for ( NSString *key in glossary )
          NSLog (@"%@: %@ " ,  key, [glossary objectForKey: key]);
       

    • A more flexible approach enables you to save any type of objects to a file, not just strings, arrays, and dictionaries. This is done by creating a keyed archive using the NSKeyedArchiver class.

      Sample:
      Write to file:
      NSDictionary *glossary;
      ......
      [NSKeyedArchiver archiveRootObject: glossary toFile: @ "glossary.archive"];

      Read from file:
      NSDictionary *glossary;
      glossary = [NSKeyedUnarchiver unarchiveObjectWithFile:@"glossary.archive"];
      for ( NSString *key in glossary )
          NSLog (@ ” %@: %@ ” ,  key, [glossary objectForKey: key]);

  4. xxx

上一篇:[Android] Device Admin: Customize Lock Screen
下一篇:[SE] Git