0

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

asked Apr 18, 2012 at 18:23
13
  • How do you declare the authormutablearray property Commented Apr 18, 2012 at 18:28
  • It's regular synthesized property. Commented Apr 18, 2012 at 18:30
  • @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? Commented Apr 18, 2012 at 18:31
  • I'll update the OP, it's strong. Commented Apr 18, 2012 at 18:33
  • In your editingStyleForRowAtIndexPath method, check to see if addModel is not nil, too. Commented Apr 18, 2012 at 18:39

3 Answers 3

5

Shouldn't the init method return self ?

answered Apr 18, 2012 at 18:55

1 Comment

Yes, it did. I have 4 NSString properties I was creating in the same init method, I just excluded them for this example. They all worked fine. Why is it the array was the only one not to keep its data?
0

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
answered Apr 18, 2012 at 18:54

6 Comments

strong is the equivalent to retain under ARC. They have equivalent semantics in their respective memory management systems. It's also unecessary to declare the ivar separately; it's created by @synthesize.
@lulius, which would you use for the array, strong or retain?
copy, actually, for a class that has a mutable counterpart, but the choice between strong and retain simply depends on whether you're using ARC or not.
Strong for ARC, retain otherwise? And what do you mean by mutable counterpart? I just want to make sure I completely understand.
@WDy Yes, strong under ARC. Mutable counterpart meaning NSArray/NSMutableArray, NSString/NSMutableString, NSIndexSet/NSMutableIndexSet, and so on. If a mutable instance is passed in to your setter, it can be changed by the original owner unless you take a copy, which may have surprising results. There are SO questions around that go into more depth about that.
|
0

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).

answered Apr 18, 2012 at 19:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.