0

当我在真实设备中运行应用程序时,NSDate 对象适用于 iPhone、iPad。但是当我在 iPad 真实设备上运行应用程序时,它会<not an Objective-C object>报错。我试着整理一下。但不能。

- (NSString*)getDateFromJSONToStringSaveFormat:(NSString *)dateString
{
    NSDate *_Date = [NSDate alloc] init];
    NSDate *_Date = [self getDateFromJSON:dateString];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    return [dateFormatter stringFromDate:_Date];
} 

- (NSDate*)  getDateFromJSON:(NSString *)dateString
{
    // Expect date in this format "/Date(1268123281843)/"
    int startPos = [dateString rangeOfString:@"("].location+1;
    int endPos = [dateString rangeOfString:@")"].location;
    NSRange range = NSMakeRange(startPos,endPos-startPos);
    unsigned long long milliseconds = [[dateString substringWithRange:range] longLongValue];

    NSTimeInterval interval = milliseconds/1000;
    return [NSDate dateWithTimeIntervalSince1970:interval];

}

由于这个问题,我初始化了 NSDate 对象并看到了日期值。(NSDate *_Date = [NSDate alloc] init];)在这里也给出了同样的错误?这是为什么?有人遇到这个错误吗??

4

1 回答 1

1

首先,您可以删除此行:

NSDate *_Date = [NSDate alloc] init];

由于下一行只是重新声明它,因此您在应该删除的行中也缺少一个[.

- (NSString*)getDateFromJSONToStringSaveFormat:(NSString *)dateString
{
    // Not needed since the line after it also declares the variable.
    //NSDate *_Date = [NSDate alloc] init];
    NSDate *_Date = [self getDateFromJSON:dateString];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    return [dateFormatter stringFromDate:_Date];
} 
于 2013-10-07T15:24:00.630 回答