Home Ask Login Register

Developers Planet

Your answer is one click away!

Stefano Vuotto César February 2016

Comparing a RLMArray with NSString

Is there a way to compare RLMArray with NSString?

I want to compare the textfield.text with the stored usernames of RLMArray.

Thanks for the help! :)


-(void)checkUser {
    RLMArray<User *> *userObjects;
            RLMResults *matches = [userObjects objectsWhere:@"name CONTAINS %@", _userName];

    if (matches) {
    else {


NeilAppleDeveloper February 2016

RLMArray is the container type in Realm used to define to-many relationships.

Unlike an NSArray, RLMArrays hold a single type, specified by the objectClassName property.

In order to compare, you have to have a string object from the RLMArray. There are many ways of doing this. For example, if you want to compare the first object of the array with textfield.text:
if ([[YOURARRAY firstObject] isEqualtoString:textfield.text]){ //do something }
You can also compare the second object in the array, do something like this:
if ([[YOURARRAY objectAtIndex:1] isEqualtoString:textfield.text]){ //do something }
This is the documentation for realm: https://realm.io/docs/objc/latest/api/index.html

TiM February 2016

If you've got an RLMArray of User objects (Since NSStrings themselves can't be directly saved to RLMArray), and you wish to see if any of them match the NSString value of a UITextField, you should be able to do this really easily with an NSPredicate query:

@interface User
@property NSString *username;

NSString *textFieldString = textfield.text;
RLMArray<User *> *userObjects = .../

RLMResults *matches = [userObjects objectsWhere:@"username CONTAINS '%@'", textFieldString];

For more info on NSPredicate filtering, Realm also maintains a list of the syntax you can use: https://realm.io/news/nspredicate-cheatsheet/

Good luck!

Post Status

Asked in February 2016
Viewed 2,361 times
Voted 9
Answered 2 times


Leave an answer

Quote of the day: live life