1

I have a model class object which has the following properties

@property (nonatomic, strong) NSString *place;
@property (nonatomic, strong) NSString *date;

I am setting these properties from a controller class object.

I want to perform a null check of my model class object properties. So I want to write a for loop like following.

for (property in modelObject)
{   
 if (object  == [NSNull null])//object is a property of modelobject
 {  
   //the next two lines wont be true but need to check that 
   if([property isKindOfClass:[NSString Class]]) property = @"no place";
   if([property isKindOfClass:[NSDate Class]]) property = @"No date;
 }
}

My question is,

If the model class object property is set to null how can I check if the property is null and also the declaration type of that property?

Directly checking the two properties instead of looping through the properties won't be helpful because in the actual scenario there are lot of properties with different types for the model class.

Thanks in advance.

jjpp
  • 1,298
  • 1
  • 15
  • 31
  • you can use Array of properties – Arslan Asim Sep 01 '15 at 13:19
  • @ArslanAsim Then how will I check Null and declaration type? – jjpp Sep 01 '15 at 13:21
  • 1
    Are `object` and `modelObject` supposed to be two different things? – Phillip Mills Sep 01 '15 at 13:25
  • as far as null is concern you you can check, [obj isKindOfClass:[NSNull class]] – Arslan Asim Sep 01 '15 at 13:26
  • refer [looping through all properties](http://stackoverflow.com/questions/9269372/loop-through-all-object-properties-at-runtime) or in detail [runtime guide](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html). Both has examples to loop through properties. – Akhilrajtr Sep 01 '15 at 13:37
  • 2
    1. Do you really want to test against `[NSNull null]` instead of `nil`? 2. Do you really want to change the content of your model to display a "special" state? – Amin Negm-Awad Sep 01 '15 at 13:48
  • always use copy instead of strong for NSString properties – some_id Sep 01 '15 at 13:53
  • @PhillipMills object is a property of modelobject. I've edited the question. Thanks for pointing it out. – jjpp Sep 02 '15 at 05:04
  • @AminNegm-Awad Yes I need to check against [NSNull null]. Not against nil. – jjpp Sep 02 '15 at 05:05
  • @jipp You have to think about that again. `[NSNull null`] is an instance of a *different* type. The usage should be restricted to type-agnostic collections. I. e. `[NSNull null]` does not have a "message to nil" behavior. – Amin Negm-Awad Sep 02 '15 at 09:20

2 Answers2

3

Add a method in Model Class

- (void)nullCheck {
    unsigned int outCount, i;
    objc_property_t *properties = class_copyPropertyList([self class], &outCount);
    for (i = 0; i < outCount; i++) {
        objc_property_t property = properties[i];
        NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property)];
        id propertyValue = [self valueForKey:(NSString *)propertyName]; //check propertyValue here
        //set property value here 
        [self setValue:@"some value" forKey:(NSString *)propertyName];
        const char * type = property_getAttributes(property);
        NSString *attr = [NSString stringWithCString:type encoding:NSUTF8StringEncoding];

        NSString * typeString = [NSString stringWithUTF8String:type];
        NSArray * attributes = [typeString componentsSeparatedByString:@","];
        NSString * typeAttribute = [attributes objectAtIndex:0];
        NSString * propertyType = [typeAttribute substringFromIndex:1];
    }
    free(properties);
}
Akhilrajtr
  • 5,170
  • 3
  • 19
  • 30
0

First, you cant do this to check the properties on the modelObject view controller, if I understand correctly that that is what you want to do :

for (property in modelObject)

What this would do is iterate through an array called modelObject. So if you had first added your properties to that array, that would be fine. There is no simple way to iterate through every property that any class has - you just wouldnt want to do that anyway. You first need to either add them to an array for reference, or refer to them in some other collection type.

Second, you can quickly check if a property remains nil - ie it has never been used, like this :

if (object) {
}

This will return true if the object is not 'nil'. You dont have to use [NSNull null] for what you are trying to achieve. So you could instead do this :

if (!place) {
place = @"no place";
}

if (!date) {
date = @"no date";
}
Luke Smith
  • 1,218
  • 12
  • 17
  • I need to check against [NSnull null] because I'm setting the values from a url response using third party classes which may set the properties to null. – jjpp Sep 02 '15 at 05:10