gpt4 book ai didi

ios - NSPredicate 对象模型数组过滤器无法在 Swift 4.0 中崩溃

转载 作者:行者123 更新时间:2023-11-28 10:44:55 26 4
gpt4 key购买 nike

  1. 我在 swift 4.0 中工作,我在 MutableArray 中有模型对象, NSPredicate 不工作。

                 //Here is code
    //Model class:
    class Modelclass: NSObject
    {
    var firstName:String!
    var lastName:String!
    }

    // Viewcontroller doing predicate:
    let ary = NSMutableArray()

    let userModel = Modelclass()
    userModel.firstName = "Arrya"
    userModel.lastName = "stark"
    ary.add(userModel)

    let commitPredicate = NSPredicate(format: "firstName == %@", "Arrya")
    let resultAry = ary.filtered(using: commitPredicate)
    print(resultAry)

最佳答案

我解决了这个问题, 解决方案一:

    //I just added below code in model class

extension Modelclass {
@objc override func value(forKey key: String) -> Any? {
switch key {
case "firstName":
return firstName
case "lastName":
return lastName
default:
return nil
}
}
}

解决方案 2:

在变量前添加@objc:

        class Modelclass: NSObject
{
@objc var firstName:String!
@objc var lastName:String!

}

关于ios - NSPredicate 对象模型数组过滤器无法在 Swift 4.0 中崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48881218/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com