首页 文章

_NSDate长度无法识别的选择器发送到实例

提问于
浏览
-1

我正在尝试将NSString转换为NSDate

NSString destructString是2015-01-04 08:36:42 0000

I know the problem starts at the place that I commented below but I can't seem to find what the problem is.

程序在该点停止并在 Headers 中给出错误 .

代码的要点是比较两个日期,看看哪个更旧 .

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");
    }
}];

Also I know there's another post on this subject however I have tried that fix (which was redoing the formatting) but it did not solve the issue. Thanks in advance!

1 回答

  • 1

    如果对象类型可能不同,您应该添加类型检查,因为我添加到您的代码中

    [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");
        }
    }];
    

相关问题