Friday, January 10, 2014

Chapter VI ios5 based tutorial examples doubt

Great God who is good:
In "ios5 based tutorial" This book, Chapter VI is about multi-view, in which case, do not quite understand:
One of the root view controller class BIDSwitchViewController defines two attributes,
@ Property (strong, nonatomic) BIDYellowViewController * yellowViewController;
@ Property (strong, nonatomic) BIDBlueViewController * blueViewController;
BIDYellowViewController and BIDBlueViewController inherit from UIViewController,
in BIDSwitchViewController of- (IBAction) switchViews: (id) sender function, there is a line of code, if (self.yellowViewController.view.superview == nil), for the first time to enter this function,
yellowViewController not allocate space to initialize the pointer, that was a wild pointer, in this case, direct access to its view property does not have a problem, do not know if you know what I mean yet?
Thank you.
Reply:
You can print under the conditions of this object, the return should be (null)
Here the object to determine the conditions for sending getter method, because the object is not initialized, so it is nil his getter method returns.
Reply:
Since it is nil, so that there should be a problem to write self.yellowViewController.view fishes ah
Reply:
reference to the second floor heiniuyang reply:
that since it is nil, and that this problem should write self.yellowViewController.view fishes ah


In oc in, nil is equivalent to c, c + + in the NULL pointer, the general release of the object after the object is set to nil is very important so that you can prevent illegal pointer references cause the program to crash out.

 [obj release] ;/ / this time obj reference count is 0 
[Obj someMethod] ;/ / crashes

[Obj release];
obj = nil;
[Obj someMethod] ;/ / obj this case is nil, but it will not crash

In objective-c, we only use nil to check the program logic, not used to determine whether an object is empty to avoid collapse. This c + + is different. objective-c runtime automatically handles sending messages (call methods) to nil object does not complain crash out.
Reply:
So for example
@ Property (strong, nonatomic) BIDYellowViewController * yellowViewController;
@ Property (strong, nonatomic) BIDBlueViewController * blueViewController;
Such a statement two properties, and when yellowViewController pointer does not allocate memory, that does not point to an object when,
yellowViewController.view.superview, access the member variables that will be what kind of situation yet.
Reply:
references, 4th Floor heiniuyang reply:
then such
@ Property (strong, nonatomic) BIDYellowViewController * yellowViewController;
@ Property (strong, nonatomic) BIDBlueViewController * blueViewController;
Such a statement two properties, and when yellowViewController pointer does not allocate memory, that does not point to an object when,
yellowViewController.view.superview, access the member variables that will be what kind of situation yet.


This is because the compiler ios defined variables in dealing with unused time will be a default value according to its type setting. If a value type, the default value is 0 reference type default is nil. You can write code to test. NSInteger define a variable, and then access it and print its value. You will find the printed value is 0, the same for the pointer to the object, print out the value is null. This is not an error when you visit yellowViewController.view.superview reasons. Remember these words, "send a message to nil, the program does not lead to an exception"


Reply:
references, 4th Floor heiniuyang reply:
then such
@ Property (strong, nonatomic) BIDYellowViewController * yellowViewController;
@ Property (strong, nonatomic) BIDBlueViewController * blueViewController;
Such a statement two properties, and when yellowViewController pointer does not allocate memory, that does not point to an object when,
yellowViewController.view.superview, access the member variables that will be what kind of situation yet.


Can call its member variables. But the return value is NULL.

No comments:

Post a Comment