结合 NSMutableArray 与 [Swift Array]
Combine NSMutableArray with [Swift Array]
目标:
我想要一个计算 属性,其中 return 是一个包含基于 NSMutableArray
和 swift 数组组合的不同类型对象的数组。
这里有两个问题:
- Computed 属性 包含带有
NSMutableArray
的代码
- 我不知道如何合并两个数组。
NSMutableArray
+ [AnyObjct]
我有一个 Objective-C class CentralManager
方法 storedDevices
returns 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 不确定我计算的 属性 应该 return NSMutableArray
,也许 return [AnyObject]
更好
你可以使用 NSMutableArray
的 addObjectsFromArray
方法
var allDevices: NSMutableArray {
get {
var blueToothDevices = CentralManager.shared().storedDevices
let devices = blueToothDevices.addObjectsFromArray(wifiDevices)
return devices
}
}
编辑: 如果您希望 allDevices
属于 swift 数组并且您的 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
}
}
让我们看一个简单的例子:
NSMutableArray
拿一个 NSMutableArray
和两个对象 "A" 和 "B"
let mutableArray = NSMutableArray()
mutableArray.addObject("A")
mutableArray.addObject("B")
print(mutableArray)
Swift数组:
取 Swift 包含两个对象 "C" 和 "D"
的数组
var swiftArray = [String]()
swiftArray = ["C" , "D"]
连接两个数组
let mutableSwift = mutableArray.objectEnumerator().allObjects as? [String]
swiftArray = swiftArray + mutableSwift!
print(swiftArray)
最后你Swift数组
["C", "D", "A", "B"]
目标:
我想要一个计算 属性,其中 return 是一个包含基于 NSMutableArray
和 swift 数组组合的不同类型对象的数组。
这里有两个问题:
- Computed 属性 包含带有
NSMutableArray
的代码
- 我不知道如何合并两个数组。
NSMutableArray
+[AnyObjct]
我有一个 Objective-C class CentralManager
方法 storedDevices
returns 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 不确定我计算的 属性 应该 return NSMutableArray
,也许 return [AnyObject]
更好
你可以使用 NSMutableArray
的 addObjectsFromArray
方法
var allDevices: NSMutableArray {
get {
var blueToothDevices = CentralManager.shared().storedDevices
let devices = blueToothDevices.addObjectsFromArray(wifiDevices)
return devices
}
}
编辑: 如果您希望 allDevices
属于 swift 数组并且您的 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
}
}
让我们看一个简单的例子:
NSMutableArray
拿一个 NSMutableArray
和两个对象 "A" 和 "B"
let mutableArray = NSMutableArray()
mutableArray.addObject("A")
mutableArray.addObject("B")
print(mutableArray)
Swift数组:
取 Swift 包含两个对象 "C" 和 "D"
的数组 var swiftArray = [String]()
swiftArray = ["C" , "D"]
连接两个数组
let mutableSwift = mutableArray.objectEnumerator().allObjects as? [String]
swiftArray = swiftArray + mutableSwift!
print(swiftArray)
最后你Swift数组
["C", "D", "A", "B"]