Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.dynamicType is deprecated. Use 'type(of ...)' instead

Tags:

xcode

swift

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.

like image 843
Rodrigo Avatar asked Sep 14 '16 16:09

Rodrigo


1 Answers

(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))         // ...     } } 
like image 119
dfrib Avatar answered Oct 05 '22 19:10

dfrib