Swift 中这个 Objective-C 安全类型转换表达式的等价物是什么?
What is the equivalent of this Objective-C safe type casting expression in Swift?
这个表达式的等价物是什么:
if ([stream isKindOfClass:[OWTRemoteMixedStream class]]) {
_mixedStream = (OWTRemoteMixedStream *)stream;
}
在 Swift?
if (stream is OWTRemoteMixedStream) {
// mixedStream = stream OWTRemoteMixedStream
}
类型检查运算符确实是Swift中的is
。但是,如果要检查类型并在类型匹配的情况下进行转换,则需要使用可选转换,as?
.
if let mixedStream = stream as? OWTRemoteMixedStream {
// use mixedStream, which has the type OWTRemoteMixedStream
}
这个表达式的等价物是什么:
if ([stream isKindOfClass:[OWTRemoteMixedStream class]]) {
_mixedStream = (OWTRemoteMixedStream *)stream;
}
在 Swift?
if (stream is OWTRemoteMixedStream) {
// mixedStream = stream OWTRemoteMixedStream
}
类型检查运算符确实是Swift中的is
。但是,如果要检查类型并在类型匹配的情况下进行转换,则需要使用可选转换,as?
.
if let mixedStream = stream as? OWTRemoteMixedStream {
// use mixedStream, which has the type OWTRemoteMixedStream
}