ios - 检测 NSMutable 数组中包含的重复自定义对象

标签 ios objective-c nsmutablearray

我已经阅读了每个类似的问题,但已经确定我正在做一些愚蠢的事情(可能)或者我无法掌握NSArray 方法containsObject:

我正在尝试设置一个包含保存的“收藏夹”的UITableView;作为名为“MapAnnotations”的自定义类保留的位置。其中包含坐标、标题、信息字段和一些其他参数等内容。我成功地从 NSUserDefaults 实例保存/检索它,但似乎无法成功检测 NSMutableArray 中保存的重复对象。

相关代码如下:

-(void)doSetUp
{
//load up saved locations, if it exists

NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];

//if there are saved locations
if ([myDefaults objectForKey:@"savedLocations"]) {

    NSLog(@"file exists!");

      //get saved data and put in a temporary array
    NSData *theData = [myDefaults dataForKey:@"savedLocations"];
      //my custom object uses NSCode protocol
    NSArray *temp = (NSArray *)[NSKeyedUnarchiver unarchiveObjectWithData:theData];
    NSLog(@"temp contains:%@",temp);
      //_myFavs currently exists as a NSMutableArray property
    _myFavs = [temp mutableCopy];

}else{

    NSLog(@"File doesn't exist");
    _myFavs = [[NSMutableArray alloc]init];
}

    //_currLoc is an instance of my Mapnnotations custom class
        // which contains coordinates, title, info, etc.

if (_currLoc != nil) {

        //if this is a duplicate of a saved location

    if ([_myFavs containsObject:_currLoc]) {

        //pop an alert

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Sorry..." message:@"That location has already been saved." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }else{
            //add location to end of myFavs array
        [_myFavs addObject:_currLoc];

        NSLog(@"myFavs now contains:%@",_myFavs);

        //write defaults

        NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:_myFavs];
        [myDefaults setObject:encodedObject forKey:@"savedLocations"];
        [myDefaults synchronize];
        }
    }
}

我尝试通过 _myFavs 数组进行枚举,检查特定字段上的匹配情况(通过可变的内容进行枚举时出现错误),尝试复制到直接数组...尝试使用 indexOfObject:..

最佳答案

您可以将 containsObject: 方法与实现 isEqual: 方法的自定义对象一起使用。将此方法的实现添加到您的 Mapnnotations 类中将解决该问题:

// In the .h file:
@interface Mapnnotations : NSObject
-(BOOL)isEqual:(id)otherObj;
...
@end

// In the .m file:
@implementation Mapnnotations
-(BOOL)isEqual:(id)otherObj {
    ... // Check if other is Mapnnotations, and compare the other instance
        // to this instance
    Mapnnotations *other = (Mapnnotations*)otherObj;
    // Instead of comparing unique identifiers, you could compare
    // a combination of other custom properties of your objects:
    return self.uniqueIdentifier == other.uniqueIdentifier;
}
@end

注意:当您实现自己的 isEqual: 方法时,最好也实现 hash 方法。这将允许您在哈希集中使用自定义对象并作为 NSDictionary 键。

关于ios - 检测 NSMutable 数组中包含的重复自定义对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22180050/

相关文章:

ios - 在NSMutableArray中删除

android - iOS 到安卓 : How to display a TextView

ios - AlertController 的代码不正确

ios - Int NSFormatter for swift 123456 到 1,23,456

ios - 字符串到字典和向后

objective-c - 如何访问首选项 Pane 包?

ios - 使用不自然的排序顺序重新排列 NSMutableArray

ios - 尝试为 iOS 构建应用程序时出现 Cordova 错误 65

iphone - 写入/读取 plist 文件 iPhone

ios - 使用 NSSet 从 NSMutableArray 中删除重复项