How to check if an object

I created a class that has several NSS properties as properties. If I have an object of this class, then how can I find out if the object is null (i.e., all NSString properties are null).

My class is as follows

//  MyClass.h
#import <Foundation/Foundation.h>


@interface MyClass : NSObject <NSCoding> {
 NSString *string1;
 NSString *string2;

}
@property (nonatomic, retain) NSString *string1;
@property (nonatomic, retain) NSString *string2;

@end

I test it like this and it does not work.

if (SecondViewController.myObject==nil) {
 NSLog(@"the object is empty");
}
+5
source share
2 answers

If I have an object of this class, then how do I know if the object is null (i.e., all NSString properties are null).

An object is not equal to zero, because all its properties are equal to zero. However, if you want to know if both properties of the string of your object are equal to zero, this will do the trick:

-(BOOL) bothStringsAreNil
{
    return [self string1] == nil && [self string2] == nil;
}

. , , ..

-(BOOL) bothStringsAreNil
{
    return ![self string1]  && ![self string2];
}

.

+7
if (!obj)  
   // obj = nil

if (!obj.property)
   // property = nil

, nil, , .

+3

All Articles