📜 ⬆️ ⬇️

Working with Cocoa / CocoaTouch .plist Files

Good luck to everyone!

Today I would like to talk about some aspects of saving settings and other program data in OS X and / or iOS. As usual, we have several options: Core Data, bare-bones SQLite, its binary formats, its text formats, NSUserDefaults and, as you have probably heard, PLIST files, that is, XML P roperty List .

In short, plist files are plain XML, but with some reservations. For example, the order of tags in it is determined by some rules: they go in pairs “key-value”, but tags like “key” and tags like “value” are on the same level. Typical example:

<key>identifier</key> <string>j3qq4-h7h2v</string> 

Plists can store basic Cocoa data types: NSString, NSNumber (int, float, BOOL), NSDate, NSArray, NSDictionary, and NSData. The following tags correspond to these types: , , , <true/>, <false/>, , , , . , plist , .

- , , API .

plist' : " ?", " ?", " ?" . , .

, , , . : , . , , . : NSCoding NSData NSKeyedArchiver. NSData . .

. , bplist ( B inary Plist ), . : Xcode , XML , : plutil -convert xml1 MyFile.plist . , plutil JSON, - , .

NSUserDefaults, . , ~/Library/Preferences/com.yourcompany.yourapp.plist (, , , , bplist), . , ? NSPropertyListSerialization , Cocoa.

, ? , NSDictionary NSArray NSData, plist. , , : NSData NSDictionary NSArray.

: ( ) , .

- (IBAction)savePlist:(id)sender { NSMutableDictionary *root = [NSMutableDictionary dictionary]; [root setObject:@YES forKey:@"autosave"]; [root setObject:@"hello" forKey:@"greet-text"]; [root setObject:@"4F4@@" forKey:@"identifier"]; NSMutableArray *elements = [NSMutableArray array]; [elements addObject:@"one"]; [elements addObject:@"two"]; [elements addObject:@"thee"]; [root setObject:elements forKey:@"elements"]; NSMutableArray *subs = [NSMutableArray array]; for (NSInteger i = 0; i < 10; i++) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSString stringWithFormat:@"John %ld", i] forKey:@"name"]; [dict setObject:[NSString stringWithFormat:@"Moscow %ld", i] forKey:@"city"]; [dict setObject:[NSNumber numberWithInteger:i] forKey:@"id"]; [subs addObject:dict]; } [root setObject:subs forKey:@"subs"]; NSLog(@"saving data:\n%@", root); NSError *error = nil; NSData *representation = [NSPropertyListSerialization dataWithPropertyList:root format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; if (!error) { BOOL ok = [representation writeToFile:self.plistFileName atomically:YES]; if (ok) { NSLog(@"ok!"); } else { NSLog(@"error writing to file: %@", self.plistFileName); } } else { NSLog(@"error: %@", error); } }
, , , :

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>autosave</key> <true/> <key>elements</key> <array> <string>one</string> <string>two</string> <string>thee</string> </array> <key>greet-text</key> <string>hello</string> <key>identifier</key> <string>4F4@@</string> <key>subs</key> <array> <dict> <key>city</key> <string>Moscow 0</string> <key>id</key> <integer>0</integer> <key>name</key> <string>John 0</string> </dict> <dict> <key>city</key> <string>Moscow 1</string> <key>id</key> <integer>1</integer> <key>name</key> <string>John 1</string> </dict> <!-- --> </array> </dict> </plist>
, ? ! XML . :

{ autosave = 1; elements = ( one, two, thee ); "greet-text" = hello; identifier = "4F4@@"; subs = ( { city = "Moscow 0"; id = 0; name = "John 0"; }, { city = "Moscow 1"; id = 1; name = "John 1"; } ); }
.

:

- (IBAction)loadPlist:(id)sender { NSData *plistData = [NSData dataWithContentsOfFile:self.plistFileName]; if (!plistData) { NSLog(@"error reading from file: %@", self.plistFileName); return; } NSPropertyListFormat format; NSError *error = nil; id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error]; if (!error) { NSMutableDictionary *root = plist; NSLog(@"loaded data:\n%@", root); } else { NSLog(@"error: %@", error); } }
? ! JSON-, ! , , : NSDictionary . .

, "mutable" , NSPropertyListMutableContainersAndLeaves . NSPropertyListImmutable , NSMutableDictionary, NSDictionary, .

, PLIST Cocoa. , , .

!

UPD: mejedi , plain-XML .

XML « », ( , «hello world» , ).

, — 10.6 , , 10.8 , - ( __CFBinaryPlistWrite).
, , , <true/>, <false/>, , , , . , plist , .

- , , API .

plist' : " ?", " ?", " ?" . , .

, , , . : , . , , . : NSCoding NSData NSKeyedArchiver. NSData . .

. , bplist ( B inary Plist ), . : Xcode , XML , : plutil -convert xml1 MyFile.plist
. , plutil JSON, - , .

NSUserDefaults, . , ~/Library/Preferences/com.yourcompany.yourapp.plist (, , , , bplist), . , ? NSPropertyListSerialization , Cocoa.

, ? , NSDictionary NSArray NSData, plist. , , : NSData NSDictionary NSArray.

: ( ) , .

