开发者

NSArray assignment

开发者 https://www.devze.com 2022-12-16 16:11 出处:网络
I have two viewControllers and one has to pass an array to another, but on receiving side i am always getting count 0. here is what i did

I have two viewControllers and one has to pass an array to another, but on receiving side i am always getting count 0. here is what i did

classA.h

Class A : UIViewController
{
@private 
    NSMutableArray *_array;
}

@property (nonatomic, retain ) NSMutableArray *array;
@end

classA.m

@implementation

@synthesis array =_array;

-(void) nowShow
{
    int objCount = [ _array count ];   // This is always coming as 0 though i tried various ways (listed below )
}
@end

classB.m

-(开发者_如何转开发void) method:(id)sender {
    NSMutableArray *msgArray = [[NSMutableArray alloc] initWithCapacity:1];

    for ( int i = 0 ; i <objCount; i++ ){
        unsigned int idMsg = msgId[i];
        [msgArray addObject:[NSNumber numberWithUnsignedInt:idMsg]];
    }

    classA *classAController = [[classA alloc] initWithStyle:UITableViewStylePlain];    

    //[ classAController.array arrayWithObject
    //[classAController.array addObjectsFromArray:msgArray];
    [ classAController.array initWithArray:msgArray];
    //[classAController.array setArray:moveArray];
    [self presentModalViewController:classAController animated:YES];
}

Any suggestion guys


You shouldn't call any init method unless it's immediately after calling alloc.

With properties, all you need to use is assignment:

classAController.array = msgArray;


The keyword should be @synthesize not @synthesis

edit: also you want to synthesize the property "array", not the instance variable _array

0

精彩评论

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