Realm斷言

Filtering

If you’re familiar with NSPredicate, then you already know how to query in Realm. RLMObjects, RLMRealm, RLMArray, and RLMResults all provide methods that allow you to query for specific RLMObject instances by simply passing in an NSPredicate instance, predicate string, or predicate format string just as you would when querying an NSArray.

For example, the following would extend our earlier example by calling [RLMObject objectsWhere:] to retrieve all tan-colored dogs whose names begin with ‘B’ from the default Realm:

// Query using a predicate string
RLMResults<Dog *> *tanDogs = [Dog objectsWhere:@"color = 'tan' AND name BEGINSWITH 'B'"];

// Query using an NSPredicate
NSPredicate *pred = [NSPredicate predicateWithFormat:@"color = %@ AND name BEGINSWITH %@", @"tan", @"B"]; tanDogs = [Dog objectsWithPredicate:pred];

See Apple’s Predicates Programming Guide for more information about building predicates and use our NSPredicate Cheatsheet. Realm supports many common predicates:

  • The comparison operands can be property names or constants. At least one of the operands must be a property name.
  • The comparison operators ==, <=, <, >=, >, !=, and BETWEEN are supported for int, long, long long, float, double, and NSDate property types. Such as age == 45
  • Identity comparisons ==, !=, e.g.[Employee objectsWhere:@"company == %@", company]
  • The comparison operators == and != are supported for boolean properties.
  • For NSString and NSData properties, we support the ==, !=, BEGINSWITH, CONTAINS, and ENDSWITH operators, such as name CONTAINS ‘Ja’
  • For NSString properties, the LIKE operator may be used to compare the left hand property with the right hand expression: ? and* are allowed as wildcard characters, where ? matches 1 character and *matches 0 or more characters. Such as value LIKE '?bc*' matching strings like “abcde” and “cbc”.
  • Case insensitive comparisons for strings, such as name CONTAINS[c] ‘Ja’. Note that only characters “A-Z” and “a-z” will be ignored for case.
  • Realm supports the following compound operators: “AND”, “OR”, and “NOT”. Such as name BEGINSWITH ‘J’ AND age >= 32
    *The containment operand IN such as name IN {‘Lisa’, ‘Spike’, ‘Hachi’}
  • Nil comparisons ==, !=, e.g.[Company objectsWhere:@"ceo == nil"]. Note that Realm treats nil as a special value rather than the absence of a value, so unlike with SQL nil equals itself.
  • ANY comparisons, such as ANY student.age < 21
  • The aggregate expressions @count, @min, @max, @sum and @avg are supported on RLMArray and RLMResults properties, e.g. [Company objectsWhere:@"employees.@count > 5"] to find all companies with more than five employees.
  • Subqueries are supported with the following limitations:
  • @count is the only operator that may be applied to the SUBQUERY expression.
  • The SUBQUERY(…).@count expression must be compared with a constant.
  • Correlated subqueries are not yet supported.

For more, see [RLMObject objectsWhere:].

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末窗宦,一起剝皮案震驚了整個(gè)濱河市存谎,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖遭赂,帶你破解...
    沈念sama閱讀 219,270評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件缀雳,死亡現(xiàn)場(chǎng)離奇詭異题诵,居然都是意外死亡紧显,警方通過查閱死者的電腦和手機(jī)讲衫,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來鸟妙,“玉大人焦人,你說我怎么就攤上這事挥吵≈馗福” “怎么了?”我有些...
    開封第一講書人閱讀 165,630評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵忽匈,是天一觀的道長(zhǎng)房午。 經(jīng)常有香客問我,道長(zhǎng)丹允,這世上最難降的妖魔是什么郭厌? 我笑而不...
    開封第一講書人閱讀 58,906評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮雕蔽,結(jié)果婚禮上折柠,老公的妹妹穿的比我還像新娘。我一直安慰自己批狐,他們只是感情好扇售,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著嚣艇,像睡著了一般承冰。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上食零,一...
    開封第一講書人閱讀 51,718評(píng)論 1 305
  • 那天困乒,我揣著相機(jī)與錄音,去河邊找鬼贰谣。 笑死娜搂,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的吱抚。 我是一名探鬼主播涌攻,決...
    沈念sama閱讀 40,442評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼频伤!你這毒婦竟也來了恳谎?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,345評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎因痛,沒想到半個(gè)月后婚苹,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,802評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡鸵膏,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評(píng)論 3 337
  • 正文 我和宋清朗相戀三年膊升,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片谭企。...
    茶點(diǎn)故事閱讀 40,117評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡廓译,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出债查,到底是詐尸還是另有隱情非区,我是刑警寧澤,帶...
    沈念sama閱讀 35,810評(píng)論 5 346
  • 正文 年R本政府宣布盹廷,位于F島的核電站征绸,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏俄占。R本人自食惡果不足惜管怠,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望缸榄。 院中可真熱鬧渤弛,春花似錦、人聲如沸甚带。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽欲低。三九已至辕宏,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間砾莱,已是汗流浹背瑞筐。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評(píng)論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留腊瑟,地道東北人聚假。 一個(gè)月前我還...
    沈念sama閱讀 48,377評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像闰非,于是被迫代替她去往敵國和親膘格。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容