objective-c-nullability

Difference between nullable, __nullable and _Nullable in Objective-C

£可爱£侵袭症+ 提交于 2020-01-10 06:13:27
问题 With Xcode 6.3 there were new annotations introduced for better expressing the intention of API's in Objective-C (and to ensure better Swift support of course). Those annotations were of course nonnull , nullable and null_unspecified . But with Xcode 7, there is a lot of warnings appearing such as: Pointer is missing a nullability type specifier (_Nonnull, _Nullable or _Null_unspecified). In addition to that, Apple uses another type of nullability specifiers, marking their C code (source):

NSError, Swift, and nullability

♀尐吖头ヾ 提交于 2019-12-31 10:03:49
问题 I'm writing a set of tools in Objective-C that will be used by Swift at some point so I'm using generics and nullability. What am I supposed to do in this situation? - (NSArray<MyObj *> * __nullable)foo:(NSError **)error; Currently I'm getting a warning: Pointer is missing a nullability type specifier... for both pointers! I'm almost certain that I'm NOT to supposed to do: - (NSArray<MyObj *> * __nullable)foo:(NSError * __autoreleasing __nullable * __nullable)error; Am I? 回答1: The Swift blog

Pointer is missing a nullability type specifier

谁说胖子不能爱 提交于 2019-12-20 08:46:32
问题 In Xcode 7 GM I started to get this warning: Pointer is missing a nullability type specifier (_Nonnull, _Nullable, or _Null_unspecified) In the following function declaration (NSUserDefaults extension) - (void)setObject:(nullable id)value forKey:(NSString *)defaultName objectChanged:(void(^)(NSUserDefaults *userDefaults, id value))changeHandler objectRamains:(void(^)(NSUserDefaults *userDefaults, id value))remainHandler; Why this warning is showing and how should I fix it? 回答1: You need to

Can we ensure nullability of `+ (nonnull instancetype)sharedInstance;`?

流过昼夜 提交于 2019-12-07 03:34:03
问题 This is a question on how to gracefully circumvent the nullability of init in NSObject class. So here is a classic objective-c implementation: + (instancetype)sharedInstance { static dispatch_once_t onceToken; static id sharedInstance; dispatch_once(&onceToken, ^ { sharedInstance = [[self alloc] init]; }); return sharedInstance; } But now I want to declare it as nonnull , if possible: + (nonnull instancetype)sharedInstance; Unfortunately, init returns a nullable instancetype value. Should I

Can we ensure nullability of `+ (nonnull instancetype)sharedInstance;`?

与世无争的帅哥 提交于 2019-12-05 09:37:10
This is a question on how to gracefully circumvent the nullability of init in NSObject class. So here is a classic objective-c implementation: + (instancetype)sharedInstance { static dispatch_once_t onceToken; static id sharedInstance; dispatch_once(&onceToken, ^ { sharedInstance = [[self alloc] init]; }); return sharedInstance; } But now I want to declare it as nonnull , if possible: + (nonnull instancetype)sharedInstance; Unfortunately, init returns a nullable instancetype value. Should I add an NSAssert or something after calling init ? I noticed that some people even document nonnull

Pointer is missing a nullability type specifier

≯℡__Kan透↙ 提交于 2019-12-02 17:50:24
In Xcode 7 GM I started to get this warning: Pointer is missing a nullability type specifier (_Nonnull, _Nullable, or _Null_unspecified) In the following function declaration (NSUserDefaults extension) - (void)setObject:(nullable id)value forKey:(NSString *)defaultName objectChanged:(void(^)(NSUserDefaults *userDefaults, id value))changeHandler objectRamains:(void(^)(NSUserDefaults *userDefaults, id value))remainHandler; Why this warning is showing and how should I fix it? You need to specify nullable also for the handlers/blocks - (void)setObject:(nullable id)value forKey:(nonnull NSString *

Difference between nullable, __nullable and _Nullable in Objective-C

放肆的年华 提交于 2019-11-29 18:35:01
With Xcode 6.3 there were new annotations introduced for better expressing the intention of API's in Objective-C (and to ensure better Swift support of course). Those annotations were of course nonnull , nullable and null_unspecified . But with Xcode 7, there is a lot of warnings appearing such as: Pointer is missing a nullability type specifier (_Nonnull, _Nullable or _Null_unspecified). In addition to that, Apple uses another type of nullability specifiers, marking their C code ( source ): CFArrayRef __nonnull CFArrayCreate(
CFAllocatorRef __nullable allocator, const void * __nonnull * _

How to create class methods that conform to a protocol shared between Swift and Objective-C?

断了今生、忘了曾经 提交于 2019-11-26 23:04:53
I've been learning Swift lately. I decided to write a hybrid Swift/Objective-C app that did compute-intensive tasks using the same algorithm implemented in both languages. The program calculates a large array of prime numbers. I defined a protocol that both the Swift and the Objective-C version of the calculate object are supposed to conform to. The objects are both singletons, so I created a typical singleton access method in Objective-C: + (NSObject <CalcPrimesProtocol> *) sharedInstance; The whole protocol looks like this: #import <Foundation/Foundation.h> @class ComputeRecord; typedef void

How to create class methods that conform to a protocol shared between Swift and Objective-C?

一曲冷凌霜 提交于 2019-11-26 08:35:11
问题 I\'ve been learning Swift lately. I decided to write a hybrid Swift/Objective-C app that did compute-intensive tasks using the same algorithm implemented in both languages. The program calculates a large array of prime numbers. I defined a protocol that both the Swift and the Objective-C version of the calculate object are supposed to conform to. The objects are both singletons, so I created a typical singleton access method in Objective-C: + (NSObject <CalcPrimesProtocol> *) sharedInstance;