0

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;
}
Saad
  • 17
  • 4
  • 2
    This needs further context: Why do you need/want these fields to be nullable? What is being modelled here? – Williham Totland Apr 07 '21 at 18:09
  • Objective C doesn't have optionals the way that Swift does. You can't make a value type null. You would need to implement what Swift does under the covers by creating struct that has the wrapped value (a bool or Int) and a Boolean that indicates whether there is a value or not. – Paulw11 Apr 07 '21 at 21:29

4 Answers4

1

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;
Tommy
  • 99,986
  • 12
  • 185
  • 204
Paulw11
  • 108,386
  • 14
  • 159
  • 186
1

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".

Gereon
  • 17,258
  • 4
  • 42
  • 73
1

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

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>.

Glenn Posadas
  • 12,555
  • 6
  • 54
  • 95