NSObjectProtocol is an unknown type

IDE: Fire
Version: Version 10.0.0.2398 (develop) built on talax, 20190425-095831. Commit ffbeaf4.
Target (If relevant): iOS
Description:
I am trying to extend the UIViewController with a helper to easier instantiate styoboard-based view controller with the commonly used approach like (from www.raywenderlich.com):

import UIKit

protocol StoryboardInstantiable: NSObjectProtocol {
  associatedtype MyType  // 1
  static var defaultFileName: String { get }  // 2
  static func instantiateViewController(_ bundle: Bundle?) -> MyType // 3
}

extension StoryboardInstantiable where Self: UIViewController {
    static var defaultFileName: String {
        return NSStringFromClass(Self.self) as? String
    }

    static func instantiateViewController(_ bundle: Bundle? = nil) -> Self {
        let fileName = defaultFileName
        let sb = UIStoryboard(name: fileName, bundle: bundle)
        return sb.instantiateInitialViewController() as! Self
    }
}

If I try to compile this I am getting the following error:
E: Unknown type "NSObjectProtocol" [/Users/x/Development/Projects/x/x/version-3/config-app/x/Extensions/UIViewController+Storyboard.swift (3)]

Expected Behavior:
The above extension should compile as expected.

Actual Behavior:
An Unknown type error is thrown by the compiler while compiling an iOS application

I am not sure if this issue is related to:

Steps:

  1. Create a new iOS application project
  2. Added the above code snippet in a file e.g. UIViewController+Storyboard.swift
  3. Notice the compiler error

I’ll add NSObjectProtocol as alias to INSObject.

1 Like

Hmm, that makes sense. Looks like that’s the protocol for NSObject :slight_smile: Sweet

1 Like

I have tried this by adding the line I noticed in SwiftBaseLibrary:

#if DARWIN
public typealias NSObjectProtocol = INSObject
public typealias INSObjectProtocol = INSObject
#endif

after adding that the NSObjectProtocol error disappears but then I am getting the error:
E: Cannot assign wrapped nullable to not nullable type (String) [/Users/x/Documents/NSObjectProtocol/UIViewController+Storyboard.swift (16)]
which is line:
return NSStringFromClass(Self.self) as? String and to resolve this compiler error I have updated this line to: return NSStringFromClass(Self.self) as! String

if I compile the project now I am getting the following internal compiler error in the Linking for Simulator-step:

               -> Task Link started for NSObjectProtocol, Toffee-iOS.
                  -> Phase Linking for Simulator started.
                     > ld: Undefined symbols for architecture x86_64:
                     > ld:   "_OBJC_CLASS_$___UIViewController", referenced from:
                     > ld:       objc-class-ref in NSObjectProtocol.a(__$Extension$TypeConstrained-aaa6913d9e6e832d5d1f0784aed517a2.o)
                     > ld:   "_OBJC_CLASS_$_instancetype", referenced from:
                     > ld:       objc-class-ref in NSObjectProtocol.a(__$Extension$TypeConstrained-aaa6913d9e6e832d5d1f0784aed517a2.o)
E:                   ld: symbol(s) not found for architecture x86_64
                  <- Phase Linking for Simulator failed.

I have attached the project
NSObjectProtocol.zip (121.5 KB)

the problem is that static var defaultFileName: String { returns. NON-nullable String, but as? String is nullable. you’ll want as!, yes. so that’s the correct fix. The same would apply to Xcode?

Curious, that does sound like a compiler bug. I’ll log.

Thanks, logged as bugs://82496

Well, actually in Xcode 10.1 you get a similar warning and when you change it to as! you are getting the following error:
Forced cast of 'String' to same type has no effect and suggests to change the line to return NSStringFromClass(Self.self) (which give no errors in both Xcode and Fire) so that’s all good!

Just that compiler bug is stopping us :slight_smile:

hm, weird…

bugs://82496 got closed with status fixed.