gpt4 book ai didi

ios - 将 NSDate 放入 UILabel 问题

转载 作者:行者123 更新时间:2023-11-29 11:58:59 24 4
gpt4 key购买 nike

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

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__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];
}

最佳答案

不能直接将NSDate赋值给文本,使用NSDateFormatter将其转化为NSString后再赋值。 UILabel 的文本属性属于 NSString 类型。你正在分配 NSDate。

关于ios - 将 NSDate 放入 UILabel 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37848868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com