【问题标题】:Placing NSDate into UILabel issue将 NSDate 放入 UILabel 问题
【发布时间】:2016-06-16 02:24:04
【问题描述】:

我正在尝试将我的 NSDate 放入单元格内的 UILabel 中。现在我遇到了这个问题

*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSTaggedDate rangeOfCharacterFromSet:]: unrecognized selector sent to instance 0xe41bd157d5000000”

这是我到目前为止所做的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 50, 130)];
NSDate *date  = [[myArray objectAtIndex:indexPath.row] objectForKey:@"Date"];
typeLabel.tag =2;
typeLabel.text = date;

[[cell.contentView viewWithTag:2]removeFromSuperview];
[cell.contentView addSubview:dateLabel];

return cell;
}

{
    Start = [[[[array objectAtIndex:0] valueForKey:@"StartTime"]objectAtIndex:index]valueForKey:@"text"];
    Start = [Start stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    Start = [Start stringByReplacingOccurrencesOfString:@"\t" withString:@""];
    Stream = [Stream stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString* format = @"yyyy-MM-dd HH:mm:ss";

    NSDateFormatter* formatterUtc = [[NSDateFormatter alloc] init];
    [formatterUtc setDateFormat:format];
    [formatterUtc setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];

    // Cast the input string to NSDate
    NSDate* utcDate = [formatterUtc dateFromString:Start];

    // Set up an NSDateFormatter for the device's local time zone
    NSDateFormatter* formatterLocal = [[NSDateFormatter alloc] init];
    [formatterLocal setDateFormat:format];
    [formatterLocal setTimeZone:[NSTimeZone localTimeZone]];

    // Create local NSDate with time zone difference
    NSDate* localDate = [formatterUtc dateFromString:[formatterLocal stringFromDate:utcDate]];

    NSLog(@"utc:   %@", utcDate);
    NSLog(@"local: %@", localDate);

    [dict setObject:localDate forKey:@"Date"];
    [myArray addObject:dict];
}

【问题讨论】:

    标签: ios objective-c nsdate


    【解决方案1】:

    您不能将NSDate 直接分配给文本,使用NSDateFormatter 将其转换为NSString 然后分配。 UILabel 的 text 属性是 NSString 类型的。你正在分配 NSDate。

    【讨论】:

      【解决方案2】:

      您正在将 NSDate 分配给 NSString 对象。首先创建一个NSDateFormatter 对象并将日期分配给这样的字符串。

      NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
      [dateFormatter setDateFormat:@"dd-MM-yyyy''HH:mm"];
      NSString *formattedDateString = [dateFormatter stringFromDate:date];
      

      【讨论】:

        【解决方案3】:

        将您的NSDate 转换为NSString,然后将其存储到字典中,如下所示:

            NSDate* localDate = [formatterUtc dateFromString:[formatterLocal stringFromDate:utcDate]];
        
            NSString *strLocalDate=[formatterUtc stringFromDate:localDate];
        
            [dict setObject:strLocalDate forKey:@"Date"];
            [myArray addObject:dict];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-10
          相关资源
          最近更新 更多