开发者

How do I set an array in one class with another array in another class

开发者 https://www.devze.com 2022-12-25 19:57 出处:网络
I\'ve populated and array with data like this in one class... PowerClass.h NSMutableArray pickerArray;开发者_运维百科

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 @synthesized 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.

0

精彩评论

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

关注公众号