Swift语法中的"as"有什么作用?
What function does "as" in the Swift syntax have?
最近我偶然发现了一个我找不到参考的语法:What does as mean in the Swift syntax?
点赞:
var touch = touches.anyObject() as UITouch!
不幸的是,很难搜索到像 as 这样的词,所以我没有在 Apple 的 Swift 编程语言手册中找到它。也许有人可以引导我到正确的段落?
为什么 as 之后的元素总是有一个 ! 来表示展开一个 Optional?
谢谢!
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
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).
来自 Swift Programming Language, Type Casting
And why does the element after as always have an ! to denote to unwrap an Optional?
不是。它正在尝试向下转换为 "Implicitly Unwrapped Optionals",请参阅 Swift Programming Language, Types
as
keyword 用于将一个对象转换为另一种类型的对象。为此,class 必须 可转换为该类型。
例如,这个有效:
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()
returns 和 AnyObject?
,您必须将结果转换为 UITouch
才能使用它。
在该示例中,如果 anyObject()
returns 为零,应用程序将崩溃,因为您强制转换为 UITouch!
(显式展开)。更安全的方法是这样的:
if let touch: UITouch = touches.anyObject() as? UITouch {
// Continue
}
最近我偶然发现了一个我找不到参考的语法:What does as mean in the Swift syntax?
点赞:
var touch = touches.anyObject() as UITouch!
不幸的是,很难搜索到像 as 这样的词,所以我没有在 Apple 的 Swift 编程语言手册中找到它。也许有人可以引导我到正确的段落?
为什么 as 之后的元素总是有一个 ! 来表示展开一个 Optional?
谢谢!
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
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).
来自 Swift Programming Language, Type Casting
And why does the element after as always have an ! to denote to unwrap an Optional?
不是。它正在尝试向下转换为 "Implicitly Unwrapped Optionals",请参阅 Swift Programming Language, Types
as
keyword 用于将一个对象转换为另一种类型的对象。为此,class 必须 可转换为该类型。
例如,这个有效:
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()
returns 和 AnyObject?
,您必须将结果转换为 UITouch
才能使用它。
在该示例中,如果 anyObject()
returns 为零,应用程序将崩溃,因为您强制转换为 UITouch!
(显式展开)。更安全的方法是这样的:
if let touch: UITouch = touches.anyObject() as? UITouch {
// Continue
}