Special attention:
Do not use both%@ and self in the description method, use both%@ and self to represent the description method to invoke self, and eventually cause the program to go into a dead loop and call the description method in a loop
1.NSLog Review
As we all know, we can use the NSLog function to output strings and some basic data classes
1 int age = 11;
2 NSLog (@ "Age was%d", age);
* The 2nd line of%d will output an integer data, and the right variable age will be output in place of%d
* Output Result:
Age is 11
2.NSLog Output OC Object
In fact, in addition to the output of the basic data type, the NSLog function can also output any OC object
1 Student *stu = [[Student alloc] initwithage:10];
2
3 NSLog (@ "%@", Stu);
4
5 [Stu release];
* In line 3rd with the NSLog function output Stu object, note the left of the format character%@, later want to output OC object, you have to use%@ this format character
* Once the NSLog function is found to output an OC object with%@, the object's description method is called (The return value of this method is the NSString type, the string type in OC), and the string returned by the description method is replaced by the%@ The location of the output
* The output of the above code is:0x100109910>
Student is the class name, 0x100109910 is the memory address of the object
Example: (When the description method in OC is not overridden) the default implementation of the description method is to return the format:< class name: The memory address of the object >
* Note that%@ can only be used for output OC objects and cannot output other types such as struct body
* People with Java development experience should be able to feel that the description method in OC is the ToString method in Java
3. Overriding the description method
The default implementation of the description method is to return the class name and the memory address of the object, so that using NSLog to output the OC object is not very significant because we do not care about the memory address of the object, but rather the value of some variables inside the object. Therefore, the description method is often overridden to override the default implementation of the Description method
For example, overriding the student description method, returning the value of the member variable _age
1-(NSString *) Description {
2 return [NSString stringwithformat:@ "age=%i", _age];
3}
* In line 2nd, a static method called the NSString class is invoked stringWithFormat initializes a string object and returns the string
* If you're going to use NSLog, then you should be able to understand what the 2nd line of method parameters means.
* If _age is 10, then the string returned by the description method is @ "age=10"
* Some people may find it strange that the student object created before is needed to be freed, why is the string object created here not freed? To fully understand this problem, we need to understand the memory management of OC, here we do not do a detailed discussion, there will be chapters in detail to discuss memory management. You can first remember a rule: Normally, the object returned by the static method is not released manually.
* After rewriting the description method, execute the following code again
Student *stu = [[Student alloc] initwithage:10];
2
3 NSLog (@ "%@", Stu);
4
5 [Stu release];
1 output results are:
age=10
4.description trap of the method
Do not use%@ and self in the description method at the same time, the following notation is wrong:
-(NSString *) Description {
2 return [NSString stringwithformat:@ "%@", self];
3}
1 The 2nd line uses both%@ and self, which represents the description method to invoke self, and therefore eventually causes the program to fall into a dead loop, calling the description method
Under what circumstances do you want to rewrite the description method in Objective-c