@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 Answers 3
Shouldn't the init method return self ?
1 Comment
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
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
.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
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.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).
Comments
Explore related questions
See similar questions with these tags.
strong
,weak
,assign
? Also, how are you accessing the array afterwards? Can you show us the code for that?editingStyleForRowAtIndexPath
method, check to see ifaddModel
is not nil, too.