.dynamicType is deprecated. Use 'type(of ...)' instead
@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.
Swift 2 to Swift 2.3 guard let and for errors
1)
(location, response, error) in
guard error == nil else {
print(error!)
return
}
// In this scope `location` and `response` are optional but guaranteed not `nil`
or
(location, response, error) in
guard let loc = location, let resp = response, error == nil else {
print(error!)
return
}
2)
if let item = constraint.firstItem as? UIView, item == containerView {
3)
let bundle = NSBundle(forClass: type(of:self))
4) The Swift way of removeAllSubViews
is (no ugly C-style loops)
func removeAllSubViews() { subViews.forEach{ $0.removeFromSuperview() }
Swift metatype (Type, self)
No, cls
and cls2
are different things. The easiest way to understand the difference will be to extend your example like this:
class SomeClass {
class func doIt() {
print("I'm a class method. I belong to my type.")
}
func doItOnlyIfInstanceOfThisType() {
print("I'm a instance method. I belong to my type instance.")
}
}
And now let's take your cls
:
let cls : SomeClass.Type = SomeClass.self
cls.doIt()
That will print I'm a class method. I belong to my type.
. But you cannot invoke this:
cls.doItOnlyIfInstanceOfThisType() // causes a compilation error, PRO TIP: actually you can use this method as a func property, but I'll add the explanation for this later
Let's take your cls2
. The only visible method of it is doItOnlyIfInstanceOfThisType
because it's an instance method (of this type).
let cls2 : SomeClass = SomeClass()
cls2.doItOnlyIfInstanceOfThisType()
So the difference between them is that cls
is a type and cls2
is an instance of this type.
A little bit more knowledge about why SomeClass.self and SomeClass()?
The type of a class also exists in memory (it has for example its own methods), as a singleton representing the Type (not an instance of this type - that's something different).
If you call self
on a Type like this SomeClass.self
you will get a singleton instance representing the SomeClass
Type.
SomeClass()
invokes the init()
method of SomeClass
, a constructor that creates an instance of SomeClass
.
PRO Tip
You can manipulate a Type instance function (like closures/blocks in ObjC). It's a generated class method. But you must pass an instance of the type that you take this method from as an argument, like this:
let myFunc :()->() = cls.doItOnlyIfInstanceOfThisType(cls2)
myFunc()
Why XCode 7.1 compiles code everytime I write code?
I had the same issue some weeks ago and I finally found hat it's because I used 2 Xcode's windows: one for the storyboard and one for the code. As soon as I stopped using 2 windows, XCode stopped to build my storyboard after every code update.
Note that I'm not talking about tabs but windows.
UIApplication.sharedApplication().setStatusBarStyle() deprecated in iOS 9
I think I have found a solution.
I ended up setting the
View controller-based status bar appearance
boolean to NO
In my info.plist file.
Then I went to my target's General settings -> Deployment info
and changed the dropdown optionStatus Bar Style
to Light
instead of Default
This changed the statusbar style to Light
for my whole application, just what I wanted.
I Hope this helps!
Related Topics
Why Does a Public Class/Struct in Swift Require an Explicit Public Initializer
Convert Swift Dictionary to String
How to Record Video in Realitykit
Speed Up Xcode Swift Build Times
How to Check for 'Nil' in While Loop Condition in Swift
How to Perform a Curl Request in Swift
Why Do Servicesubscribercellularproviders Return Nil? (In iOS 12)
Module File's Deployment Target Is iOS9.0 V9.0 with Xcode 7/Swift 2
Swift: Overriding Didset Results in a Recursion
Sliding One Swiftui View Out from Underneath Another
How to Check If Annotation Is Clustered (Mkmarkerannotationview and Cluster)
Call Methods from Swift Initializer
Understanding @Binding in Swiftui
Storyboard Entry Point Missing