如何编写NSPredicate自定义对象(CGFloat var)BETWEEN操作

时间:2016-06-07 23:33:06

标签: swift nspredicate

我想通过我的自定义对象 BETWEEN 进行号码比较 我已完成字符串比较,但数字比较喊出错误,如下面的代码所示。

class CustomClass: NSObject {
    var name        : String?
    var aWidth      : CGFloat!
}

有效的字符串比较

let predicate = NSPredicate(format: "SELF.name contains[c] %@","Sal")

无论其

let predicate = NSPredicate(format: "SELF.aWidth > 0 AND SELF.aWidth < 50")

let predicate = NSPredicate(format: "SELF.aWidth < 50")

我知道这个错误。

let predicate = NSPredicate(format: "SELF.aWidth BETWEEN {%@, %@}", {minWidth, maxWidth})
  

原因:&#39; [&lt; test.CustomClass 0x7fe86274bfd0&gt; valueForUndefinedKey:]:此类与键 aWidth 不符合键值编码。&#39;

所有对此错误的搜索都会带给我故事板问题。

如何使用BETWEEN编写谓词? 提前致谢

更新

我还发现了这个关于在swift 中使用过滤器的例子,这很有帮助。但我仍然有问题。

class Person : NSObject {

    var age : CGFloat?

    init(age: CGFloat) {
        self.age = age
    }
}

过滤

let eventAttendees = [Person(age: 22), Person(age: 41), Person(age: 23), Person(age: 30)]
                let filteredAttendees = eventAttendees.filter({
                    $0.age < 30
                })

                for person in filteredAttendees {
                    print(person.age!)
                    }

当我尝试使用相同的技术时,我这次出现错误

第一种方法

let array = self.testDictionary.allValues as Array

var filteredArray = array.filter( { (user: Person) -> Bool in

                    return $0.age < 30
                })
  

匿名闭包参数不能在具有的闭包内使用   显式参数

第二种方法

let array = self.testDictionary.allValues as Array

var filteredArray = array.filter( { (user: Person) -> Bool in

                        return true
                    })
  

无法转换类型&#39;(人物)&#39;的值 - &GT;布尔对预期的争论   输入&#39;(Anyobject)&#39; - &GT;布尔

由于

3 个答案:

答案 0 :(得分:0)

首先更新Xcode以使用Swift 2。

假设您有字典ctest

[Int:CustomClass]

获取值数组(编译器知道它包含let testDictionary = [1: CustomClass(name: "Foo", aWidth: 1.0), 2: CustomClass(name: "Bar", aWidth: 25.0), 3: CustomClass(name: "Baz", aWidth: 75.0)] 个实例)

CustomClass

然后您可以使用let array = Array(testDictionary.values) 函数

filter

答案 1 :(得分:0)

通过添加构造函数来初始化

来修改您的类
class CustomClass: NSObject {
    var name        : String?
    var width      : CGFloat = 0.0

    // *** define constructor ***
    init(name: String, width: CGFloat) {
        self.name = name
        self.width = width
    }
}

// *** Declare Array ***
let arrObjects = NSMutableArray()

// *** create objects and add to array ***
let obj1 = CustomClass(name: "Scale", width: 200.0)
self.arrObjects.addObject(obj1)

let obj2 = CustomClass(name: "Pen", width: 40.0)
self.arrObjects.addObject(obj2)

print("1. Filter on String")   
// *** 1. Filter on String ***
var predicate = NSPredicate(format: "SELF.name contains[c] %@","sca")
var filteredArray = self.arrObjects.filteredArrayUsingPredicate(predicate)

for object in filteredArray
{
    let obj = object as! CustomClass
    print("\(obj.name!) \(obj.width)")
}

print("2. Filter on CGFloat")

// *** 2. Filter on CGFloat ***
predicate = NSPredicate(format: "width > %f AND width < %f",0.0, 50.0)

filteredArray = self.arrObjects.filteredArrayUsingPredicate(predicate)

for object in filteredArray
{
    let obj = object as! CustomClass
    print("\(obj.name!) \(obj.width)")
}


print("3. Filter using BETWEEN")
// *** 3. Filter using BETWEEN ***
predicate = NSPredicate(format: "width BETWEEN {%f, %f}",0.0, 50.0)
filteredArray = self.arrObjects.filteredArrayUsingPredicate(predicate)

for object in filteredArray
{
    let obj = object as! CustomClass
    print("\(obj.name!) \(obj.width)")
}

引自NSHipster blog by Mattt Thompson

答案 2 :(得分:-1)

对于swift 4 +:

,要在数组使用中对自定义对象使用谓词,如下所示
func checkIfJobIdContains(array:NSArray, job_id: String) -> Bool
{
    let temp = array.filter({ (result) -> Bool in
    let modelObj = result as! ModalMultipleJobRequestsPage
    if modelObj.job_id == job_id
    {
        return true
    }
    else
    {
        return false
    }
    })

    if temp.count != 0
    {
        return true
    }
    return false
}