无法将字符串与自定义对象进行比较?

时间:2014-04-04 02:22:47

标签: ios iphone objective-c xcode nsuserdefaults

我需要将NSString从我的NSUserDefaults与我的自定义对象进行比较,但我无法弄清楚如何让它工作。

- (void)tableView:(UITableView *)tableViews commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MMMM dd, yyyy"];

    NSString *todayString = [dateFormat stringFromDate:self.todayDate];

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        //Delete from storage
        for (int x = 0; x!=-99; x++) {

            NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@%d",todayString,x]];
            AG_Storage *someStorageObject = [NSKeyedUnarchiver unarchiveObjectWithData:data];
            AG_Storage *storeToArray = [[AG_Storage alloc]init];
            storeToArray.itemName = someStorageObject;


            NSLog(@"%@",someStorageObject);
            NSLog(@"%@", [self.mainArray objectAtIndex:indexPath.row]);

            //HAVE TO TURN mainArray TO A STRING
            if ([self.mainArray objectAtIndex:indexPath.row] == someStorageObject) {

                NSLog(@"strings equaled, trying to delete...");
                [[NSUserDefaults standardUserDefaults]removeObjectForKey:[NSString stringWithFormat:@"%@%d",todayString,x]];
                [[NSUserDefaults standardUserDefaults]synchronize];
                x=-100;
            }
            else
            {
                NSLog(@"Trying loop again...");
            }
            if (x>5) {
                x=-100;
                NSLog(@"ERROR BROKE");
            }


        }

        [self.mainArray removeObjectAtIndex:indexPath.row];
        [tableViews deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];


    }
}

我正在尝试从运行的mutablearray和您看到的存储数据中删除一些内容。这是我的日志

2014-04-03 22:19:18.344 AgendaBk[60877:a0b] test
2014-04-03 22:19:18.345 AgendaBk[60877:a0b] <AG_Storage: 0x89627f0>
2014-04-03 22:19:18.345 AgendaBk[60877:a0b] Trying loop again...
2014-04-03 22:19:18.346 AgendaBk[60877:a0b] Test 2
2014-04-03 22:19:18.346 AgendaBk[60877:a0b] <AG_Storage: 0x89627f0>
2014-04-03 22:19:18.347 AgendaBk[60877:a0b] Trying loop again...
2014-04-03 22:19:18.347 AgendaBk[60877:a0b] testeisca
2014-04-03 22:19:18.347 AgendaBk[60877:a0b] <AG_Storage: 0x89627f0>
2014-04-03 22:19:18.348 AgendaBk[60877:a0b] Trying loop again...
2014-04-03 22:19:18.348 AgendaBk[60877:a0b] *** -[NSKeyedUnarchiver initForReadingWithData:]: data is NULL
2014-04-03 22:19:18.349 AgendaBk[60877:a0b] (null)
2014-04-03 22:19:18.349 AgendaBk[60877:a0b] <AG_Storage: 0x89627f0>
2014-04-03 22:19:18.350 AgendaBk[60877:a0b] Trying loop again...
2014-04-03 22:19:18.350 AgendaBk[60877:a0b] *** -[NSKeyedUnarchiver initForReadingWithData:]: data is NULL
2014-04-03 22:19:18.350 AgendaBk[60877:a0b] (null)
2014-04-03 22:19:18.351 AgendaBk[60877:a0b] <AG_Storage: 0x89627f0>
2014-04-03 22:19:18.351 AgendaBk[60877:a0b] Trying loop again...
2014-04-03 22:19:18.352 AgendaBk[60877:a0b] *** -[NSKeyedUnarchiver initForReadingWithData:]: data is NULL
2014-04-03 22:19:18.352 AgendaBk[60877:a0b] (null)
2014-04-03 22:19:18.353 AgendaBk[60877:a0b] <AG_Storage: 0x89627f0>
2014-04-03 22:19:18.353 AgendaBk[60877:a0b] Trying loop again...
2014-04-03 22:19:18.354 AgendaBk[60877:a0b] *** -[NSKeyedUnarchiver initForReadingWithData:]: data is NULL
2014-04-03 22:19:18.354 AgendaBk[60877:a0b] (null)
2014-04-03 22:19:18.355 AgendaBk[60877:a0b] <AG_Storage: 0x89627f0>
2014-04-03 22:19:18.355 AgendaBk[60877:a0b] Trying loop again...
2014-04-03 22:19:18.356 AgendaBk[60877:a0b] ERROR BROKE

有什么方法可以让<AG_Storage: 0x89627f0>再次成为一个字符串,这样我可以比较两个并删除吗?如果没有,是否有合适的备份方法?令人沮丧的部分是我知道我做得不对,但不知道如何修复它。

2 个答案:

答案 0 :(得分:0)

AG_Storage *compareStorage = (AG_Storage *)[self.mainArray objectAtIndex:indexPath.row];
if ([compareStorage isKindOfClass:[AG_Storage class]] && [compareStorage.itemName isEqualToString:someStorageObject.itemName])
{
  //do something
}

答案 1 :(得分:-1)

你可以测试和演员:

id someObject = [self.mainArray objectAtIndex:indexPath.row];
if([someObject isKindOfClass:[NSString class]]) {
    NSString *string = (NSString *) someObject;
} else {
    // unexpected error?
}