- (IBAction)savePlist:(id)sender { NSMutableDictionary *root = [NSMutableDictionary dictionary]; [root setObject:@YES forKey:@"autosave"]; [root setObject:@"hello" forKey:@"greet-text"]; [root setObject:@"4F4@@" forKey:@"identifier"]; NSMutableArray *elements = [NSMutableArray array]; [elements addObject:@"one"]; [elements addObject:@"two"]; [elements addObject:@"thee"]; [root setObject:elements forKey:@"elements"]; NSMutableArray *subs = [NSMutableArray array]; for (NSInteger i = 0; i < 10; i++) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSString stringWithFormat:@"John %ld", i] forKey:@"name"]; [dict setObject:[NSString stringWithFormat:@"Moscow %ld", i] forKey:@"city"]; [dict setObject:[NSNumber numberWithInteger:i] forKey:@"id"]; [subs addObject:dict]; } [root setObject:subs forKey:@"subs"]; NSLog(@"saving data:\n%@", root); NSError *error = nil; NSData *representation = [NSPropertyListSerialization dataWithPropertyList:root format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; if (!error) { BOOL ok = [representation writeToFile:self.plistFileName atomically:YES]; if (ok) { NSLog(@"ok!"); } else { NSLog(@"error writing to file: %@", self.plistFileName); } } else { NSLog(@"error: %@", error); } }
, , , :

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>autosave</key> <true/> <key>elements</key> <array> <string>one</string> <string>two</string> <string>thee</string> </array> <key>greet-text</key> <string>hello</string> <key>identifier</key> <string>4F4@@</string> <key>subs</key> <array> <dict> <key>city</key> <string>Moscow 0</string> <key>id</key> <integer>0</integer> <key>name</key> <string>John 0</string> </dict> <dict> <key>city</key> <string>Moscow 1</string> <key>id</key> <integer>1</integer> <key>name</key> <string>John 1</string> </dict> <!-- --> </array> </dict> </plist>
, ? ! XML . :

{ autosave = 1; elements = ( one, two, thee ); "greet-text" = hello; identifier = "4F4@@"; subs = ( { city = "Moscow 0"; id = 0; name = "John 0"; }, { city = "Moscow 1"; id = 1; name = "John 1"; } ); }
.

:

- (IBAction)loadPlist:(id)sender { NSData *plistData = [NSData dataWithContentsOfFile:self.plistFileName]; if (!plistData) { NSLog(@"error reading from file: %@", self.plistFileName); return; } NSPropertyListFormat format; NSError *error = nil; id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error]; if (!error) { NSMutableDictionary *root = plist; NSLog(@"loaded data:\n%@", root); } else { NSLog(@"error: %@", error); } }
? ! JSON-, ! , , : NSDictionary . .

, "mutable" , NSPropertyListMutableContainersAndLeaves . NSPropertyListImmutable , NSMutableDictionary, NSDictionary, .

, PLIST Cocoa. , , .

!

UPD: mejedi , plain-XML .

XML « », ( , «hello world» , ).

, — 10.6 , , 10.8 , - ( __CFBinaryPlistWrite).
 , , , <true/>, <false/>, , , , . , plist    ,       . 

- , , API .

plist' : " ?", " ?", " ?" . , .

, , , . : , . , , . : NSCoding NSData NSKeyedArchiver. NSData . .

. , bplist ( B inary Plist ), . : Xcode , XML , : plutil -convert xml1 MyFile.plist
. , plutil JSON, - , .

NSUserDefaults, . , ~/Library/Preferences/com.yourcompany.yourapp.plist (, , , , bplist), . , ? NSPropertyListSerialization , Cocoa.

, ? , NSDictionary NSArray NSData, plist. , , : NSData NSDictionary NSArray.

: ( ) , .

- (IBAction)savePlist:(id)sender { NSMutableDictionary *root = [NSMutableDictionary dictionary]; [root setObject:@YES forKey:@"autosave"]; [root setObject:@"hello" forKey:@"greet-text"]; [root setObject:@"4F4@@" forKey:@"identifier"]; NSMutableArray *elements = [NSMutableArray array]; [elements addObject:@"one"]; [elements addObject:@"two"]; [elements addObject:@"thee"]; [root setObject:elements forKey:@"elements"]; NSMutableArray *subs = [NSMutableArray array]; for (NSInteger i = 0; i < 10; i++) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSString stringWithFormat:@"John %ld", i] forKey:@"name"]; [dict setObject:[NSString stringWithFormat:@"Moscow %ld", i] forKey:@"city"]; [dict setObject:[NSNumber numberWithInteger:i] forKey:@"id"]; [subs addObject:dict]; } [root setObject:subs forKey:@"subs"]; NSLog(@"saving data:\n%@", root); NSError *error = nil; NSData *representation = [NSPropertyListSerialization dataWithPropertyList:root format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; if (!error) { BOOL ok = [representation writeToFile:self.plistFileName atomically:YES]; if (ok) { NSLog(@"ok!"); } else { NSLog(@"error writing to file: %@", self.plistFileName); } } else { NSLog(@"error: %@", error); } }
, , , :

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>autosave</key> <true/> <key>elements</key> <array> <string>one</string> <string>two</string> <string>thee</string> </array> <key>greet-text</key> <string>hello</string> <key>identifier</key> <string>4F4@@</string> <key>subs</key> <array> <dict> <key>city</key> <string>Moscow 0</string> <key>id</key> <integer>0</integer> <key>name</key> <string>John 0</string> </dict> <dict> <key>city</key> <string>Moscow 1</string> <key>id</key> <integer>1</integer> <key>name</key> <string>John 1</string> </dict> <!-- --> </array> </dict> </plist>
, ? ! XML . :

