views:

35

answers:

2
+1  Q: 

NSArray assignment

Hi Guys -

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

+2  A: 

The keyword should be @synthesize not @synthesis

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

darren
`@synthesize array = _array;` should be fine. It directs the compiler to use the instance variable `_array` for the property `array` in the accessors.
outis
Are you sure about this: @synthesize array = _array; ? I have never seen syntax like that on a @synthesize macro before. Interesting.
darren
It was type mistake, it's actually @synthesize
iamiPhone
@darren: yup, I'm sure: http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/ObjectiveC/Articles/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW11
outis
+2  A: 

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;
outis