views:

2441

answers:

5

Saving data:

- (NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename];
}
- (void)applicationWillTerminate:(NSNotification *)notification
{
NSMutableArray *array = [[NSMutableArray alloc] init];

NSNumber *number = [NSNumber numberWithInt:points];
  [array addObject:number];

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

Reading data: BestLabel.text will display the high score. I get a warning: "assignment makes integer from pointer without a cast"

-(void)LoadData{
BestLabel.center = CGPointMake(150,300);

NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
 NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
 loadint = [array objectAtIndex:0];
 NSString *myString35 = [NSString stringWithFormat:@"%d", loadint]; 
 BestLabel.text = myString35;
 [array release];
}
}

The sample code I got this from was saving and loading text fields with no problem.. I suppose I could do a string to int conversion, but that seems unnecessary. Thanks for your help!

EDIT:

- (void)applicationWillTerminate:(NSNotification *)notification

{ NSMutableArray *array = [[NSMutableArray alloc] init];

if (points > highscore){

 NSString *myString35 = [NSString stringWithFormat:@"%d", points];
 [array addObject:myString35];
}else {
 NSString *myString35 = [NSString stringWithFormat:@"%d", highscore];
 [array addObject:myString35];
}


NSNumber *number = [NSNumber numberWithInt:GreenBlot.center.x];
[array addObject:number];
NSNumber *number2 = [NSNumber numberWithInt:GreenBlot.center.y];
[array addObject:number2];

And now loading with:

NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
 NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
 BestLabel.text = [array objectAtIndex:0];
 highscore = [[array objectAtIndex:0] intValue];


 lx = [[array objectAtIndex:1] intValue];
 ly = [[array objectAtIndex:2] intValue];
 GreenBlot.center = CGPointMake( lx,ly);


 [array release];
}

lx and ly are integers. I tried CGPointMake( [[array objectAtIndex:1] intValue], [[array objectAtIndex:2] intValue] ) first. Same results, GreenBlot is getting centered at 0,0. Any idea why?

+1  A: 

The easiest thing to do is use NSNumber's intValue method:

loadint = [[array objectAtIndex:0] intValue];
e.James
Trying this.. see main post. Attempting to set a CGPointMake with the intValues is only giving me 0,0.
1. What type of variable is returned by GreenBlot.center.x?2. What type of variable are lx and ly?
e.James
A: 

You need the second line below:

loadint = [array objectAtIndex:0];
int score = [loadint intValue];
NSString *myString35 = [NSString stringWithFormat:@"%d", loadint];

If you are only saving one value, then use a NSDictionary with a key that makes it easier for you to label the value you are saving.

mahboudz
minor correction: [loadint integerValue] returns an `NSInteger`, which (depending on the system) may or may not be an `int`. You'd either want to change the method call to `intValue` or change the type of `score` to `NSInteger`.
Dave DeLong
thanks. Changed.
mahboudz
A: 

CGPointMake is defined:

CGPoint CGPointMake(
    float x,
    float y
)

You are taking GreenBlot.center.x which is presumably a float and feeding it to NSNumber as an int.... Go back and look at the types of your variables and change to [NSNumber numberWithFloat:...] and lx = [[array objectAtIndex:1] floatValue]; etc., as appropriate.

Also, you've created a different question in your editing of the original question.

mahboudz
A: 

OP here, (other computer's net is down, dont remember the open id used). Mahboudz: Thanks, I was trying to use integers with floats. Another problem has arisen.. if I quit my application with the home button, trying to load the data returns 0.. but if I rapidly tap the home button, the data will save correctly - I know because the next time I run the application, the GreenBlot.center is set appropriately. Any idea on why this is? Tried putting my saving code in the start of the dealloc method, but that never worked.

A: 

Please tell me that i am saving only one integer value into Plist.I have a counter in a function that increments when a function calls.so i want to save that value in plist and increment that value again and again in Plist.

so Please Help.

IHSAN KHAN