_NSDate 长度无法识别的选择器发送到实例
_NSDate length unrecognized selector sent to instance
我正在尝试将 NSString 转换为 NSDate
NSString destructString 是 2015-01-04 08:36:42 +0000
我知道问题从我在下面评论的地方开始,但我似乎找不到问题所在。
程序在该点停止并给出标题中的错误。
代码的重点是比较两个日期以查看哪个日期更早。
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
NSDate *today = [[NSDate alloc]init];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if(!error){
for(PFObject* object in [self objects]){
NSLog(@"OBJECT: %@", object);
PAWPost *post = [[PAWPost alloc] initWithPFObject:object];
NSLog(@"post: %@", post);
NSLog(@"today: %@", today);
NSString *destructString = [post.object objectForKey:selfDestructDateKey];
NSLog(@"destruct string: %@", destructString);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss zz"];
//SOURCE OF PROBLEM BEGINS HERE
NSDate *destructDate = [dateFormat dateFromString:destructString];
NSLog(@"destruct date: %@", destructDate);
if([destructDate compare:today] == NSOrderedDescending){
[object deleteEventually];
NSLog(@"DESCENDING");
}
else
NSLog(@"NOT DESCENDING");
}
}else {
NSLog(@"Error in findObjectsInBackgroundWithBlock for destruct purposes");
}
}];
另外我知道关于这个问题还有另一个 post 但是我已经尝试了那个修复(重做格式)但它没有解决问题。提前致谢!
如果对象的类型可能会有所不同,您应该像我添加到您的代码中一样添加类型检查
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if(!error){
for(PFObject* object in [self objects]){
NSLog(@"OBJECT: %@", object);
PAWPost *post = [[PAWPost alloc] initWithPFObject:object];
NSLog(@"post: %@", post);
NSLog(@"today: %@", today);
id destructString = [post.object objectForKey:selfDestructDateKey];
NSLog(@"destruct string: %@", destructString);
if ([destructString isKindOfClass:[NSDate class]]) { // already date
NSDate * destructDate = (NSDate *)destructString;
} else { // parse string
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss zz"];
//SOURCE OF PROBLEM BEGINS HERE
NSDate *destructDate = [dateFormat dateFromString:destructString];
NSLog(@"destruct date: %@", destructDate);
if([destructDate compare:today] == NSOrderedDescending){
[object deleteEventually];
NSLog(@"DESCENDING");
}
else
NSLog(@"NOT DESCENDING");
}
}
}else {
NSLog(@"Error in findObjectsInBackgroundWithBlock for destruct purposes");
}
}];
我正在尝试将 NSString 转换为 NSDate
NSString destructString 是 2015-01-04 08:36:42 +0000
我知道问题从我在下面评论的地方开始,但我似乎找不到问题所在。
程序在该点停止并给出标题中的错误。
代码的重点是比较两个日期以查看哪个日期更早。
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
NSDate *today = [[NSDate alloc]init];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if(!error){
for(PFObject* object in [self objects]){
NSLog(@"OBJECT: %@", object);
PAWPost *post = [[PAWPost alloc] initWithPFObject:object];
NSLog(@"post: %@", post);
NSLog(@"today: %@", today);
NSString *destructString = [post.object objectForKey:selfDestructDateKey];
NSLog(@"destruct string: %@", destructString);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss zz"];
//SOURCE OF PROBLEM BEGINS HERE
NSDate *destructDate = [dateFormat dateFromString:destructString];
NSLog(@"destruct date: %@", destructDate);
if([destructDate compare:today] == NSOrderedDescending){
[object deleteEventually];
NSLog(@"DESCENDING");
}
else
NSLog(@"NOT DESCENDING");
}
}else {
NSLog(@"Error in findObjectsInBackgroundWithBlock for destruct purposes");
}
}];
另外我知道关于这个问题还有另一个 post 但是我已经尝试了那个修复(重做格式)但它没有解决问题。提前致谢!
如果对象的类型可能会有所不同,您应该像我添加到您的代码中一样添加类型检查
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if(!error){
for(PFObject* object in [self objects]){
NSLog(@"OBJECT: %@", object);
PAWPost *post = [[PAWPost alloc] initWithPFObject:object];
NSLog(@"post: %@", post);
NSLog(@"today: %@", today);
id destructString = [post.object objectForKey:selfDestructDateKey];
NSLog(@"destruct string: %@", destructString);
if ([destructString isKindOfClass:[NSDate class]]) { // already date
NSDate * destructDate = (NSDate *)destructString;
} else { // parse string
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss zz"];
//SOURCE OF PROBLEM BEGINS HERE
NSDate *destructDate = [dateFormat dateFromString:destructString];
NSLog(@"destruct date: %@", destructDate);
if([destructDate compare:today] == NSOrderedDescending){
[object deleteEventually];
NSLog(@"DESCENDING");
}
else
NSLog(@"NOT DESCENDING");
}
}
}else {
NSLog(@"Error in findObjectsInBackgroundWithBlock for destruct purposes");
}
}];