过滤自定义对象的数组 [英] Filtering array of custom objects

查看:139
本文介绍了过滤自定义对象的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不是 NSObject 的子类的类以及该类的实例数组。 enum ObjectType {
case type1
case type2 $ b $ case type3
}
$ b $ class MyObject {
var type = ObjectType!
//一些其他的属性...
}

let array = [obj1(type:type1),
obj2(type:type2),$ b $ (类型:类型3)]

let type2Array = array.filter(){$ 0.type(type:type3),
obj4(type:type2),
obj5 == .type2}
// type2Array应该是[obj2,obj4]

这导致致命错误:数组无法从Objective-C桥接



如何适当地过滤数组? / p>

是否必须从 NSObject 子类,或者让我的类符合任何协议?

$ b $我从你的问题中可以看到,以上都没有和Objective-C有任何关系。你的例子包含了一些其他的问题,但是,它保持了预期的工作。


  • MyObject 没有初始化器(从Swift 2.2开始,至少包含一个初始化器)。
  • 什么是 obj1 obj2 ,...?当我假设你打算把它们作为 MyObject 实例时,你将它们当作方法或类/结构类型 $ c $>


    如果修复上面的问题,代码的实际过滤部分将按预期工作(注意,您可以省略()来自 filter(){...} ),例如:

      enum ObjectType {
    case type1
    case type2
    case type3
    }

    class MyObject {
    var type:ObjectType
    let id:Int
    init(type:ObjectType,id:Int){
    self.type = type
    self.id = (MyType(type:.type1,id:1),
    MyObject(type:.type2,id:2) ,
    MyObject(type:.type3,id:3),
    MyObject(type:.type2,id:4),
    MyObject(type:.type3,id:5)]

    let type2Array = array.filter {$ 0.type == .type2}
    type2Array.fo rEach {print($ 0.id)} // 2,4






    作为直接筛选枚举的一种替代方法,您可以指定枚举的 rawValue 类型并与之匹配。例如。使用 Int rawValue 可以让您(除了过滤 rawValue

      enum ObjectType:Int {
    )对于枚举中的个案范围进行模式匹配。 case type1 = 1 // rawValue = 1
    case type2 // rawValue = 2,implicitly
    case type3 // ...
    }

    class MyObject {
    var type:ObjectType
    let id:Int
    init(type:ObjectType,id:Int){
    self.type = type
    self.id = id



    let array = [MyObject(type:.type1,id:1),
    MyObject(type:.type2,id:2),
    MyObject(type:.type3,id:3),
    MyObject(type:.type2,id:4),
    MyObject(type:.type3,id:5)]

    / * filter wrt to rawValue * /
    let type2Array = array.filter {$ 0.type.rawValue == 2}
    type2Array.forEach {print($ 0.id)} // 2,4

    / *使用模式匹配的过滤器,用于范围[1,2]中的rawValues,
    < =>过滤条件为.type1和.type2 *为真。 // 1,2,4


    I had a class which is not a subclass of NSObject and an array of instances of that class.

    enum ObjectType{
      case type1
      case type2
      case type3  
    }
    
    class MyObject {
      var type = ObjectType!
      //some other properties...
    }
    
    let array = [obj1(type:type1),
                 obj2(type:type2),
                 obj3(type:type3),
                 obj4(type:type2),
                 obj5(type:type3)]
    
    let type2Array = array.filter(){ $0.type == .type2}
    // type2Array is supposed to be [obj2, obj4]
    

    this is causing fatal error: array cannot be bridged from Objective-C

    How can I filter array appropriately?

    Do I have to subclass from NSObject or make my class conform to any protocol?

    解决方案

    From what I can see from your question, none of the above really has any connection to Objective-C. Your example contains a few other issues, however, keeping it from working as intended.

    • MyObject has no initializer (as of Swift 2.2, you should include at least one initializer).
    • What are obj1, obj2, ... ? You treat these as methods or class/structures types, when I presume you're intending for these to be instances of the MyObject type.

    If fixing the above, the actual filtering part of your code will work as intended (note that you can omit () from filter() {... }), e.g.:

    enum ObjectType{
        case type1
        case type2
        case type3
    }
    
    class MyObject {
        var type : ObjectType
        let id: Int
        init(type: ObjectType, id: Int) {
            self.type = type
            self.id = id
        }
    }
    
    let array = [MyObject(type: .type1, id: 1),
                 MyObject(type: .type2, id: 2),
                 MyObject(type: .type3, id: 3),
                 MyObject(type: .type2, id: 4),
                 MyObject(type: .type3, id: 5)]
    
    let type2Array = array.filter { $0.type == .type2}
    type2Array.forEach { print($0.id) } // 2, 4
    


    As an alternative to filtering directly to an enumeration case, you could specify the rawValue type of your enumeration and match to this instead. E.g. using an Int rawValue allows you to (in addition to filtering w.r.t. rawValue) perform pattern matching for say, ranges of cases in your enumeration.

    enum ObjectType : Int {
        case type1 = 1  // rawValue = 1
        case type2      // rawValue = 2, implicitly
        case type3      // ...
    }
    
    class MyObject {
        var type : ObjectType
        let id: Int
        init(type: ObjectType, id: Int) {
            self.type = type
            self.id = id
        }
    }
    
    let array = [MyObject(type: .type1, id: 1),
                 MyObject(type: .type2, id: 2),
                 MyObject(type: .type3, id: 3),
                 MyObject(type: .type2, id: 4),
                 MyObject(type: .type3, id: 5)]
    
    /* filter w.r.t. to rawValue */
    let type2Array = array.filter { $0.type.rawValue == 2}
    type2Array.forEach { print($0.id) } // 2, 4
    
    /* filter using pattern matching, for rawValues in range [1,2],
       <=> filter true for cases .type1 and .type2 */
    let type1or2Array = array.filter { 1...2 ~= $0.type.rawValue }
    type1or2Array.forEach { print($0.id) } // 1, 2, 4
    

    这篇关于过滤自定义对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