0
votes

@property (nonatomic, strong) NSMutableArray *authorMutableArray;

- (id)init {
    self = [super init];
    if (self) {

        self.authorMutableArray = [[NSMutableArray alloc] initWithObjects:@"First Row", @"Second Row", nil];

        for (NSString *string in authorMutableArray) {
            NSLog(@"String: %@", string);
        }

        NSLog(@"Init in Add Model with Author count:%i", [authorMutableArray count]);


    }
}

An example of accessing the property. The NSLog always shows the count as 0.

    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) { 
        if (indexPath.row == [self.addModel.authorMutableArray count] - 1 ) {
            NSLog(@"count of %i", [self.addModel.authorMutableArray count]);
            return  UITableViewCellEditingStyleInsert;
        }

        else {
            return  UITableViewCellEditingStyleDelete;

        }
    }

    else {
        return UITableViewCellEditingStyleNone;
    }
}

The array I'm creating in init is not keeping its values past this method. Any reason why? The for loop will show both objects in the array. If I try to ask this after the init method is called, the array is empty.

Updated: Thank you everyone for your time and eyes. I had forgotten to return self in the init method.

3
How do you declare the authormutablearray property - Warren Burton
It's regular synthesized property. - W Dyson
@WDyson What Warren means is, what specifiers are you using? strong, weak, assign? Also, how are you accessing the array afterwards? Can you show us the code for that? - Itai Ferber
I'll update the OP, it's strong. - W Dyson
In your editingStyleForRowAtIndexPath method, check to see if addModel is not nil, too. - Michael Dautermann

3 Answers

5
votes

Shouldn't the init method return self ?

0
votes

In your class' interface file(.h file) declare like this:

@interface Your_Class_Name : UIViewController {
   NSMutableArray *authorMutableArray;

}

@property (nonatomic, strong) NSMutableArray *authorMutableArray;
//i dont know why you prefered strong, chose retain and try again please
0
votes

We aren't dealing with C++ or Java here, the -init method of an object MUST return a value.

This quirk actually allows for some pretty interesting stuff, e.x. the following:

-(id) init {
    NSLog(@"Creating new instance of singleton object...");

    #if __has_feature(objc-arc)
    self = singleton_instance;
    #else
    [self release];
    self = [singleton_instance retain];
    #endif   

    return self;
}

It also allows for class 'posing' of a sort, allowing you to track exactly when an object of a particular class is initialized (that is too deep of a topic for this answer).