test.h
1, @ interface test: UITableViewController {
2, NSArray * documentArray;}
3, @ property (retain, nonatomic) NSArray * documentArray;
4, @ end
test.m
@ Implementation test
@ Synthesize documentArray;
6, - (id) initWithNibName: (NSString *) nibNameOrNil bundle: (NSBundle *) nibBundleOrNil {
7, self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil];
8, if (self! = nil) {
9, documentArray = [[NSArray alloc] initWithObjects: @ "aa", @ "bb", @ "cc", nil];
10, return self;}
11, .....
I am in the first three lines of code in the property uses retain, theoretically speaking, documentArray the retainCount has an up, so the first nine lines of code can be written:
documentArray = [NSArray arrayWithObjects: @ "aa", @ "bb", @ "cc", nil];
But sometimes write, program errors, and sometimes can not go wrong tangle ...
Reply:
retain, is the description of the property at the time the assignment, the value before the first release, and then assign a new value to the property, plus a reference.
Actually or 1. First assigned to it no problem, assign to it a second time have to be careful.
Reply:
self.documentArray = [NSArray arrayWithObjects: @ "aa", @ "bb", @ "cc", nil];
Reply:
The heroes mean I basically understand, but want to confirm: For this post, in the. M file we should
With self.documentArray = [NSArray arrayWithObjects: @ "aa", @ "bb", @ "cc", nil];
Or self.documentArray = [[NSArray alloc] initWithObjects: @ "aa", @ "bb", @ "cc", nil];
2, asked what sometimes appears with an error it, I guarantee documentArray assigned only once (because of mistakes that practice, I just need to assign an unchanging set of array values to use in the following UITableView in , so I'm sure)
Reply:
NSLog (@ "documentArray retainCount:% d", [documentArray retainCount]);
self.documentArray = [NSArray arrayWithObjects: @ "aa", @ "bb", @ "cc", nil];
NSLog (@ "documentArray retainCount:% d", [documentArray retainCount]);
NSLog (@ "documentArray retainCount:% d", [documentArray retainCount]);
self.documentArray = [[NSArray alloc] initWithObjects: @ "aa", @ "bb", @ "cc", nil];
NSLog (@ "documentArray retainCount:% d", [documentArray retainCount];
Results in two ways before the assignment, respectively, after the same (assignment before retainCount is 0, after 1)
Why is it wrong sometimes, tangled ...
Reply:
http://stackoverflow.com/questions/4636146/when-to-use-retaincount
Reply:
In fact, the use of the property, or the problem, and only when it is used self.documentArray = attribute, directly documentArray = is the assignment useless to the property.
self.documentArray = [NSArray arrayWithObjects: @ "aa", @ "bb", @ "cc", nil]; / / attribute to retain it, you need to release a second.
self.documentArray = [[NSArray alloc] initWithObjects: @ "aa", @ "bb", @ "cc", nil]; / / attribute to retain a bit, alloc have it, you need to release two times.
documentArray = [NSArray arrayWithObjects: @ "aa", @ "bb", @ "cc", nil]; / / do not retain and alloc, will be back with abnormalities.
documentArray = [[NSArray alloc] initWithObjects: @ "aa", @ "bb", @ "cc", nil] ;/ / alloc it, you need to release a second.
Actually, this means that your property (property) is not deep enough to understand. You can look at Apple this document, as well as some of the other online information.
https://developer.apple.com/library/mac/ # documentation / Cocoa / Conceptual / ObjectiveC / Chapters / ocProperties.html
Reply:
Novices usually encounter this problem, in fact, summed up or memory problems, even if you set the property, and then only when using the self to take effect. Do not put objects declared interface, and the use of property in the object retain equated, they are in fact has little to do. You can put the second line 2, NSArray * documentArray; removed, and then use the self.documentArray = [NSArray arrayWithObjects: @ "aa", @ "bb", @ "cc", nil]; so foolproof, or The third line @ property (retain, nonatomic) NSArray * documentArray; remove with documentArray = [NSArray arrayWithObjects: @ "aa", @ "bb", @ "cc", nil]; Or documentArray = [[NSArray alloc] initWithObjects: @ "aa", @ "bb", @ "cc", nil]; all OK.
Personal recommendations, unless the object to be used in another class, or do not set a property, for internal use, with the member variable enough, no need to use properties.
Reply:
Thank you, xiaowei4895 and Linux_fay two up.
No comments:
Post a Comment