Question

I am trying to save an array which contains CGRect values. I know plists are not compatible with the CGRect type, so I slightly modified my code and now I am storing NSNumber instead of storing the rect value -- I split the rect value into four values: x, y, width, height.

[myArray1 addObject:[NSNumber numberWithFloat:CGRectGetMinX(rawRect)]];
[myArray1 addObject:[NSNumber numberWithFloat:CGRectGetMinY(rawRect)]];
[myArray1 addObject:[NSNumber numberWithFloat:CGRectGetWidth(rawRect)]];
[myArray1 addObject:[NSNumber numberWithFloat:CGRectGetHeight(rawRect)]];

Now myArray1 contains only NSNumbers and I am trying to store these values into a plist, but I can't loading the values back. Can any one correct me if I am doing anything wrong with my code?

Thanks in advance; I'm waiting for your valuable information.

Was it helpful?

Solution

CGRect rect  = CGRectMake(0, 0, 320.0f, 480.0f);
//you need to translate the rect into a compatible "Plist" object such as NSString
//luckily, there is a method for that

[rectArray addObject:NSStringFromRect(rect)];

//save into a plist


...

on retrieval of this value

CGRect rect = CGRectFromString([rectArray objectAtIndex:0]);

OTHER TIPS

The code you have presented looks correct. Your issue is likely elsewhere. It might be in the loading code, or somewhere else in the saving code, but you haven't posted anything else, so I can't be sure of where the problem lies.

You can try johnoodles approach if you like for simplicity's sake, but that will likely create a larger plist file and will also take longer to store and retrieve. If you're storing a relatively small number of rects, it probably not a problem, but if you're storing a lot, you might notice a delay that would be much shorter if storing as actual numbers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top