Mad*_*han 4 printing types objective-c
我想打印所有类型的值,如char,long ...等等,还有nsdate,nsdictionary,frame ....我现在要打印每个类型变量的值.
Ste*_*vin 10
原语类型,如整数,浮点,双,等可以在它们被打印在C相同的方式进行打印,使用printf,fprintf等等.如果需要打印的一类经常可以使用数据NSObject的方法(NSString *)description,以得到一个NSString代表对象的数据.这是一个例子......
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *string = [NSString stringWithFormat:@"Hello World!"];
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
NSArray *array = [NSArray arrayWithObject:@"Hello There!"];
char *c_string = "Familiar ol' c string!";
int number = 3;
printf("C String: %s\n",c_string);
printf("Int number: %u\n", number);
//In 10.5+ do not use [NSString cString] as it has been deprecated
printf("NSString: %s\n", [string UTF8String]);
printf("NSDate: %s\n", [date.description UTF8String]);
printf("NSArray: %s\n", [array.description UTF8String]);
//If you are using this information for debugging, it's often useful to pass the object to NSLOG()
NSLog(@"NSArray *array = \n%@", array);
[pool drain];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编辑:我认为在运行示例时查看输出会很有帮助...
C String: Familiar ol' c string!
Int number: 3
NSString: Hello World!
NSDate: 2010-03-12 01:52:31 -0600
NSArray: (
"Hello There!"
)
2010-03-12 01:52:31.385 printfTest[2828:a0f] NSArray *array =
(
"Hello There!"
)
Run Code Online (Sandbox Code Playgroud)