Как использовать результат запроса в самом запросе в Realm - PullRequest
0 голосов
/ 26 марта 2020

Я хочу использовать значение результата в запросе области. У меня есть пример запроса 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). Я хочу сделать такой же запрос в области.

1 Ответ

0 голосов
/ 26 марта 2020

Вот ваша таблица 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)
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...