我有一个由数组组成的数组AnyObject.我想迭代它,并找到所有元素作为数组实例.
如何在Swift中检查对象是否属于给定类型?
我注意到,我可以将具有常规参数的闭包转换为将其参数包装在元组中的闭包。但是,只有当我使用一种特殊的铸造方法时才可以!
let myClosure = { (a: Int, b: Float) -> Void in
print(a, b)
}
// I want to convert the closure to be of this type (I won't go into why).
var myClosureWithTupleArgVar: (((Int, Float)) -> Void)? = nil
// This cast is possible.
myClosureWithTupleArgVar = (((Int, Float)) -> Void)?(myClosure)
myClosureWithTupleArgVar?((1, 2))
// This cast will always fail and return nil (as warned by the compiler).
myClosureWithTupleArgVar = myClosure as? (((Int, Float)) -> Void)
myClosureWithTupleArgVar?((3, 4))
Run Code Online (Sandbox Code Playgroud)
输出:
1 2.0 …Run Code Online (Sandbox Code Playgroud) 代码如下,在OC中使用[touch.view类]获取对象类型,在Swift 3中如何获取它.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
return NO;
} else {
return YES;
}
}
Run Code Online (Sandbox Code Playgroud)