首页 > 在oc里的 (UIImageView *)[tap.view viewWithTag:1]这句在swift中该如何写?

在oc里的 (UIImageView *)[tap.view viewWithTag:1]这句在swift中该如何写?

像我这一句,我想给他用oc里(UIImageView *)这样的写法,在swift里改怎么写呢,找了一遍教程里没有看到类似的使用示例
如果在oc里 我这句话应该是 (UIImageView *)[tap.view viewWithTag:1]; 这样


let imageView : UIImageView = backgroundView.viewWithTag(1) as! UIImageView

不过有空指针危险

guard let view = self.view.viewWithTag(1) where view.isKindOfClass(UIImageView.self) else {
    return
}
        
let imageView = view as! UIImageView

这样会安全一些。
写swift不多,暂时我是这么处理的,期待别人的答案:P


这样写应该可以
let imageView = top.view!.viewWithTag(1);


if let imageView = view.viewWithTag(tag) as? UIImageView {

......

}

guard let imageView = view.viewWithTag(tag) as? UIImageView else {

return

}

...... do something

【热门文章】
【热门文章】