Saad
Saad

Reputation: 17

how to make thie non-pointer type property nullable in objc

hi iam learning IOS development & for learning porpoises
i want to know how to make this property nullable in objc (this Four @property)

@property (nonatomic) BOOL Hood;
@property (nonatomic) BOOL smartclean;
@property (nonatomic) t_ShirtSize size;
@property (nonatomic) NSUInteger newsoftness;

because when i use nullable i get this Error

@property (nullable,nonatomic) BOOL  Hood;

Nullability specifier 'nullable' cannot be applied to non-pointer type 'BOOL' (aka 'signed char')

what is the Right way to make all 4 property nullable how can i do that (accept Null)

this is M file for The My app

-(instancetype _Nonnull) initWithSize:(t_ShirtSize)size
                         Hood: (BOOL)hoody {
    
    self = [super init];
    if(self) {
        Hood = hoody;
        _size = size;
    }
    
    return self;
}

Upvotes: 0

Views: 1685

Answers (4)

Paulw11
Paulw11

Reputation: 114773

The nullable annotation helps Swift understand whether a reference is optional or not when Objective-C code interfaces with Swift. It doesn't change the operation of Objective-C in any way.

nil can be assigned to a pointer in Objective-C even if it doesn't have a nullability annotation; that is just the nature of C pointers.

Scalar value types (Integers, bools etc) cannot be null in Objective-C, they always have a value since there is no pointer to their value. Their value is stored directly.

There is no direct Objective-C support for optional or nullable value types as there is in Swift

In Swift, an optional is actually a struct containing the wrapped value and a property that indicates whether the optional has a value or not. The compiler hides this detail from you.

You could use NSNumber. This wraps scalar value types in an object. The object reference can be nil and so is nullable

@property (nonatomic, nullable) NSNumber* Hood;
@property (nonatomic, nullable) NSNumber* smartclean;
@property (nonatomic) t_ShirtSize size;
@property (nonatomic, nullable) NSNumber* newsoftness;

Upvotes: 1

matt
matt

Reputation: 534885

A BOOL is a primitive, not an object. If you need an object, the usual thing is to wrap your BOOL in an NSNumber:

https://developer.apple.com/documentation/foundation/nsnumber/1415728-initwithbool?language=objc

https://developer.apple.com/documentation/foundation/nsnumber/1410865-boolvalue?language=objc

Now you have an NSNumber, so you have something that can be nil to indicate there is no object there.

Upvotes: 1

Gereon
Gereon

Reputation: 17844

If you absolutely need a nullable BOOL, you can use NSNumber as a wrapper:

@property (nullable,nonatomic) NSNumber* Hood;

use Hood = @YES; /* or @NO */ to assign a value, and Hood.boolValue to read the value again.

Note that, as others have said already, this is not a full replacement for Swift's optionals, and you have to take extra care to always use the correct data type - the compiler won't help you if you accidentally store a floating point value in your "nullable NSUInteger".

Upvotes: 1

Glenn Posadas
Glenn Posadas

Reputation: 13283

You don't need that nullability specifier anymore with BOOL properties, and perhaps also for the NSUInteger.

For BOOL, the reason why you don't need it is that it's by default 0 or <nil>.

Upvotes: 0

Related Questions