Flipacholas Flipacholas - 2 months ago 176
Swift Question

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

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.

Answer

@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.

Comments