Game Development Reference
In-Depth Information
Listing 14-3 . Logging objects (Objective-C class instances)
NSString* helloLog = @"Hello Log";
NSLog(@"string=%@ self=%@ cmd=%@", helloLog, self,
NSStringFromSelector(_cmd));
The %@ format specifier can be used for any pointer to an Objective-C class instance. The
%@ format specifier actually sends the description message to the object, which then re-
turns a description string you can customize by implementing the description method. An
example is shown in Listing 14-4 .
Listing 14-4 . A custom description method
-(NSString*) description
{
return [NSString stringWithFormat:
@"%@ selfPointer=%p selfClass=%@",
[super description], self,
NSStringFromClass([self class])];
}
So instead of formatting the same information repeatedly for multiple NSLog statements,
it's preferable to override the description method to return a custom description string that
contains all necessary information about the object. In this case, it also prints out the
pointer value (the memory address of the object) with the %p specifier and the name of the
class, in addition to calling [super description] , which returns the super class'
description string.
The resulting output string of Listing 14-3 might look like this:
string=Hello Log self=<GameScene = 0x10a52bb00 | Name = >
selfPointer=0x10a52bb00 selfClass=GameScene
cmd=didLoadFromCCB
Last, there are struct types like CGPoint , CGSize , and CGRect . You can log them
by printing each struct field individually, or for the aforementioned structs you can use
the conversion methods NSStringFromCGPoint , NSStringFromCGSize , and
NSStringFromCGRect . Listing 14-5 shows an example use.
Search WWH ::




Custom Search