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 01-18-2011, 05:42 PM   #1 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 3
jasondavis984 is on a distinguished road
Default Help needed with Plist,

ok so i have a Plist in its SAVING fine... my problem is on a different UI i want to save DIFFERENT information... so on my first UI, a player enters his name and it saves perfect... next on a different UI i want the player to enter a value (number 0-100) and SAVE to the SAME plist... and i will use this later in the game as that players Stat tracking... but on the second UI it dosent let me save ... and the error is
-01-18 17:28:35.528 Game [7634:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (1)'

Code:
-(NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:k1Filename];
}
#pragma mark -
-(void)viewDidLoad{

NSString *filePath= [self dataFilePath];
NSLog(@"%@", filePath);
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
NSLog(@"File exist inserting coins HOPEFULLY");
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
child1.text = [array objectAtIndex:0];
coin1.text = [array objectAtIndex:1];// Crashes after this line of code
[array writeToFile:[self dataFilePath] atomically:YES];

//[array release];
}

else {
NSLog(@"Inside the loop4 saving coins");
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:child1.text];
[array writeToFile:[self dataFilePath] atomically:YES];
[array release];
}


UIApplication *app= [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate
name:UIApplicationWillTerminateNotification
object:app];
NSLog(@"this loads to the kids COINS");


}


-(IBAction)goPayoutid)sender
{
PayoutVC *splashMenu = [[PayoutVC alloc] initWithNibName:@"PayoutVC" bundle:nil];


NSLog(@"Does it get THIS far????? should add coin1 to plist");
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:child1.text];
[array writeToFile:[self dataFilePath] atomically:YES];
[array release];


[pKidDashboardVC.view removeFromSuperview];
[self.view addSubview:splashMenu.view];

}

As u can see its pretty standard plist, when i use this code on other UI i can PULL that child1.text FINE and save it Back to the Plist, but when i try and save the coin1.text it wont let me create a new object in the plist TOO save it... ANY help is wanted!!! thanks again

ANY information/Help would GREATLY be THANKED!!!!! Thanks again for your time!

Last edited by jasondavis984; 01-18-2011 at 05:45 PM.
jasondavis984 is offline   Reply With Quote
Old 01-18-2011, 07:07 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Your array only has one item, at index 0, so it crashes when you try to access index 1. Where do you add coin1.text to the array?
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-19-2011, 10:51 AM   #3 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 3
jasondavis984 is on a distinguished road
Default

in the ELSE statement right beneath it... i commented it out for visual reason... NSString *filePath= [self dataFilePath];
NSLog(@"%@", filePath);
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
NSLog(@"File exist inserting coins HOPEFULLY");
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
child1.text = [array objectAtIndex:0];
coin1.text = [array objectAtIndex:1];// Crashes after this line of code
[array writeToFile:[self dataFilePath] atomically:YES];

//[array release];
}

else {
NSLog(@"Inside the loop4 saving coins");
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:child1.text];
///// HERE is where i TRY and save the NEW coin1.txt
[array1 addObject:coin1.text];


[array writeToFile:[self dataFilePath] atomically:YES];
[array release];
}


But its NOT getting past the object at index: 1 and crashing.... Any way of solving this? Is my problem that i Created the Plist on a different UI and cant ADD objects to the list on a different UI, or is my array set up wrong... cuz the array is SUPPOSED to read from the pList first, then if NO entry is found the ELSE is supposed to add the coin1 as the next entry.... THANKS again for ur input!!!!
jasondavis984 is offline   Reply With Quote
Old 01-19-2011, 11:44 AM   #4 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

The file exists so you enter the "file exists" code, but it only has one item so you crash. Your new code looks likes it should work now though; try deleting the file or changing your filename.

Your original code has three places where you call [array writeToFile:] ; make sure you fixed them all to write two items. Actually it looks like the first [array writeToFile:] serves no purpose; it writes the same array you just read back to the file.

Also use [code] [ /code] tags on the board for readability.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-19-2011, 02:21 PM   #5 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 3
jasondavis984 is on a distinguished road
Default

OK after initial build it saves fine the child1.text the same... working fine, but im STILL having errors trying to save the coin1.text
2011-01-19 14:15:56.659 Eya[10145:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 1'

Any idea what im doing wrong? i have the same code as above. Is the error because im not entering coin1.text into the pList in a different UI? The way its set up now i can use this SAME block of code on OTHER UI's to output the child1.text to Multiple labels on different UI's... All i want now is on a seperate UI just save coin.text as the second value in the pList.... THANKS for your answers so far!
jasondavis984 is offline   Reply With Quote
Old 01-19-2011, 05:00 PM   #6 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
'[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 1'
It sounds like you're trying to insert "nil" into the array instead of inserting a real object. Look at the line causing this error and figure out why the object you are adding is nil. If you can't, post the whole method.
__________________

Free Games!
smasher is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone & ipod touch apps, plist saving, saving errors

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: 364
12 members and 352 guests
7twenty7, blueorb, dre, iAppDeveloper, jeroenkeij, Mah6447, Morrisone, mottdog, sacha1996, Sami Gh, stanny, toon4413
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:40 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0