{ autosave = 1; elements = ( one, two, thee ); "greet-text" = hello; identifier = "4F4@@"; subs = ( { city = "Moscow 0"; id = 0; name = "John 0"; }, { city = "Moscow 1"; id = 1; name = "John 1"; } ); }
.

:

- (IBAction)loadPlist:(id)sender { NSData *plistData = [NSData dataWithContentsOfFile:self.plistFileName]; if (!plistData) { NSLog(@"error reading from file: %@", self.plistFileName); return; } NSPropertyListFormat format; NSError *error = nil; id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error]; if (!error) { NSMutableDictionary *root = plist; NSLog(@"loaded data:\n%@", root); } else { NSLog(@"error: %@", error); } }
? ! JSON-, ! , , : NSDictionary . .

, "mutable" , NSPropertyListMutableContainersAndLeaves . NSPropertyListImmutable , NSMutableDictionary, NSDictionary, .

, PLIST Cocoa. , , .

!

UPD: mejedi , plain-XML .

XML « », ( , «hello world» , ).

, — 10.6 , , 10.8 , - ( __CFBinaryPlistWrite).
, , , <true/>, <false/>, , , , . , plist , .

- , , API .

plist' : " ?", " ?", " ?" . , .

, , , . : , . , , . : NSCoding NSData NSKeyedArchiver. NSData . .

. , bplist ( B inary Plist ), . : Xcode , XML , : plutil -convert xml1 MyFile.plist
. , plutil JSON, - , .

NSUserDefaults, . , ~/Library/Preferences/com.yourcompany.yourapp.plist (, , , , bplist), . , ? NSPropertyListSerialization , Cocoa.

, ? , NSDictionary NSArray NSData, plist. , , : NSData NSDictionary NSArray.

: ( ) , .

- (IBAction)savePlist:(id)sender { NSMutableDictionary *root = [NSMutableDictionary dictionary]; [root setObject:@YES forKey:@"autosave"]; [root setObject:@"hello" forKey:@"greet-text"]; [root setObject:@"4F4@@" forKey:@"identifier"]; NSMutableArray *elements = [NSMutableArray array]; [elements addObject:@"one"]; [elements addObject:@"two"]; [elements addObject:@"thee"]; [root setObject:elements forKey:@"elements"]; NSMutableArray *subs = [NSMutableArray array]; for (NSInteger i = 0; i < 10; i++) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSString stringWithFormat:@"John %ld", i] forKey:@"name"]; [dict setObject:[NSString stringWithFormat:@"Moscow %ld", i] forKey:@"city"]; [dict setObject:[NSNumber numberWithInteger:i] forKey:@"id"]; [subs addObject:dict]; } [root setObject:subs forKey:@"subs"]; NSLog(@"saving data:\n%@", root); NSError *error = nil; NSData *representation = [NSPropertyListSerialization dataWithPropertyList:root format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; if (!error) { BOOL ok = [representation writeToFile:self.plistFileName atomically:YES]; if (ok) { NSLog(@"ok!"); } else { NSLog(@"error writing to file: %@", self.plistFileName); } } else { NSLog(@"error: %@", error); } }
, , , :

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>autosave</key> <true/> <key>elements</key> <array> <string>one</string> <string>two</string> <string>thee</string> </array> <key>greet-text</key> <string>hello</string> <key>identifier</key> <string>4F4@@</string> <key>subs</key> <array> <dict> <key>city</key> <string>Moscow 0</string> <key>id</key> <integer>0</integer> <key>name</key> <string>John 0</string> </dict> <dict> <key>city</key> <string>Moscow 1</string> <key>id</key> <integer>1</integer> <key>name</key> <string>John 1</string> </dict> <!-- --> </array> </dict> </plist>
, ? ! XML . :

{ autosave = 1; elements = ( one, two, thee ); "greet-text" = hello; identifier = "4F4@@"; subs = ( { city = "Moscow 0"; id = 0; name = "John 0"; }, { city = "Moscow 1"; id = 1; name = "John 1"; } ); }
.

:

- (IBAction)loadPlist:(id)sender { NSData *plistData = [NSData dataWithContentsOfFile:self.plistFileName]; if (!plistData) { NSLog(@"error reading from file: %@", self.plistFileName); return; } NSPropertyListFormat format; NSError *error = nil; id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error]; if (!error) { NSMutableDictionary *root = plist; NSLog(@"loaded data:\n%@", root); } else { NSLog(@"error: %@", error); } }
? ! JSON-, ! , , : NSDictionary . .

, "mutable" , NSPropertyListMutableContainersAndLeaves . NSPropertyListImmutable , NSMutableDictionary, NSDictionary, .

, PLIST Cocoa. , , .

!

UPD: mejedi , plain-XML .

XML « », ( , «hello world» , ).

, — 10.6 , , 10.8 , - ( __CFBinaryPlistWrite).
 , , , <true/>, <false/>, , , , . , plist    ,       . 

