DesperateLearner
DesperateLearner

Reputation: 1175

Core data - Check for null - iOS

On entering a new data into my core data for my given entity, how do I check if the entry for a particular attribute is null? I have three attribute name, mail and mailedCustomer. I add data as follows:

SalesPerson *data = (SalesPerson *)[NSEntityDescription         insertNewObjectForEntityForName:@"SalesPerson" inManagedObjectContext:self.managedObjectContext];
[data setName:name];
[data setEmail:userEmail];
 NSLog(@" mailed personel%@",data.mailedCustomer);
if([data.mailedCustomer != nil){
    NSLog(@"inside condition");
    [data setMailedCustomer:@"//"];
}

This doesn't work for me. Im trying to append some strings. So when I enter for the first time I need that attribute to be @"//" then append on further calls.

 NSLog(@" mailed personnel %@",data.mailedCustomer); 

The above NSLog gives me:

  mailed personnel (null)

Upvotes: 0

Views: 2963

Answers (2)

Scott Berrevoets
Scott Berrevoets

Reputation: 16946

If I get what you want, your if statement is incorrect. You're now checking if it's NOT nil (meaning it has some value), and then you're resetting it to //. If you want it to be // and then append values, you have to check if it IS nil and then set it to //:

if (!data.mailedCustomer) {
    NSLog(@"inside condition");
    [data setMailedCustomer:@"//"];
}

Upvotes: 2

Alok Singh
Alok Singh

Reputation: 896

@interface NSObject (PE)
- (BOOL) isNull:(NSObject*) object;
- (BOOL) isNotNull:(NSObject*) object;
@end


@implementation NSObject (PE)

- (BOOL) isNull:(NSObject*) object {
    if (!object)
        return YES;
    else if (object == [NSNull null])
        return YES;
    else if ([object isKindOfClass: [NSString class]]) {
        return ([((NSString*)object) isEqualToString:@""]
                || [((NSString*)object) isEqualToString:@"null"]
                || [((NSString*)object) isEqualToString:@"<null>"]);
    }
    return NO;
}

- (BOOL) isNotNull:(NSObject*) object {
    return ![self isNull:object];
}

@end
















if([self isNotNull:some object])
{
not null
}
else
{
null
}

Upvotes: 1

Related Questions