What function does the "how" do in Swift syntax?

I recently came across a syntax, I can not find a link to: What does it mean like in Swift syntax?

How in:

var touch = touches.anyObject() as UITouch!

Unfortunately, it’s hard to find a word like how , so I did not find it in Apple’s Swift Programming Language Reference. Maybe someone can direct me to the right passage?

And why the item after as always has ! to deploy optional?

Thank!

+4
source share
3 answers

as keyword . , .

, :

let myInt: Int = 0.5 as Int // Double is convertible to Int

, , :

let myStr String = 0.5 as String // Double is not convertible to String

( if-let) ?:

if let myStr: String = myDict.valueForKey("theString") as? String {
    // Successful cast
} else {
    // Unsuccessful cast
}

touches ( anyObject()) NSSet. NSSet.anyObject() AnyObject?, UITouch, .

, anyObject() nil, , UITouch! ( ). :

if let touch: UITouch = touches.anyObject() as? UITouch {
  // Continue
}
+5

as - , .

:

, NSSet , Car. , : Car , anyObject().

var someCar = set.anyObject() //here someCar is Optional with type AnyObject (:AnyObject?), because anyObject() -> AnyObject?

, Car.

var realCar: Car = someCar as Car //here realCar is Optional with type Car (:Car?)

, someCar (someCar!= nil), :

var realCarAndNotAnOptional = someCar as Car! //here realCarAndNotAnOptional just have a type == Car

: Swift: Type Casting

+2

. , , (as).

from ,

! ?

. " ", . Swift,

+2

All Articles