Realm断言

作者: 木子F | 来源:发表于2017-02-21 09:33 被阅读89次

    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:].

    相关文章

      网友评论

        本文标题:Realm断言

        本文链接:https://www.haomeiwen.com/subject/awuywttx.html