- , , API .

plist' : " ?", " ?", " ?" . , .

, , , . : , . , , . : NSCoding NSData NSKeyedArchiver. NSData . .

. , bplist ( B inary Plist ), . : Xcode , XML , : plutil -convert xml1 MyFile.plist
. , plutil JSON, - , .

NSUserDefaults, . , ~/Library/Preferences/com.yourcompany.yourapp.plist (, , , , bplist), . , ? NSPropertyListSerialization , Cocoa.

, ? , NSDictionary NSArray NSData, plist. , , : NSData NSDictionary NSArray.

: ( ) , .

- (IBAction)savePlist:(id)sender { NSMutableDictionary *root = [NSMutableDictionary dictionary]; [root setObject:@YES forKey:@"autosave"]; [root setObject:@"hello" forKey:@"greet-text"]; [root setObject:@"4F4@@" forKey:@"identifier"]; NSMutableArray *elements = [NSMutableArray array]; [elements addObject:@"one"]; [elements addObject:@"two"]; [elements addObject:@"thee"]; [root setObject:elements forKey:@"elements"]; NSMutableArray *subs = [NSMutableArray array]; for (NSInteger i = 0; i < 10; i++) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSString stringWithFormat:@"John %ld", i] forKey:@"name"]; [dict setObject:[NSString stringWithFormat:@"Moscow %ld", i] forKey:@"city"]; [dict setObject:[NSNumber numberWithInteger:i] forKey:@"id"]; [subs addObject:dict]; } [root setObject:subs forKey:@"subs"]; NSLog(@"saving data:\n%@", root); NSError *error = nil; NSData *representation = [NSPropertyListSerialization dataWithPropertyList:root format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; if (!error) { BOOL ok = [representation writeToFile:self.plistFileName atomically:YES]; if (ok) { NSLog(@"ok!"); } else { NSLog(@"error writing to file: %@", self.plistFileName); } } else { NSLog(@"error: %@", error); } }
, , , :

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>autosave</key> <true/> <key>elements</key> <array> <string>one</string> <string>two</string> <string>thee</string> </array> <key>greet-text</key> <string>hello</string> <key>identifier</key> <string>4F4@@</string> <key>subs</key> <array> <dict> <key>city</key> <string>Moscow 0</string> <key>id</key> <integer>0</integer> <key>name</key> <string>John 0</string> </dict> <dict> <key>city</key> <string>Moscow 1</string> <key>id</key> <integer>1</integer> <key>name</key> <string>John 1</string> </dict> <!-- --> </array> </dict> </plist>
, ? ! XML . :

{ autosave = 1; elements = ( one, two, thee ); "greet-text" = hello; identifier = "4F4@@"; subs = ( { city = "Moscow 0"; id = 0; name = "John 0"; }, { city = "Moscow 1"; id = 1; name = "John 1"; } ); }
.

:

- (IBAction)loadPlist:(id)sender { NSData *plistData = [NSData dataWithContentsOfFile:self.plistFileName]; if (!plistData) { NSLog(@"error reading from file: %@", self.plistFileName); return; } NSPropertyListFormat format; NSError *error = nil; id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error]; if (!error) { NSMutableDictionary *root = plist; NSLog(@"loaded data:\n%@", root); } else { NSLog(@"error: %@", error); } }
? ! JSON-, ! , , : NSDictionary . .

, "mutable" , NSPropertyListMutableContainersAndLeaves . NSPropertyListImmutable , NSMutableDictionary, NSDictionary, .

, PLIST Cocoa. , , .

!

UPD: mejedi , plain-XML .

XML « », ( , «hello world» , ).

, — 10.6 , , 10.8 , - ( __CFBinaryPlistWrite).
, , , <true/>, <false/>, , , , . , plist , .

- , , API .

plist' : " ?", " ?", " ?" . , .

, , , . : , . , , . : NSCoding NSData NSKeyedArchiver. NSData . .

. , bplist ( B inary Plist ), . : Xcode , XML , : plutil -convert xml1 MyFile.plist
. , plutil JSON, - , .

NSUserDefaults, . , ~/Library/Preferences/com.yourcompany.yourapp.plist (, , , , bplist), . , ? NSPropertyListSerialization , Cocoa.

, ? , NSDictionary NSArray NSData, plist. , , : NSData NSDictionary NSArray.

: ( ) , .

