I am bit confused with typecasting in Swift.
Have a small doubt.
What is the difference between as?,as! and only as.
And can we say "as" is similar to is.
Thanks in advance.
The 'as' keyword is used for casting.
'as' example:
let calcVC = destinationViewController as CalculatorViewController
This line casts the destinationViewController to a CalculatorViewController. However, this would crash if destinationViewController was not a CalculatorViewController or a subclass thereof.
To protect against a crash, you can use 'if let' with 'as?'...
'as?' example:
if let calcVC = destinationViewController as? CalculatorViewController {
// ... write code to execute if destinationViewController is in fact a CalculatorViewController
}
You can even check before you even try to do 'as' with the 'is' keyword...
'is' example:
if destinationViewController is CalculatorViewController {
//...
}
is is used to check the type of a value whereas as is used to cast a value to a different type.
The conditional form, as?, returns an optional value of the type you are trying to downcast to.
Example:
let number: Int = 1
if number as? Int {
print ("Int")
}
You are "asking" if number is of type Int.
Using the as! you're forcing, for example, your let number to be of a certain type, in case it is not, a crash you're going to take!
Example:
if number as! String { //CRASH
}
Answer in simple words ->
as! -> it is used for casting one data type to other data type forcefully(Use this only if you are sure..). We even call it force downcast means we downcast a particular type from superclass to subclass.
as -> This keywprd is used to raise an object to superclass type. So in other words we can say its upcasting.
as? -> So in this case unless you are certain that this cast is going to work then better option to add ? instead of !. Its also downcasting.
Related
I have tried
self.adc_role_id = String(res["adc_role_id"])
self.adc_role_id = "\(res["adc_role_id']"
self.adc_role_id = (\(res["adc_role_id"] as? String)!
but still get
Could not cast value of type '__NSCFNumber' to 'NSString'
I added the dump of res[4] below
As new as I am to Swift, I don't know anything else to try
In Swift 4, the String initializer requires the describing: argument label.
I don't know if this will solve your problem, but your first line of code should be written:
self.adc_role_id = String(describing: res["adc_role_id"])
In your screenshot we can see that res["adc_role_id"] is an NSNumber.
To transform an NSNumber to a String you should use its stringValue property.
And since a dictionary gives an Optional, you should use optional binding to safely unwrap it.
Example:
if let val = res["adc_role_id"] {
self.adc_role_id = val.stringValue
}
You could also, if you want, use string interpolation instead of the property:
if let val = res["adc_role_id"] {
self.adc_role_id = "\(val)"
}
but I think using the property is more relevant.
If for some reason the compiler complains about the type of the content, cast it:
if let val = res["adc_role_id"] as? NSNumber {
self.adc_role_id = val.stringValue
}
Note that you should not use String(describing:) because this initializer will try to represent the string in many ways, and some of them will give inaccurate and unexpected results (for example, if String(describing:) resolves to use the debugDescription property, as explained in the documentation, you may get a totally different string than the one you want).
It's also worth noting that using String(describing:) with an optional value such as your dictionary will resolve to a wrong string: String(describing: res["adc_role_id"]) will give Optional(yourNumber)! This is why Mike's answer is wrong. Be careful about this. My advice is to avoid using String(describing:) altogether unless for debugging purposes.
The error message is clear and the dump is clear, too.
The value is not a String, it's an Int(64) wrapped in NSNumber
Optional bind the value directly to Int (NSNumber is implicit bridged to Int) and use the String initializer.
if let roleID = res["adc_role_id"] as? Int {
self.adc_role_id = String(roleID)
}
Please conform to the naming convention that variable names are camelCased rather than snake_cased
Here is my swift 2.X code that does'nt work any more on swift 3 :
var dictThemesNamesStyles=[String:[Int:Int]]()
self.styles=dictThemesNamesStyles
let keysArray:Array=Array(self.styles.keys)
let sortedKeysArray = keysArray.sorted(by:
{
(str1: NSObject, str2: NSObject) -> Bool in
return Int((str1 as! String))<Int((str2 as! String))
})
self.stylesLevel1Keys=sortedKeysArray
self.styleThemesPickerView.reloadAllComponents()
On line :
"return Int((str1 as! String)) < Int((str2 as! String))"
it complains with the error : "Type of expression is ambiguous without more context"
What do I have to change in this code to make it work ?
Thanks a lot.
Let's go through this line by line:
var dictThemesNamesStyles=[String:[Int:Int]]()
self.styles=dictThemesNamesStyles
Okay, we've got a dictionary of strings to dictionaries of integers.
let keysArray:Array=Array(self.styles.keys)
There are a few problems with this line:
The declaration of :Array without a generic parameter.
The type declaration is unnecessary, since the type system already knows this is an Array, since you're calling Array's initializer.
Creating this whole Array is unnecessary, since we're just passing the result to sorted, which already exists on the collection returned by keys, and which will return an Array. Creating the array is therefore a needless performance hit which we should avoid.
I would, in fact, delete this entire line, and just replace keysArray with self.styles.keys in the next line:
let sortedKeysArray = self.styles.keys.sorted(by:
Next:
{
(str1: NSObject, str2: NSObject) -> Bool in
return Int((str1 as! String))<Int((str2 as! String))
Okay, we've got a few problems here.
str1 and str2 are declared as NSObject, when they are in fact Strings.
Consequently, the as! String casts are unnecessary.
Int(String) returns an Optional, so you need to take into account the case where the result may be nil. I'd just provide a default value; probably 0, although you could also use a guard statement to throw an error if you prefer.
In general, there's a lot of verbosity here. This whole closure can actually be succinctly written as a one-liner:
let sortedKeysArray = self.styles.keys.sorted { (Int($0) ?? 0) < (Int($1) ?? 0) }
Anyway, take care of these issues, and your code will compile.
That all seems pretty complex. You can simplify this quite a bit:
self.styles = [String: [Int: Int]]()
self.stylesLevel1Keys= self.styles.keys.sorted { Int($0)! < Int($1)! }
self.styleThemesPickerView.reloadAllComponents()
I'm trying to understand of as typecasting.
Reading Type Casting chapter on Apple Documentation, I've two syntax for downcasting (as? and as! operators) but I didn't find anything about as.
So I thought that I should never have used this kink of operator but yesterday when I was typing code with do-try-catch statement, I met this one:
catch let error as NSError {
print(error)
}
Initially, error was type conforms to the Error protocol.
Now using the as NSError casting, it has become an instance of a NSError class.
But my question is: What does as operator do?
It's not a downcasting sure.. Could it be used for "convert" object?
EDIT
I don't think it's a duplicate.
In my case, the error variable, is not a class and doesn't inherit from a super class so I have not a upcasting. It is not even a pattern matching.
I've already read both Swift Blog page and this thread on StackOverflow.
EDIT 2
From Swift Blog
Swift 1.2 separates the notions of guaranteed conversion and forced
conversion into two distinct operators. Guaranteed conversion is still
performed with the as operator, but forced conversion now uses the as!
operator. The ! is meant to indicate that the conversion may fail.
This way, you know at a glance which conversions may cause the program
to crash.
The text above doesn't work for me because if I tried to use the as! operator instead of as, compiler complain me.
EDIT 3
Even in Using Swift with Cocoa and Obj-C documentation they use the let-as? syntax for checking and casting to a protocol.
So, why in my case, I can't use it?
First of all, as suggested in the dasblinkenlight's comment, your code snippet is not using a type-casting-operator. Check the syntax of do-statement and you can find these:
catch-clause → catch patternopt where-clauseopt code-block
pattern → value-binding-pattern
value-binding-pattern → var pattern | let pattern
pattern → type-casting-pattern
type-casting-pattern → is-pattern | as-pattern
as-pattern → pattern as type
So, your EDIT 2 has no meaning, there are no syntax accepting as! in catch-clause.
But this code (using type-casting-operator) works, so I try to explain how to use as-casting.
enum MyError: Error {
case bad
//...
}
let error: Error = MyError.bad
let nsError = error as NSError
As shown in the linked article in the EI Captain v2.0's comment, as-casting is used for Guaranteed conversion. I have collected some use cases of such conversions.
Upcasting
class Animal {}
class Dog: Animal {}
let d = Dog()
d as Animal // upcast succeeds
As shown in the article, upcasting always succeeds, so you can use as.
Specifying literal type
let byte = 123 as UInt8
let ch = "a" as UnicodeScalar
In Swift, literals are typeless, so you can use as to specify the types of literals
In case Swift can infer the type of the literal, you can omit such as-casting:
let byte: UInt8 = 123
let ch: UnicodeScalar = "a"
Disambiguating overloaded methods
class MyClass {
func aMethod(_ arg: String) {
print(arg)
}
func aMethod(_ arg: Int) {
print("\"\(arg)\"")
}
}
let obj = MyClass()
let theFunc = obj.aMethod as (String)->Void
theFunc("abc") //->abc
Always-succeeds bridging
let str: String = "a String"
let nsStr = str as NSString
let intArr: [Int] = [1,2,3]
let nsArr = intArr as NSArray
The example let nsError = error as NSError is included in this category, and you need to read this article carefully, to understand why this is an always-succeeds bridging.
For your EDIT 3.
You may need to distinguish these two syntaxes:
let a: Any = 1
//Using if-let -- Optional binding, `(a as? Int)` is an expression using type-casting-operator which generates an Optional result
if let intA = (a as? Int) {
print("\(intA) is Int")
}
//Using if-case -- pattern matching, `(let intA as Int)` is a pattern using as-pattern
if case (let intA as Int) = a {
print("\(intA) is Int")
}
As already noted, catch leads a pattern, and you cannot use as? in pattern.
as
Use as for types that Apple has done some work to handle the conversion in the background. These are usually Foundation types that Apple bridged into Swift and want you to have a quick way to convert back and forth from their ObjC equivalents, for example:
String <-> NSString
URL <-> NSURL
Array <-> NSArray
Data <-> NSData
These casts always succeed and Xcode will warn you if you use as? or as!. In your specific case, Apple has done some meddling in the background to make the Error protocol and NSError to be castable to/from each other.
as!
Use as! when you know the object is castable to another type. as! will crash your app if the object is not castable (for example, when sender is actually a UITextField)
let button = sender as! UIButton // you are sure that the sender is always
// a UIButton or your app will crash
as?
Use as? when you not sure if the object is castable to the other type. In practice, this should be your preferred method and you should optional binding to check for success. as? produces nil if the object is not castable:
// Exit the function if the sender is not a UIButton
guard let sender = sender as? UIButton else {
return
}
// Only execute the block if the sender is UIButton
if let button = sender as? UIButton {
// ...
}
When looping though a an array of UICollectionViewCells in UICollectionView.visibleCells() then checking if the cell conforms to a protocol, it forgets that the cell is a UIView and has a frame property.
for cell in collectionView.visibleCells() {
let cellPosition1 = cell.frame.origin
if let cell = cell as? AwesomeCellProtocol {
let cellPosition2 = cell.frame.origin
cell.doAwesome(cellPosition)
}
}
Swift give a compile error on setting cellPosition2:
Value of type 'AwesomeCellProtocol' has no member 'frame'
Setting cellPosition1 works fine.
Can I check for both UIView and AwesomeCellProtocol?
With
if let cell = cell as? AwesomeCellProtocol { ... }
you introduce a new variable cell for the scope of the if-block,
which "shadows" the cell variable from the outer scope of the for-loop.
That local variable has the type AwesomeCellProtocol and not
UICollectionViewCell.
You can avoid that problem by binding to a different name:
if let awesomeCell = cell as? AwesomeCellProtocol {
let cellPosition2 = cell.frame.origin
awesomeCell.doAwesome(cellPosition)
}
When you unwrap the optional, you are correct that it is no longer the Type you expect, but rather objectWithAwesomeCellProtocol .
If you want to keep it as a UICollectionViewCell instead, you could try using is instead of as?, but then you'd need to handle nil cases yourself.
From Apple's Swift Programming Language Guide under "Checking for Protocol Conformance"
You can use the is and as operators described in Type Casting to check for protocol conformance, and to cast to a specific protocol.
The is operator returns true if an instance conforms to a protocol and returns false if it does not.
The as? version of the downcast operator returns an optional value of the protocol’s type, and this value is nil if the instance does not conform to that protocol.
Note that the underlying objects are not changed by the casting process ... However, at the point that they are stored in the [unwrapped optional] constant, they are only known to be of type [protocol], and so only their [protocol-specific] property can be accessed.
Recently I stumbled upon a syntax I cannot find a reference to: What does as mean in the Swift syntax?
Like in:
var touch = touches.anyObject() as UITouch!
Unfortunately, it's hard to search for a word like as, so I didn't find it in the Swift Programming Language handbook by Apple. Maybe someone can guide me to the right passage?
And why does the element after as always have an ! to denote to unwrap an Optional?
Thanks!
The as keyword is used for casting an object as another type of object. For this to work, the class must be convertible to that type.
For example, this works:
let myInt: Int = 0.5 as Int // Double is convertible to Int
This, however, doesn't:
let myStr String = 0.5 as String // Double is not convertible to String
You can also perform optional casting (commonly used in if-let statements) with the ? operator:
if let myStr: String = myDict.valueForKey("theString") as? String {
// Successful cast
} else {
// Unsuccessful cast
}
In your case, touches is (I'm assuming from the anyObject() call) an NSSet. Because NSSet.anyObject() returns an AnyObject?, you have to cast the result as a UITouch to be able to use it.
In that example, if anyObject() returns nil, the app will crash, because you are forcing a cast to UITouch! (explicitly unwrapping). A safer way would be something like this:
if let touch: UITouch = touches.anyObject() as? UITouch {
// Continue
}
A constant or variable of a certain class type may actually refer to
an instance of a subclass behind the scenes. Where you believe this is
the case, you can try to downcast to the subclass type with the type
cast operator (as).
from Swift Programming Language, Type Casting
And why does the element after as always have an ! to denote to unwrap an Optional?
It is not. It is trying to downcast to "Implicitly Unwrapped Optionals", see Swift Programming Language, Types
as is an operator that cast a value to a different type.
For example:
Suppose you have an NSSet instance with some elements that have a type Car.
Then if you want to get any object:Car from this set, you should call anyObject().
var someCar = set.anyObject() //here someCar is Optional with type AnyObject (:AnyObject?), because anyObject() -> AnyObject?
Let's imagine the situation when you need to get an object from the set with type Car.
var realCar: Car = someCar as Car //here realCar is Optional with type Car (:Car?)
Than if you exactly know that someCar is not an Optional ( someCar != nil) you can do follow:
var realCarAndNotAnOptional = someCar as Car! //here realCarAndNotAnOptional just have a type == Car
More info here: Swift: Type Casting