gpt4 book ai didi

objective-c - 我们如何在 Objective-C 中打印不同类型的数据类型?

转载 作者:太空狗 更新时间:2023-10-30 03:59:15 28 4
gpt4 key购买 nike

我想打印所有类型的值,例如 char、long...等等,还有 nsdate、nsdictionary、frame...。我现在想打印每个类型变量的值。

最佳答案

int、float、double 等基本类型可以使用 printffprintf 等以与在 C 中打印相同的方式打印。如果您需要打印一个类的数据你经常可以使用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;
}

编辑:我认为在示例运行时查看输出会有所帮助...

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!"
)

关于objective-c - 我们如何在 Objective-C 中打印不同类型的数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2430706/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com