.dynamicType is deprecated. Use 'type(of ...)' instead
Asked Answered
P

2

33

I've just updated to Xcode 8 and iOS 10 (using legacy Swift Language Version).

Trying to compile again my project has been an agony, even still using the old Swift syntax. This time one of my functions uses NSBundle(forClass: self.dynamicType) but now appears that .dynamicType is deprecated and Xcode doesn't even want to compile it.

His suggestion is to use type(of: self) but that fails as well. Anyone knows the solution? Thanks.

Plumose answered 14/9, 2016 at 16:4 Comment(3)
NSBundle(forClass: self.dynamicType) compiles fine for me in Swift 2.3, are you sure "Use Legacy Swift Language Version" is set to "Yes"? You could always try cleaning your build folder.Richart
Yes, it's set and I can see the effect because the number of syntax errors decreases from 30 to 1. My opinion is that this error is related to the new UIKit framework updated in the new iOS 10 SDK, that's why when I updated to Xcode 8 it appeared even activating the legacy Swift syntax.Plumose
Never mind my last statement, UIKit is still the samePlumose
P
3

@dfri answer works perfectly for Swift 3.

Regarding Swift 2.3, my solution was to clean Xcode (Command+Option+Shift+K) and delete everything in ~/Library/Developer/Xcode/DerivedData. The problem doesn't disappear instantly but after some time it will stop giving that error.

Maybe it's a bug, after all we are in 8.0. I hope it gets fixed in next releases.

Thank you everyone.

Plumose answered 22/9, 2016 at 9:25 Comment(1)
@sgonzalez His answer works for Swift 3. I was using legacy Swift (2).Plumose
E
35

(The below holds for Swift 3; not legacy Swift Language Version (2.3), however, so it doesn't answer the OP's question, but could be valuable for Swift 3 users, nonetheless)

As noted in your question, dynamicType is now (Swift 3) deprecated in favour of type(of:). In addition:

  • NSBundle has been renamed to Bundle.
  • The init(forClass:) initializer of Bundle has been renamed to init(for:).

Taking these changes into account, For Swift 3 you initialize (or fetch an existing instance associated with the specific class) your Bundle object in the following manner:

class Foo {
    func bar() -> () {
        let bundle = Bundle(for: type(of: self))
        // ...
    }
}
Eligible answered 14/9, 2016 at 16:31 Comment(4)
OP is using the legacy Swift version, aka Swift 2.3 – so AFAIK his current code should be validRichart
No problem, I thought the same at first glance!Richart
What's type(of: self) ? Where does it come from? Autocomplete doesn't suggest me such code. Also, looking at documentation can't seem to find anything related to type(of: ). But copy pasting the exact same code doesn't yield any errors. Maybe I should restart Xcode.Ancylostomiasis
@Ancylostomiasis See the release notes for Xcode 8: "The dynamicType keyword has been removed from Swift. In its place a new primitive function type(of:) has been added to the language. Existing code that uses the .dynamicType member to retrieve the type of an expression should migrate to this new primitive.". If you attempt to use dynamicType in Swift 3/Xcode 8, you should be promted with similar information (-> replace with type(of:)).Eligible
P
3

@dfri answer works perfectly for Swift 3.

Regarding Swift 2.3, my solution was to clean Xcode (Command+Option+Shift+K) and delete everything in ~/Library/Developer/Xcode/DerivedData. The problem doesn't disappear instantly but after some time it will stop giving that error.

Maybe it's a bug, after all we are in 8.0. I hope it gets fixed in next releases.

Thank you everyone.

Plumose answered 22/9, 2016 at 9:25 Comment(1)
@sgonzalez His answer works for Swift 3. I was using legacy Swift (2).Plumose

© 2022 - 2024 — McMap. All rights reserved.