一、在objective-c中,打印使用NSLog()。
常規的打印大家都清楚。
<span style="font-size:18px;">NSLog(@"%@",@"輸出內容");</span>
%@:佔位符;@"輸出內容":輸出的內容
常用的佔位符如下:
description方法。下面是例子:
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic,assign)int age;
@property (nonatomic,assign)NSString *name;
@end
#import "Person.h"
@implementation Person
//重寫description方法,實現打印對象的所有成員變量
-(NSString*)description{
return [NSString stringWithFormat:@"age = %d,name = %@",_age,_name];
}
@end
Person *person = [[Person alloc]init];
person.age = 23;
person.name = @"wxx";
NSLog(@"%@",person);
打印結果是:
age = 23,name = wxx
三、相關補充
NSLog(@"%d",__LINE__);//打印當前代碼行號
NSLog(@"%s",__FILE__);//打印當前文件路徑
NSLog(@"%s",__func__);//打印所屬的方法或函數名