Ios 快速检查对象';财产存在

Ios 快速检查对象';财产存在,ios,iphone,xcode,swift,Ios,Iphone,Xcode,Swift,我有下面的UINib扩展方法,我想知道是否可以为未归档视图设置委托 public class func decodeView<T:UIView>(nibName name:String,className classType:T.Type,delegate:AnyObject) -> T { let nib = UINib(nibName: name) let topLevelObjects = nib.instantiateWithOwner(nil

我有下面的UINib扩展方法,我想知道是否可以为未归档视图设置委托

    public class func decodeView<T:UIView>(nibName name:String,className classType:T.Type,delegate:AnyObject) -> T {

    let nib = UINib(nibName: name)
    let topLevelObjects = nib.instantiateWithOwner(nil, options: nil)
    let view = topLevelObjects[0] as T
    view.setTranslatesAutoresizingMaskIntoConstraints(false)
    //check if view.delegate exists then view.delegate = delegate 
    return view
}
public class func decodeView(nibName名称:String,className classType:T.Type,委托:AnyObject)->T{
设nib=UINib(nibName:name)
让topLevelObjects=nib.InstanceWithOwner(nil,选项:nil)
将view=topLevelObjects[0]设为T
view.setTranslatesAutoResizengMaskintoConstraints(false)
//检查view.delegate是否存在,然后查看view.delegate=delegate
返回视图
}

如果您询问Swift是否支持反射,TL;DR:您需要从
NSObject
创建子类。否则你会得到有限的信息

在这里,你可以更详细地讨论你的可能性

清除此部分后,可以在下面的列表中找到如何获取属性列表的示例

尽管一种快速而肮脏的方法可能只是尝试访问属性(使用KVC)并在异常失败时捕获异常。Swift不支持Try/Catch/Finally构造,但这允许您编写如下代码:

SwiftTryCatch.try({
//试试看
},catch:{(错误)在
println(“\(error.description)”)
},最后:{
//密切资源
})