2016-11-03 4 views
0

id이 nil이거나 비어있는 경우 영역 결과 목록을 필터링하려고합니다.영역 목록의 빈 개체 필터링

{ 
    "id":"1" 
    "name": "first" 
}, 
{ 
    "id":"2" 
    "name": "second" 
}, 
{ 
    "id":"3" 
    "name": "third" 
}, 
{ 
    "id":"" //here it can be empty 
    "name": "" 
}, 
{ 
    "id": nil // here it can be nil 
    "name": nil 
} 

나는이 같은 ID를 사용하여 필터링하려고하지만 충돌 :

import RealmSwift 

public final class Declaration: Object { 
    dynamic var id: String = "" 
    dynamic var name: String = "" 

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

답변

0
: 여기
lazy var declarations: Results<Declaration> = { 
     let realm = try! Realm() 
     return self.realm.objects(Declaration.self).filter("id == " "") 
    }() 

모델입니다 여기

결과의 데모 목록입니다

.filter("id == " "") 당신은 그 따옴표를 이스케이프하지 않았기 때문에 분명히 추락 할 것입니다. .filter("id == \"\"")이 필요하지만 작은 따옴표를 사용하는 것이 좋습니다.

영역 질의는 영역 속성이 비어 있거나 nil이 아닌 경우 단순히 확인하려는 경우, this question에서 답을 복사 NSPredicate을 준수하기 때문에

, 당신은 단지 사용하여 조회 할 수 있어야한다

lazy var declarations: Results<Declaration> = { 
    let realm = try! Realm() 
    return self.realm.objects(Declaration.self).filter("id != nil AND id != ''") 
}()