当前位置: 代码迷 >> Iphone >> iphone中NSDateFormatter模拟器可以通常显示,真机不行,为NULL
  详细解决方案

iphone中NSDateFormatter模拟器可以通常显示,真机不行,为NULL

热度:14   发布时间:2016-04-25 06:30:05.0
iphone中NSDateFormatter模拟器可以正常显示,真机不行,为NULL

iphone中NSDateFormatter模拟器可以正常显示,但在真机上不行,转换后的NSString为NULL,部分代码如下:

?

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"EEE MMM dd HH:mm:ss zzzz yyyy"];

NSDate *date = [formatter dateFromString:createTime];

[formatter setDateFormat:@"yyyyMMdd HH:mm:ss"];

NSString *dateStr =?[formatter?stringFromDate:date];

NSLog(@"%@",dateStr); ? ?//模拟器上正常,真机打出来为Null

createTimeLbl.text = dateStr;

[bgScrollVw addSubview:createTimeLbl];

[formatter release];

[createTimeLbl release];

?

找了半天,终于找到原因:

在真机运行需要设置Local,添加下面一段代码就OK:

NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];

[formatter setLocale:locale];

  相关解决方案