How do I set an array in one class with another array in another class
I've populated and array with data like this in one class...
PowerClass.h
NSMutableArray pickerArray;开发者_运维百科
@property (nonatomic, retain) NSMutableArray pickerArray;
-
PowerClass.m
@synthesize pickerArray;
@implementation
NSMutableArray *array = [[NSArray alloc] initWithObjects:@"stef", @"steve", @"baddamans", @"jonny", nil];
pickerArray = [NSMutableArray arrayWithArray:array];
And I'm trying to set the Array in another class
WeekClass.h
PowerClass *powerClass;
NSMutableArray *pickerData;
@property (nonatomic, retain) NSMutableArray pickerData;
@property (nonatomic, retain) PowerClass *powerClass;
WeekClass.m
@implementation
pickerData = [NSMutableArray arrayWithArray:powerClass.pickerArray];
I have no errors or warnings. It just crashes. The NSLog says that the powerClass.pickerArray is NULL.
Please help point me in the right direction.
Memory management!
You've set pickerArray = [NSMutableArray arrayWithArray:array];
, which is an autoreleased object. By the time you ask for pickerArray later, it's disappeared!
The solution is to use the @synthesize
d accessors. Instead of:
pickerArray = [NSMutableArray arrayWithArray:array];
... use one of the following:
[self setPickerArray:[NSMutableArray arrayWithArray:array]];
self.pickerArray = [NSMutableArray arrayWithArray:array];
//These two are exactly equivalent, but are both very different from what you have now.
This way, your property will handle memory management for you.
精彩评论