Concept
NSLog () can use the%@ format specifier to output an object. When NSLog () processes the%@ specifier, it asks the corresponding object in the argument list to get a description of the object. Technically, NSLog () sends a description message to the object, and then the description method of the object generates a NSString and returns it. NSLog () will include this string in the output result. The description method is provided in the class to customize how the NSLog () outputs the object.
In the custom description method, you can choose to return a literal nsstring, such as @ "Gkejlkgjlkghkehgl", or you can construct a string that describes the various types of information about the object. In cocoa, the Nsarray class manages a collection of objects, and its description method provides information about the array itself, such as the number of objects in the array and the description that each object contains. Of course, the description of an object is obtained by sending an description message to an object in the array separately.
Note:%@ just invokes the description method for each object and displays the result.
Example code
. h file:
#import "Person.h"@interface Test : Person@end
. m File:
#import "Test.h"@implementation Test- (NSString *)description { return (@"1234567890");}@end
In the main () function:
new@"%@", _oneTest );
Output Result:
Objective-c----NSLog ()