目标:我想拥有一个计算属性,该属性将基于数组NSMutableArray
和快速数组组合返回具有不同类型对象的数组。
这里有两个问题:
NSMutableArray
NSMutableArray
+[AnyObjct]
我有一个Objective-C类,CentralManager
带有一个storedDevices
返回NSMutableArray
一些对象的方法。例子:
var wifiDevices = [WifiDevice]()
var allDevices: NSMutableArray {
get {
let blueToothDevices = CentralManager.shared().storedDevices
let devices = blueToothDevices + wifiDevices // it does not work as we can't combine NSMutableArray and swift array.
return devices
}
}
另外,由于我使用swift不确定我的计算属性应该返回NSMutableArray
,所以最好返回[AnyObject]
您可以使用addObjectsFromArray
的方法NSMutableArray
为
var allDevices: NSMutableArray {
get {
var blueToothDevices = CentralManager.shared().storedDevices
let devices = blueToothDevices.addObjectsFromArray(wifiDevices)
return devices
}
}
编辑:如果您想allDevices
是快速数组和blueToothDevices
包含WifiDevice
对象的类型,您可以使用[WifiDevice]
这种方式。
var blueToothDevices = CentralManager.shared().storedDevices
var devices = blueToothDevices.objectEnumerator().allObjects as! [WifiDevice]
let devices = devices + wifiDevices
对于任何物体
var allDevices: [AnyObject] {
get {
var blueToothDevices = CentralManager.shared().storedDevices
var blueTooths = blueToothDevices.objectEnumerator().allObjects
blueTooths = blueTooths + wifiDevices
return blueTooths
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句