开发者

Problem while storing CGRect into plist

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:[NSNumb开发者_如何转开发er 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.


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]);


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