iPhone中日期字符串中的问题?

时间:2012-10-16 13:24:17

标签: iphone ios string nsdateformatter

我正在开发一款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:

1 个答案:

答案 0 :(得分:0)

试试这个,

UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 30)];
    lbl.text = theDate;
    [self.view addSubview:lbl];

它正在运作