- (IBAction)savePlist:(id)sender { NSMutableDictionary *root = [NSMutableDictionary dictionary]; [root setObject:@YES forKey:@"autosave"]; [root setObject:@"hello" forKey:@"greet-text"]; [root setObject:@"4F4@@" forKey:@"identifier"]; NSMutableArray *elements = [NSMutableArray array]; [elements addObject:@"one"]; [elements addObject:@"two"]; [elements addObject:@"thee"]; [root setObject:elements forKey:@"elements"]; NSMutableArray *subs = [NSMutableArray array]; for (NSInteger i = 0; i < 10; i++) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSString stringWithFormat:@"John %ld", i] forKey:@"name"]; [dict setObject:[NSString stringWithFormat:@"Moscow %ld", i] forKey:@"city"]; [dict setObject:[NSNumber numberWithInteger:i] forKey:@"id"]; [subs addObject:dict]; } [root setObject:subs forKey:@"subs"]; NSLog(@"saving data:\n%@", root); NSError *error = nil; NSData *representation = [NSPropertyListSerialization dataWithPropertyList:root format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; if (!error) { BOOL ok = [representation writeToFile:self.plistFileName atomically:YES]; if (ok) { NSLog(@"ok!"); } else { NSLog(@"error writing to file: %@", self.plistFileName); } } else { NSLog(@"error: %@", error); } }
, , , :

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>autosave</key> <true/> <key>elements</key> <array> <string>one</string> <string>two</string> <string>thee</string> </array> <key>greet-text</key> <string>hello</string> <key>identifier</key> <string>4F4@@</string> <key>subs</key> <array> <dict> <key>city</key> <string>Moscow 0</string> <key>id</key> <integer>0</integer> <key>name</key> <string>John 0</string> </dict> <dict> <key>city</key> <string>Moscow 1</string> <key>id</key> <integer>1</integer> <key>name</key> <string>John 1</string> </dict> <!-- --> </array> </dict> </plist>
, ? ! XML . :

{ autosave = 1; elements = ( one, two, thee ); "greet-text" = hello; identifier = "4F4@@"; subs = ( { city = "Moscow 0"; id = 0; name = "John 0"; }, { city = "Moscow 1"; id = 1; name = "John 1"; } ); }
.

:

- (IBAction)loadPlist:(id)sender { NSData *plistData = [NSData dataWithContentsOfFile:self.plistFileName]; if (!plistData) { NSLog(@"error reading from file: %@", self.plistFileName); return; } NSPropertyListFormat format; NSError *error = nil; id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error]; if (!error) { NSMutableDictionary *root = plist; NSLog(@"loaded data:\n%@", root); } else { NSLog(@"error: %@", error); } }
? ! JSON-, ! , , : NSDictionary . .

, "mutable" , NSPropertyListMutableContainersAndLeaves . NSPropertyListImmutable , NSMutableDictionary, NSDictionary, .

, PLIST Cocoa. , , .

!

UPD: mejedi , plain-XML .

XML « », ( , «hello world» , ).

, — 10.6 , , 10.8 , - ( __CFBinaryPlistWrite).
 , , , <true/>, <false/>, , , , . , plist    ,       . 

- , , API .

plist' : " ?", " ?", " ?" . , .

, , , . : , . , , . : NSCoding NSData NSKeyedArchiver. NSData . .

. , bplist ( B inary Plist ), . : Xcode , XML , : plutil -convert xml1 MyFile.plist
. , plutil JSON, - , .

NSUserDefaults, . , ~/Library/Preferences/com.yourcompany.yourapp.plist (, , , , bplist), . , ? NSPropertyListSerialization , Cocoa.

, ? , NSDictionary NSArray NSData, plist. , , : NSData NSDictionary NSArray.

: ( ) , .

- (IBAction)savePlist:(id)sender { NSMutableDictionary *root = [NSMutableDictionary dictionary]; [root setObject:@YES forKey:@"autosave"]; [root setObject:@"hello" forKey:@"greet-text"]; [root setObject:@"4F4@@" forKey:@"identifier"]; NSMutableArray *elements = [NSMutableArray array]; [elements addObject:@"one"]; [elements addObject:@"two"]; [elements addObject:@"thee"]; [root setObject:elements forKey:@"elements"]; NSMutableArray *subs = [NSMutableArray array]; for (NSInteger i = 0; i < 10; i++) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSString stringWithFormat:@"John %ld", i] forKey:@"name"]; [dict setObject:[NSString stringWithFormat:@"Moscow %ld", i] forKey:@"city"]; [dict setObject:[NSNumber numberWithInteger:i] forKey:@"id"]; [subs addObject:dict]; } [root setObject:subs forKey:@"subs"]; NSLog(@"saving data:\n%@", root); NSError *error = nil; NSData *representation = [NSPropertyListSerialization dataWithPropertyList:root format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; if (!error) { BOOL ok = [representation writeToFile:self.plistFileName atomically:YES]; if (ok) { NSLog(@"ok!"); } else { NSLog(@"error writing to file: %@", self.plistFileName); } } else { NSLog(@"error: %@", error); } }
, , , :

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>autosave</key> <true/> <key>elements</key> <array> <string>one</string> <string>two</string> <string>thee</string> </array> <key>greet-text</key> <string>hello</string> <key>identifier</key> <string>4F4@@</string> <key>subs</key> <array> <dict> <key>city</key> <string>Moscow 0</string> <key>id</key> <integer>0</integer> <key>name</key> <string>John 0</string> </dict> <dict> <key>city</key> <string>Moscow 1</string> <key>id</key> <integer>1</integer> <key>name</key> <string>John 1</string> </dict> <!-- --> </array> </dict> </plist>
, ? ! XML . :

{ autosave = 1; elements = ( one, two, thee ); "greet-text" = hello; identifier = "4F4@@"; subs = ( { city = "Moscow 0"; id = 0; name = "John 0"; }, { city = "Moscow 1"; id = 1; name = "John 1"; } ); }
.

:

