Monday, December 23, 2013

Started novice ask questions.

The basic operation is to UIPickerVIew
data, copy the code according to the book binding. H file
{
NSArray * pickerData; / / line: 1
}
@ Property (nonatomic, retain) NSArray * pickerData; / / line2

. M file
/ / Beginning
@ Synthesize pickerData;

/ / Load event
NSArray * array = [[NSArray alloc] initWithObjects: @ "Luke", @ "Leia", @ "Han", @ "Chewbacca", @ "Artoo", @ "Threepio", @ "Lando", nil]; < br />self.pickerData = array; / / line3
[Array release]; / / line4

- (NSInteger) pickerView: (UIPickerView *) pickerView numberOfRowsInComponent: (NSInteger) component {

return [pickerData count]; / / line5
}

Question:
1. Line1 code seems useless, right? Deleted as you can run.
. 2 line3 code if directly into pickerData = array; in line5 will go wrong. And if commented line4, you can,
Description pickerData = array; pickerData should be directly referenced array address?
But pickerData = array; and self.pickerData = array; did not understand what is the difference in the end.
Reply:
1. Line1 in principle to declare the variable is
2. Self.pickerDataj is a method of execution, but pickerData = just assign variables.

self.xxx =, so to actually execute:

- (Void) setPickerDataj: (NSArray *) c {
[C retain];
[PickerData release];
pickerData = c;
}


It has a retain and release operation, it can retain the value operate outside the immediate release of the variable, it will assign it after retain. Guarantee that it will not go wrong.

For the above example:

self.pickerData = array; / / line3 array is a non-basic types, which means that it is actually a pointer assignment assignment
[Array release]; / / line4 Here you have the array is released, so the array into a nil pointer

return [pickerData count]; / / line5 and pickerData and points to the array pointer, so there will be a mistake.

If you comment out the line 4 line, you can use pickerData = array, as to why you think it out of it.
Reply:
cited a floor taito reply:. on
1 line1 to affirm the principle of variable is
2. Self.pickerDataj is a method of execution, but pickerData = just assign variables.

self.xxx =, so to actually execute:

- (Void) setPickerDataj: (NSArray *) c {
[C retain];
[PickerData release];
pickerData = c;
}
......

In fact, this is performed
- (Void) setPickerDataj: (NSArray *) c {
if (c! = pickerData) {
[PickerData release];
pickerData = [c retain];
}
}
Reply:
1 can not declare. If line1 and line2 at two different sound when you need to have.
(2) Because line2 at the way you're retain the definition of property, it will
 - (void) setPickerDataj: (NSArray *) c {
if (c! = pickerData) {
[PickerData release];
pickerData = [c retain];
}
}

Such execution line3 sentence. If your reputation is to assign it the same as going wrong.
Here is a reference count of the concept, see: http://www .cocoachina.com/macdev/cocoa/2010/1015/2194.html
Get to know this article, your memory management is almost OK.
Reply:
Thank you
Reply:
Top of the answer proposed landlord look very good object-c memory management Ha

No comments:

Post a Comment