I've been playing around with one of Erica Sadun's examples (the persistence stuff).
In her example, she stores off the position of 16 randomly created ImageViews (which works fine) by doing;
I wanted to modify this so that I could also save off the background color of the image views, so I added this;
then in the for{} loop;
and then after the for{}loop;
Now the weird thing is, the "colors" array is populated correctly by the for{} loop but if I try to read the objects back;
it comes out with garbage but reading the "locations" back;
works as expected.
I've tried all sorts of things;
casting the colors as strings
getting the CGColor from dv.backgroundColor (in the for {} loop)
casting the CGColor as a string
setting all the colors to constants (in the for{} loop) i.e. [colors addObject:[UIColor redColor]];
but nothing works. When the object is read back from the user defaults, it reads back the right number of objects (16 in this case) but they're all set to the same number.
Can anyone shed some light on it or suggest a simpler way to do what I'm trying to do? I've spent a whole day on it and I'm no closer to a solution.
Thanks
In her example, she stores off the position of 16 randomly created ImageViews (which works fine) by doing;
Code:
NSMutableArray *locations = [[NSMutableArray alloc] init];
for (DragView *dv in [contentView subviews])
{
[locations addObject:NSStringFromCGRect([dv rect])];
}
[[NSUserDefaults standardUserDefaults] setObject:locations forKey:@"locations"];
[[NSUserDefaults standardUserDefaults] synchronize];
[locations release];
Code:
NSMutableArray *colors = [[NSMutableArray alloc] init];
Code:
[colors addObject:[dv backgroundColor]];
Code:
[[NSUserDefaults standardUserDefaults] setObject:colors forKey:@"colors"];
Code:
colors = [[NSUserDefaults standardUserDefaults] objectForKey:@"colors"];
Code:
locs = [[NSUserDefaults standardUserDefaults] objectForKey:@"locationss"];
I've tried all sorts of things;
casting the colors as strings
getting the CGColor from dv.backgroundColor (in the for {} loop)
casting the CGColor as a string
setting all the colors to constants (in the for{} loop) i.e. [colors addObject:[UIColor redColor]];
but nothing works. When the object is read back from the user defaults, it reads back the right number of objects (16 in this case) but they're all set to the same number.
Can anyone shed some light on it or suggest a simpler way to do what I'm trying to do? I've spent a whole day on it and I'm no closer to a solution.
Thanks