- (IBAction)loadPlist:(id)sender { NSData *plistData = [NSData dataWithContentsOfFile:self.plistFileName]; if (!plistData) { NSLog(@"error reading from file: %@", self.plistFileName); return; } NSPropertyListFormat format; NSError *error = nil; id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error]; if (!error) { NSMutableDictionary *root = plist; NSLog(@"loaded data:\n%@", root); } else { NSLog(@"error: %@", error); } }
? ! JSON-, ! , , : NSDictionary . .

, "mutable" , NSPropertyListMutableContainersAndLeaves . NSPropertyListImmutable , NSMutableDictionary, NSDictionary, .

, PLIST Cocoa. , , .

!

UPD: mejedi , plain-XML .

XML « », ( , «hello world» , ).

, — 10.6 , , 10.8 , - ( __CFBinaryPlistWrite).
, , , <true/>, <false/>, , , , . , plist , .

- , , API .

plist' : " ?", " ?", " ?" . , .

, , , . : , . , , . : NSCoding NSData NSKeyedArchiver. NSData . .

. , bplist ( B inary Plist ), . : Xcode , XML , : plutil -convert xml1 MyFile.plist
. , plutil JSON, - , .

NSUserDefaults, . , ~/Library/Preferences/com.yourcompany.yourapp.plist (, , , , bplist), . , ? NSPropertyListSerialization , Cocoa.

, ? , NSDictionary NSArray NSData, plist. , , : NSData NSDictionary NSArray.

: ( ) , .

- (IBAction)savePlist:(id)sender { NSMutableDictionary *root = [NSMutableDictionary dictionary]; [root setObject:@YES forKey:@"autosave"]; [root setObject:@"hello" forKey:@"greet-text"]; [root setObject:@"4F4@@" forKey:@"identifier"]; NSMutableArray *elements = [NSMutableArray array]; [elements addObject:@"one"]; [elements addObject:@"two"]; [elements addObject:@"thee"]; [root setObject:elements forKey:@"elements"]; NSMutableArray *subs = [NSMutableArray array]; for (NSInteger i = 0; i < 10; i++) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSString stringWithFormat:@"John %ld", i] forKey:@"name"]; [dict setObject:[NSString stringWithFormat:@"Moscow %ld", i] forKey:@"city"]; [dict setObject:[NSNumber numberWithInteger:i] forKey:@"id"]; [subs addObject:dict]; } [root setObject:subs forKey:@"subs"]; NSLog(@"saving data:\n%@", root); NSError *error = nil; NSData *representation = [NSPropertyListSerialization dataWithPropertyList:root format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; if (!error) { BOOL ok = [representation writeToFile:self.plistFileName atomically:YES]; if (ok) { NSLog(@"ok!"); } else { NSLog(@"error writing to file: %@", self.plistFileName); } } else { NSLog(@"error: %@", error); } }
, , , :

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>autosave</key> <true/> <key>elements</key> <array> <string>one</string> <string>two</string> <string>thee</string> </array> <key>greet-text</key> <string>hello</string> <key>identifier</key> <string>4F4@@</string> <key>subs</key> <array> <dict> <key>city</key> <string>Moscow 0</string> <key>id</key> <integer>0</integer> <key>name</key> <string>John 0</string> </dict> <dict> <key>city</key> <string>Moscow 1</string> <key>id</key> <integer>1</integer> <key>name</key> <string>John 1</string> </dict> <!-- --> </array> </dict> </plist>
, ? ! XML . :

{ autosave = 1; elements = ( one, two, thee ); "greet-text" = hello; identifier = "4F4@@"; subs = ( { city = "Moscow 0"; id = 0; name = "John 0"; }, { city = "Moscow 1"; id = 1; name = "John 1"; } ); }
.

:

- (IBAction)loadPlist:(id)sender { NSData *plistData = [NSData dataWithContentsOfFile:self.plistFileName]; if (!plistData) { NSLog(@"error reading from file: %@", self.plistFileName); return; } NSPropertyListFormat format; NSError *error = nil; id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error]; if (!error) { NSMutableDictionary *root = plist; NSLog(@"loaded data:\n%@", root); } else { NSLog(@"error: %@", error); } }
? ! JSON-, ! , , : NSDictionary . .

, "mutable" , NSPropertyListMutableContainersAndLeaves . NSPropertyListImmutable , NSMutableDictionary, NSDictionary, .

, PLIST Cocoa. , , .

!

UPD: mejedi , plain-XML .

XML « », ( , «hello world» , ).

, — 10.6 , , 10.8 , - ( __CFBinaryPlistWrite).
 , , , <true/>, <false/>, , , , . , plist    ,       . 

- , , API .

plist' : " ?", " ?", " ?" . , .

, , , . : , . , , . : NSCoding NSData NSKeyedArchiver. NSData . .

. , bplist ( B inary Plist ), . : Xcode , XML , : plutil -convert xml1 MyFile.plist
. , plutil JSON, - , .

NSUserDefaults, . , ~/Library/Preferences/com.yourcompany.yourapp.plist (, , , , bplist), . , ? NSPropertyListSerialization , Cocoa.

, ? , NSDictionary NSArray NSData, plist. , , : NSData NSDictionary NSArray.

: ( ) , .

