Reputation: 115
I am trying to put my NSDate into a UILabel inside a cell. Now I am having this issue
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSTaggedDate rangeOfCharacterFromSet:]: unrecognized selector sent to instance 0xe41bd157d5000000'
Here's what I've done so far
- (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];
}
Upvotes: 1
Views: 227
Reputation: 113
You can't assign NSDate
to text directly, convert it to NSString
using NSDateFormatter
and then assign it. The text property of UILabel is of NSString kind. And you are assigning NSDate.
Upvotes: 1
Reputation: 5435
Convert your NSDate
to NSString
, than store it into Dictionary as below:
NSDate* localDate = [formatterUtc dateFromString:[formatterLocal stringFromDate:utcDate]];
NSString *strLocalDate=[formatterUtc stringFromDate:localDate];
[dict setObject:strLocalDate forKey:@"Date"];
[myArray addObject:dict];
Upvotes: 0
Reputation: 3677
You are assigning a NSDate
to a NSString
object. First create a NSDateFormatter
object and the assign the date to string like this.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy''HH:mm"];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
Upvotes: 0