我正在开发一款iPhone应用程序。我正在使用NSDateFormatter
格式化NSString
中的日期,然后我尝试将该日期字符串设置为UILabel
。我试过这段代码:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm:ss"];
NSDate *now = [[NSDate alloc] init];
NSString *theDate = [dateFormat stringFromDate:now];
Result: theDate:16/10/2012
但是我收到了一个错误:
UILabel *lbl = [[UILabel alloc]init];
lbl.text = theDate; // the error comes here
错误讯息:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray isEqualToString:]: unrecognized selector sent to instance 0x6b837e0'
*** First throw call stack:
答案 0 :(得分:0)
试试这个,
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 30)];
lbl.text = theDate;
[self.view addSubview:lbl];
它正在运作