Parse.com: How to correctly check if a field is empty?

I am asking this in the context of an iOS / Swift app:

When retrieving an object from Parse with a request, I used to validate empty fields by checking against nil. if! = nil .... then do that, etc. etc.

Apparently this is not the correct way to do it, because even if the field is empty in Parse, it is NOT considered nil by swift.

So what to check to determine if a field is empty or not, and for the different forms supported: Strings, Number, Array .....?

Edit: there are answers for objective-c that don't work in swift, where if the value is not boolean, you MUST compare it to nil (which is causing the problem here) or whatever to see if it exists.

+3


source to share


6 answers


So, here's what I found after some investigation:

The string field, when it looks empty in the parse browser, actually contains ". Only when the field has" undefined "in it is zero, which you can compare with === and not ==. Not sure if why, if someone knows ...



Fields other than String cannot contain "". They either contain a value or undefined.

So, if you need to know if a field is null or has no value if the string compares it to the characters "" and === nil and uses === nil if it is not a string.

+2


source


Would you use whereKeyExists:

Checkout Analysis Documentation



If you want to get objects with a specific set of keys, you can use whereKeyExists. Conversely, if you want to retrieve objects without a specific key, you can use whereKeyDoesNotExist.

// Finds objects that have the score set
[query whereKeyExists:@"score"];

      

+1


source


This is what I used to check if the field containing the file is null. As the value can be (undefined) or (null).

func isParseNull(obj : AnyObject!) -> Bool {
    if obj == nil {
        return true
    }
    if obj as NSObject == NSNull() {
        return true
    }
    return false
}

      

+1


source


I'm sure the Parse documentation says! = Nil, but you can try

entity == ""

      

0


source


I would extend the String class and do the following

   extension String {
         func isOnlyEmptySpacesAndNewLineCharacters() -> Bool {
         return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).length == 0
         }
}

      

0


source


Here is my method as I was also trying to check if the string was empty in the parsing and that seems to work fine.

- (void) nameCheck    
{
    //--------GET CURRENT USER-------//
    PFUser *user = [PFUser currentUser];
    //----------CHECK IF PARSE IS EMPTY OR TEXTFIELD IS EMPTY-----------//
    if ([user[@"fullname"]  isEqual: @""] || [fieldName.text isEqual:nil])
    {
        //-IF EMPTY, GIVE USER ANONYMOUS NAME-//
        user[@"fullname"] = @"Anonymous User";
        user[@"fullname_lower"] = @"anonymous user";

        //------------------SAVE NEW NAME IN BACKGROUND----------------//
        [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
         {
             if (error == nil)
             {
                 NSLog(@"Saved Username");
             }
         }];
    }
    //--ELSE USER NAME EXISTS--//
    else
    {
    //--LOAD USER METHOD--//
    [self loadUser];
    NSLog(@"User has username");
    }
}

      

0


source







All Articles