ios - 从 NSArray 中删除重复的键值

标签 ios swift nsmutablearray

{
        ImageName = "http://192.168.1.8:9701/190214/190214_image2_08_13_2016_11_55_44.jpg";
        ImagePath = "<null>";
        ImageTypeId = 2;
    },
        {
        ImageName = "http://192.168.1.8:9701/190214/190214_image3_08_13_2016_11_55_45.jpg";
        ImagePath = "<null>";
        ImageTypeId = 3;
    },
        {
        ImageName = "http://192.168.1.8:9701/190214/190214_image4_08_13_2016_11_55_48.jpg";
        ImagePath = "<null>";
        ImageTypeId = 4;
    },
        {
        ImageName = "http://192.168.1.8:9701/190214/190214_image5_08_13_2016_11_55_51.jpg";
        ImagePath = "<null>";
        ImageTypeId = 5;
    },
        {
        ImageName = "http://192.168.1.8:9701/190214/190214_image6_08_13_2016_11_55_53.jpg";
        ImagePath = "<null>";
        ImageTypeId = 6;
    },
        {
        ImageName = "http://192.168.1.8:9701/190214/190214_image0_08_13_2016_06_15_21.jpg";
        ImagePath = "<null>";
        ImageTypeId = 0;
    },
        {
        ImageName = "http://192.168.1.8:9701/190214/190214_image1_08_13_2016_07_19_57.jpg";
        ImagePath = "<null>";
        ImageTypeId = 1;
    },
        {
        ImageName = "http://192.168.1.8:9701/190214/190214_image1_08_13_2016_07_19_57.jpg";
        ImagePath = "<null>";
        ImageTypeId = 1;
    }
)

我想删除所有键重复的对象 对于前。图像类型ID = 1;对象是两次,因此想要删除 1 个对象,并且仅删除一个 ImageTypeId = 1 的对象;

最佳答案

创建一个字典来保存值:

var dict = [NSObject: AnyObject]()

使用 typeid 作为键将每个项目添加到字典中:

for item in arr {
    guard let typeid = item["ImageTypeId"] as? NSObject else { continue }
    dict[typeid] = item
}

使用字典值创建一个数组:

let newarray = Array(dict.values)

关于ios - 从 NSArray 中删除重复的键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38933644/

相关文章:

IOS:切换段时为 'Thread 1: Signal SIGABRT'

string - 显示没有毫秒的 CFGregorianDate

ios - UITableview 中各部分之间的空间

objective-c - 将 NSMutableArray 的前 10 个对象复制到另一个 NSMutableArray

ios - 尝试插入非属性值 Objective C

ios - 导航 Controller header 未显示

ios - 实现 App Transport Security 的最佳方式是什么?

ios - UILabel 初始化时调用的函数

ios - Swift 从另一个 View Controller 呈现 View Controller

ios - 为什么我可以将消息发送到已释放的 NSArray 实例?