Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-29-2010, 12:53 PM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 8
archaicsoftware is on a distinguished road
Default Adding items to plist

Alright, I am using a plist to load data into a tableview. I have all of that working just fine. I have also successfully copied the plist from the bundle to the documents folder.

I need some help figuring out the code needed to add an item to the plist. Here is the structure of the plist thus far

NSDictionary (ROOT)
- NSArray (The Array I am Accessing
- Item 0 (DICTIONARY)
- Name - String
- Category - String
- Image - String
- Item 1 (DICTIONARY)
- Name - String
- Category - String
- Image - String
- Item 2 (DICTIONARY)
- Name - String
- Category - String
- Image - String
- Item 3 (THIS IS WHAT I WANT TO ADD)
- Name - String
- Category - String
- Image - String

I am not sure how to edit the plist. Here is the code i have so far (which is pretty much a hacking together of crap I searched for on the internet)

NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory,NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDir stringByAppendingPathComponent:@"Fav.plist"]; //3

NSMutableDictionary *favPlist = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

//here add elements to data file and write data to file
NSDictionary *newDictionary = [[[NSDictionary alloc] init] autorelease];
newDictionary = [NSDictionary dictionaryWithObjectsAndKeys:trickTitle,@"Title", trickDifficulty, @"Difficulty",trickDescription, @"Description", posterLocation, @"Poster", videoLocation, @"Play", nil];

[favPlist setObject:newDictionary forKey:@"Fav"];
[favPlist writeToFileath atomically:YES];
[favPlist release];
archaicsoftware is offline   Reply With Quote
Old 12-29-2010, 05:17 PM   #2 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 32
garyshaf is on a distinguished road
Default

Quote:
Originally Posted by archaicsoftware View Post
Alright, I am using a plist to load data into a tableview. I have all of that working just fine. I have also successfully copied the plist from the bundle to the documents folder.

I need some help figuring out the code needed to add an item to the plist. Here is the structure of the plist thus far

NSDictionary (ROOT)
- NSArray (The Array I am Accessing
- Item 0 (DICTIONARY)
- Name - String
- Category - String
- Image - String
- Item 1 (DICTIONARY)
- Name - String
- Category - String
- Image - String
- Item 2 (DICTIONARY)
- Name - String
- Category - String
- Image - String
- Item 3 (THIS IS WHAT I WANT TO ADD)
- Name - String
- Category - String
- Image - String

I am not sure how to edit the plist. Here is the code i have so far (which is pretty much a hacking together of crap I searched for on the internet)

NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory,NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDir stringByAppendingPathComponent:@"Fav.plist"]; //3

NSMutableDictionary *favPlist = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

//here add elements to data file and write data to file
NSDictionary *newDictionary = [[[NSDictionary alloc] init] autorelease];
newDictionary = [NSDictionary dictionaryWithObjectsAndKeys:trickTitle,@"Title", trickDifficulty, @"Difficulty",trickDescription, @"Description", posterLocation, @"Poster", videoLocation, @"Play", nil];

[favPlist setObject:newDictionary forKey:@"Fav"];
[favPlist writeToFileath atomically:YES];
[favPlist release];
Learning NSDictionary concepts is quite important. Please take the time to go over the Apple documents, it'll help you tremendously in the future.

I personally like property list more than Core Data. They're easier to work with than the core data framework.

Anyway here is some code I have that may help you understand better:

Code:
sessionDict = [[NSMutableArray alloc] initWithContentsOfFile:pathToUserCopyOfPlist];
		
			//NSLog(@"sessionDict: %@", sessionDict);
		
			NSMutableDictionary *existingDict = (NSMutableDictionary *)sessionDict;
		
			//NSLog(@"existingDict: %@", existingDict);
	
			NSMutableArray *array = [[NSMutableArray alloc] init];
		
			NSMutableArray *holdingArray = [[NSMutableArray alloc] init];
		
			for (int i = 0; i < [sessionDict count]; i++) {
			
				existingDict = [sessionDict objectAtIndex:i];
			
				if (![[existingDict valueForKey:@"name"] isEqualToString:@""]) {
				
					[array addObject:[existingDict valueForKey:@"name"]];
				
					[holdingArray addObject:existingDict];
				}
			
			}
		
			NSMutableDictionary *newDict = [[NSMutableDictionary dictionaryWithObjects:
										 [NSArray arrayWithObjects:[NSArray arrayWithObjects:nil, nil], [NSArray arrayWithObjects:nil, nil], @"", [[NSUserDefaults standardUserDefaults] valueForKey:@"DefaultTimerValue"], myTextField.text, nil] forKeys:
										 [NSArray arrayWithObjects:@"images",@"moods",@"notes",@"timer", @"name", nil]] retain];
		

			[holdingArray addObject:newDict];
		
			//NSLog(@"holdingArray: %@", holdingArray);
		
			[holdingArray writeToFile:pathToUserCopyOfPlist atomically:YES];
		
			[holdingArray release];
			[array release];
			[existingDict release];
			[newDict release];
Remember, adding to the property list is simply adding a new object to the array. When modifying, you'll need to find the index of the object first, then set its 'valueForKey'.

Hope this helps.

Good luck!
- Gary
garyshaf is offline   Reply With Quote
Reply

Bookmarks

Tags
edit plist, plist

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 359
9 members and 350 guests
akphyo, blueorb, fredidf, iAppDeveloper, iGamesDev, Kirkout, MarkC, mottdog, Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:27 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0