Objective-C and Swift, using assistant editor. Nullability

34 views Asked by At

I am currently following Paul Hudson's book Objective-C for Swift developers.

In a book, in chapter Nullability, I create a class:

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface PersonNullabilityCheck : NSObject @property NSString *name;
- (instancetype)initWithName:(NSString*)name;
- (NSString*)fetchGreetingsForTime:(NSString*)time; @end

NS_ASSUME_NONNULL_END

After that, Editor -> Assistant. Finally, Counterparts -> PersonNullabilityCheck.h (Swift 5 interface)

At this point in the book, Paul shows how Swift sees this Objective-C class. And in the book it looks like this:

import Foundation

public class Person: NSObject {
    public var name: String!

    public init!(name:String!)
    public func fetchGreetingForTime(time: String!) -> String!
}

Everything here is implicitly unwrapped.

However, when I do the same, I can see this:

import Foundation

open class PersonNullabilityCheck : NSObject {

    open var name: String

    public init(name: String)

    open func fetchGreetings(forTime time: String) -> String
}

I assume, since the book was written, some new, better techniques were developed to handle nullability?

I would appreciate it if someone could explain to me where the difference is, and how to read it. Many thanks!

1

There are 1 answers

0
Jakub Gawecki On

Okay, I got it, the answer was right there and I haven't noticed it.

NS_ASSUME_NONNULL_BEGIN
NS_ASSUME_NONNULL_END

These lines of code that differ from the version from the book, make every element automatically non-nullable. Therefore I assume I don't have to worry to make a manual check, but if I want to make something non-nullable, I need to add it manually.

Thanks.