我有一些簡單的代碼應該轉換我從CoreData中檢索到的NSDate。由於某些原因,當相同的代碼在其他視圖中工作時,它不適用於此類。我究竟做錯了什麼?下面是有問題的代碼和我的日誌的屏幕截圖...stringFromDate沒有將NSDate轉換爲字符串
NSFetchRequest *request26 = [[NSFetchRequest alloc] init];
[request26 setEntity:entityDiscription];
[request26 setResultType:NSDictionaryResultType];
NSExpression *keyPathExpression26 = [NSExpression expressionForKeyPath:@"date"];
NSExpression *swimLast750Expression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:keyPathExpression26]];
NSExpressionDescription *expressionDescription26 = [[NSExpressionDescription alloc] init];
[expressionDescription26 setName:@"swimLast750"];
[expressionDescription26 setExpression:swimLast750Expression];
[expressionDescription26 setExpressionResultType:NSInteger16AttributeType];
[request26 setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription26]];
NSPredicate *pred26 = [NSPredicate predicateWithFormat:@"(date >= %@) AND (sport like %@) AND (sessiontype like %@)", swimSinceDateAsDate, sportTypeSwim, sessType1];
[request26 setPredicate:pred26];
NSError *error26;
NSArray *objects26 = [context executeFetchRequest:request26 error:&error26];
if (objects26 == nil) {
NSLog(@"The fetch request returned an array == nil");
} else {
NSLog(@"Contents of Array Object26 is:%@", objects26);
NSDate *sessDate = [[objects26 objectAtIndex:0] valueForKey:@"swimLast750"];
NSLog(@"NSDate is: %@", sessDate);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MMM-yyyy"];
NSLog(@"DATE FORMATTER: %@", [formatter stringFromDate:[[objects26 objectAtIndex:0] valueForKey:@"swimLast750"]]);
NSString *dateString = [formatter stringFromDate:sessDate];
//NSString *dateString = [formatter stringFromDate:[[objects26 objectAtIndex:0] valueForKey:@"swimLast750"]];
NSLog(@"dateString is: %@", dateString);
sw750Last = dateString;
}
'sessDate'不是'NSDate'。看起來這可能是一個時間戳 - 從某個參考時間偏移幾秒鐘;可以存儲爲'NSNumber'或'NSString'。 –