- (IBAction)savePlist:(id)sender { NSMutableDictionary *root = [NSMutableDictionary dictionary]; [root setObject:@YES forKey:@"autosave"]; [root setObject:@"hello" forKey:@"greet-text"]; [root setObject:@"4F4@@" forKey:@"identifier"]; NSMutableArray *elements = [NSMutableArray array]; [elements addObject:@"one"]; [elements addObject:@"two"]; [elements addObject:@"thee"]; [root setObject:elements forKey:@"elements"]; NSMutableArray *subs = [NSMutableArray array]; for (NSInteger i = 0; i < 10; i++) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSString stringWithFormat:@"John %ld", i] forKey:@"name"]; [dict setObject:[NSString stringWithFormat:@"Moscow %ld", i] forKey:@"city"]; [dict setObject:[NSNumber numberWithInteger:i] forKey:@"id"]; [subs addObject:dict]; } [root setObject:subs forKey:@"subs"]; NSLog(@"saving data:\n%@", root); NSError *error = nil; NSData *representation = [NSPropertyListSerialization dataWithPropertyList:root format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; if (!error) { BOOL ok = [representation writeToFile:self.plistFileName atomically:YES]; if (ok) { NSLog(@"ok!"); } else { NSLog(@"error writing to file: %@", self.plistFileName); } } else { NSLog(@"error: %@", error); } }
, , , :

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>autosave</key> <true/> <key>elements</key> <array> <string>one</string> <string>two</string> <string>thee</string> </array> <key>greet-text</key> <string>hello</string> <key>identifier</key> <string>4F4@@</string> <key>subs</key> <array> <dict> <key>city</key> <string>Moscow 0</string> <key>id</key> <integer>0</integer> <key>name</key> <string>John 0</string> </dict> <dict> <key>city</key> <string>Moscow 1</string> <key>id</key> <integer>1</integer> <key>name</key> <string>John 1</string> </dict> <!-- --> </array> </dict> </plist>
, ? ! XML . :

{ autosave = 1; elements = ( one, two, thee ); "greet-text" = hello; identifier = "4F4@@"; subs = ( { city = "Moscow 0"; id = 0; name = "John 0"; }, { city = "Moscow 1"; id = 1; name = "John 1"; } ); }
.

:

- (IBAction)loadPlist:(id)sender { NSData *plistData = [NSData dataWithContentsOfFile:self.plistFileName]; if (!plistData) { NSLog(@"error reading from file: %@", self.plistFileName); return; } NSPropertyListFormat format; NSError *error = nil; id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error]; if (!error) { NSMutableDictionary *root = plist; NSLog(@"loaded data:\n%@", root); } else { NSLog(@"error: %@", error); } }
? ! JSON-, ! , , : NSDictionary . .

, "mutable" , NSPropertyListMutableContainersAndLeaves . NSPropertyListImmutable , NSMutableDictionary, NSDictionary, .

, PLIST Cocoa. , , .

!

UPD: mejedi , plain-XML .

XML « », ( , «hello world» , ).

, — 10.6 , , 10.8 , - ( __CFBinaryPlistWrite).
, , , <true/>, <false/>, , , , . , plist , .

- , , API .

plist' : " ?", " ?", " ?" . , .

, , , . : , . , , . : NSCoding NSData NSKeyedArchiver. NSData . .

. , bplist ( B inary Plist ), . : Xcode , XML , : plutil -convert xml1 MyFile.plist
. , plutil JSON, - , .

NSUserDefaults, . , ~/Library/Preferences/com.yourcompany.yourapp.plist (, , , , bplist), . , ? NSPropertyListSerialization , Cocoa.

, ? , NSDictionary NSArray NSData, plist. , , : NSData NSDictionary NSArray.

: ( ) , .

- (IBAction)savePlist:(id)sender { NSMutableDictionary *root = [NSMutableDictionary dictionary]; [root setObject:@YES forKey:@"autosave"]; [root setObject:@"hello" forKey:@"greet-text"]; [root setObject:@"4F4@@" forKey:@"identifier"]; NSMutableArray *elements = [NSMutableArray array]; [elements addObject:@"one"]; [elements addObject:@"two"]; [elements addObject:@"thee"]; [root setObject:elements forKey:@"elements"]; NSMutableArray *subs = [NSMutableArray array]; for (NSInteger i = 0; i < 10; i++) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSString stringWithFormat:@"John %ld", i] forKey:@"name"]; [dict setObject:[NSString stringWithFormat:@"Moscow %ld", i] forKey:@"city"]; [dict setObject:[NSNumber numberWithInteger:i] forKey:@"id"]; [subs addObject:dict]; } [root setObject:subs forKey:@"subs"]; NSLog(@"saving data:\n%@", root); NSError *error = nil; NSData *representation = [NSPropertyListSerialization dataWithPropertyList:root format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; if (!error) { BOOL ok = [representation writeToFile:self.plistFileName atomically:YES]; if (ok) { NSLog(@"ok!"); } else { NSLog(@"error writing to file: %@", self.plistFileName); } } else { NSLog(@"error: %@", error); } }
, , , :

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>autosave</key> <true/> <key>elements</key> <array> <string>one</string> <string>two</string> <string>thee</string> </array> <key>greet-text</key> <string>hello</string> <key>identifier</key> <string>4F4@@</string> <key>subs</key> <array> <dict> <key>city</key> <string>Moscow 0</string> <key>id</key> <integer>0</integer> <key>name</key> <string>John 0</string> </dict> <dict> <key>city</key> <string>Moscow 1</string> <key>id</key> <integer>1</integer> <key>name</key> <string>John 1</string> </dict> <!-- --> </array> </dict> </plist>
, ? ! XML . :

