过滤Realm对象的属性

时间:2018-05-16 05:07:20

标签: ios json swift filter realm

是否可以使用谓词过滤空值?例如,我有一个看起来像这样的Realm对象:

class CropData: Object  {

@objc dynamic var cropper = ""
@objc dynamic var x: CGFloat = 0.0
@objc dynamic var y: CGFloat = 0.0
@objc dynamic var width : CGFloat = 0.0
@objc dynamic var height: CGFloat = 0.0
@objc dynamic var scaleX: CGFloat = 0.0
@objc dynamic var scaleY: CGFloat = 0.0
@objc dynamic var rotate: CGFloat = 0.0


override static func primaryKey() -> String? {
return "cropper"
}

示例json数据

{"cropper": "","x": "0","y": "0","width": "0","height": "0","scaleX": "0","scaleY": "0","rotate": "0"}

在这个例子中,有没有办法可以过滤或忽略空的" cropper"属性?

2 个答案:

答案 0 :(得分:0)

您可以像这样过滤

let realm = try! Realm()
let result = realm.objects(CropData).filter("cropper = %@", <#YOUR VALUE#>)

答案 1 :(得分:0)

您可以使用Predicate过滤

使用cropper获取所有对象=&#34;&#34; :

let result = realm.objects(CropData.self).filter("cropper = %@", "")

或忽略它

let result = realm.objects(CropData.self).filter("cropper != %@", "")

您可以在此处阅读更多Predicate语法https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html