我想在领域查询中使用结果值。我有如下的sqlite查询示例,
query = [NSString stringWithFormat:@"Select * from tbl_numbering_plan where np_country_code ='%@' and np_prefix = SUBSTR('%@',0,Length(np_prefix))", [dictContactDetails objectForKey:KEY_CountryCode], [dictContactDetails objectForKey:KEY_CountryCode], [dictContactDetails objectForKey:KEY_Phonenumber]];
这里tbl_numbering_plan
是表名,np_country_code
和np_prefix
是表的行。
请考虑查询的最后一部分是np_prefix = SUBSTR('%@',0,Length(np_prefix)
。我想在Realm中进行相同的查询。
这是您的表格tbl_numbering_plan
像常规的Swift类一样定义模型
class Numbering_plan: Object {
@objc dynamic var np_country_code = ""
@objc dynamic var np_prefix = ""
}
查询的最后一部分是np_prefix = SUBSTR('%@',0,Length(np_prefix)
,
只需将其作为Swift常规Substring
处理>
let dictContactDetails = [String: Any]()
let KEY_CountryCode = "KEY_CountryCode"
let KEY_Phonenumber = "KEY_Phonenumber"
let np_prefix = "np_prefix"
if let code = dictContactDetails[KEY_CountryCode] as? String, var phone = dictContactDetails[KEY_Phonenumber] as? String{
let index = phone.index(phone.startIndex, offsetBy: np_prefix.count)
phone = String(phone[..<index])
let result = realm.objects(Numbering_plan.self).filter("np_country_code = '\(code)' AND np_prefix = '\(phone)'")
print(result)
}