如何在领域本身的查询中使用查询结果

问题描述 投票:0回答:1

我想在领域查询中使用结果值。我有如下的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_codenp_prefix是表的行。

请考虑查询的最后一部分是np_prefix = SUBSTR('%@',0,Length(np_prefix)。我想在Realm中进行相同的查询。

ios swift realm
1个回答
0
投票

这是您的表格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)
}
© www.soinside.com 2019 - 2024. All rights reserved.