{ autosave = 1; elements = ( one, two, thee ); "greet-text" = hello; identifier = "4F4@@"; subs = ( { city = "Moscow 0"; id = 0; name = "John 0"; }, { city = "Moscow 1"; id = 1; name = "John 1"; } ); }
.

:

- (IBAction)loadPlist:(id)sender { NSData *plistData = [NSData dataWithContentsOfFile:self.plistFileName]; if (!plistData) { NSLog(@"error reading from file: %@", self.plistFileName); return; } NSPropertyListFormat format; NSError *error = nil; id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error]; if (!error) { NSMutableDictionary *root = plist; NSLog(@"loaded data:\n%@", root); } else { NSLog(@"error: %@", error); } }
? ! JSON-, ! , , : NSDictionary . .

, "mutable" , NSPropertyListMutableContainersAndLeaves . NSPropertyListImmutable , NSMutableDictionary, NSDictionary, .

, PLIST Cocoa. , , .

!

UPD: mejedi , plain-XML .

XML « », ( , «hello world» , ).

, — 10.6 , , 10.8 , - ( __CFBinaryPlistWrite).
, , , <true/>, <false/>, , , , . , plist , .

- , , API .

plist' : " ?", " ?", " ?" . , .

, , , . : , . , , . : NSCoding NSData NSKeyedArchiver. NSData . .

. , bplist ( B inary Plist ), . : Xcode , XML , : plutil -convert xml1 MyFile.plist
. , plutil JSON, - , .

NSUserDefaults, . , ~/Library/Preferences/com.yourcompany.yourapp.plist (, , , , bplist), . , ? NSPropertyListSerialization , Cocoa.

, ? , NSDictionary NSArray NSData, plist. , , : NSData NSDictionary NSArray.

: ( ) , .

- (IBAction)savePlist:(id)sender { NSMutableDictionary *root = [NSMutableDictionary dictionary]; [root setObject:@YES forKey:@"autosave"]; [root setObject:@"hello" forKey:@"greet-text"]; [root setObject:@"4F4@@" forKey:@"identifier"]; NSMutableArray *elements = [NSMutableArray array]; [elements addObject:@"one"]; [elements addObject:@"two"]; [elements addObject:@"thee"]; [root setObject:elements forKey:@"elements"]; NSMutableArray *subs = [NSMutableArray array]; for (NSInteger i = 0; i < 10; i++) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSString stringWithFormat:@"John %ld", i] forKey:@"name"]; [dict setObject:[NSString stringWithFormat:@"Moscow %ld", i] forKey:@"city"]; [dict setObject:[NSNumber numberWithInteger:i] forKey:@"id"]; [subs addObject:dict]; } [root setObject:subs forKey:@"subs"]; NSLog(@"saving data:\n%@", root); NSError *error = nil; NSData *representation = [NSPropertyListSerialization dataWithPropertyList:root format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; if (!error) { BOOL ok = [representation writeToFile:self.plistFileName atomically:YES]; if (ok) { NSLog(@"ok!"); } else { NSLog(@"error writing to file: %@", self.plistFileName); } } else { NSLog(@"error: %@", error); } }
, , , :

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>autosave</key> <true/> <key>elements</key> <array> <string>one</string> <string>two</string> <string>thee</string> </array> <key>greet-text</key> <string>hello</string> <key>identifier</key> <string>4F4@@</string> <key>subs</key> <array> <dict> <key>city</key> <string>Moscow 0</string> <key>id</key> <integer>0</integer> <key>name</key> <string>John 0</string> </dict> <dict> <key>city</key> <string>Moscow 1</string> <key>id</key> <integer>1</integer> <key>name</key> <string>John 1</string> </dict> <!-- --> </array> </dict> </plist>
, ? ! XML . :

{ autosave = 1; elements = ( one, two, thee ); "greet-text" = hello; identifier = "4F4@@"; subs = ( { city = "Moscow 0"; id = 0; name = "John 0"; }, { city = "Moscow 1"; id = 1; name = "John 1"; } ); }
.

:

- (IBAction)loadPlist:(id)sender { NSData *plistData = [NSData dataWithContentsOfFile:self.plistFileName]; if (!plistData) { NSLog(@"error reading from file: %@", self.plistFileName); return; } NSPropertyListFormat format; NSError *error = nil; id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error]; if (!error) { NSMutableDictionary *root = plist; NSLog(@"loaded data:\n%@", root); } else { NSLog(@"error: %@", error); } }
? ! JSON-, ! , , : NSDictionary . .

, "mutable" , NSPropertyListMutableContainersAndLeaves . NSPropertyListImmutable , NSMutableDictionary, NSDictionary, .

, PLIST Cocoa. , , .

!

UPD: mejedi , plain-XML .

XML « », ( , «hello world» , ).

, — 10.6 , , 10.8 , - ( __CFBinaryPlistWrite).

')

Source: https://habr.com/ru/post/158727/


All Articles