Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-10457] Xcode 10.2/Swift 5.0 init() behavior change => Unexpected type initialized at runtime. #52857

Closed
swift-ci opened this issue Apr 11, 2019 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-10457
Radar None
Original Reporter razvandub (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

Environment

Xcode 10.2/Swift 5.0 Playground

Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee None
Priority Medium

md5: 1e9f0e77208018daa081e47835493bb7

duplicates:

  • SR-10285 Subclass of a class that conforms to a protocol and extends NSObject is not instantiate correctly

Issue Description:

In a nutshell, Xcode 10.2/Swift 5.0 playground initializes the base type at runtime. Adding @objc to the protocol returns the expected subtype at runtime. Xcode 10.1/Swift 4.2.1 returns the subtype at runtime with/without @objc.

// *******************
// *** Playground ***
// *******************

import Foundation

internal protocol Initializable: NSObjectProtocol {
 init()
}

internal class BaseClass: NSObject, Initializable {
 required override init() {
 print("Initializing BaseClass.")
 }
}

internal final class SubClass: BaseClass {
 required init() {
 super.init()
 
 print("Initializing SubClass.")
 }
}

let className = "SubClass"

let classNamePrefix = (NSStringFromClass(BaseClass.self) as NSString).components(separatedBy: ".").first ?? ""

let completeClassName = classNamePrefix + (!classNamePrefix.isEmpty ? ".": "") + className

if let type = NSClassFromString(completeClassName) as? Initializable.Type {
 let instance = type.init()
 
 print("type = '\(type)'. instance = '\(instance)'.")
}

Possibly relates to/duplicates: https://bugs.swift.org/browse/SR-10338 and https://bugs.swift.org/browse/SR-10285.

Attached playground.

Trial runs using varios versions of Xcode/Swift and code below:

*********************************************************************************************
*** Xcode 10.1/Swift 4.2.1 output, without '@objc' before 'internal protocol Initializable': ***
*********************************************************************************************
Initializing BaseClass.
Initializing SubClass.
type = 'SubClass'. instance = '<__lldb_expr_3.SubClass: 0x6000032cc130>'.

 

*********************************************************************************************
*** Xcode 10.1/Swift 4.2.1 output, with '@objc' before 'internal protocol Initializable': ***
*********************************************************************************************
Initializing BaseClass.
Initializing SubClass.
type = 'SubClass'. instance = '<__lldb_expr_1.SubClass: 0x600001c7c2a0>'.

 

********************************************************************************************
*** Xcode 10.2/Swift 5 output, without '@objc' before 'internal protocol Initializable': ***
********************************************************************************************
Initializing BaseClass.
type = 'SubClass'. instance = '<__lldb_expr_1.BaseClass: 0x600003104130>'.

 

*****************************************************************************************
*** Xcode 10.2/Swift 5 output, with '@objc' before 'internal protocol Initializable': ***
*****************************************************************************************
Initializing BaseClass.
Initializing SubClass.
# type = 'SubClass'. instance = '<__lldb_expr_3.SubClass: 0x600001d78110>'.
@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.
Projects
None yet
Development

No branches or pull requests

1 participant