gpt4 book ai didi

ios - 结合 NSMutableArray 与 [Swift Array]

转载 作者:行者123 更新时间:2023-11-28 16:16:05 27 4
gpt4 key购买 nike

目标:我想要一个计算属性,它返回一个包含基于 NSMutableArray 的不同类型对象的数组。和快速数组组合。

这里有两个问题:

  1. 计算属性包含带有 NSMutableArray 的代码
  2. 我不知道如何组合两个数组。 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]

最佳答案

你可以使用 NSMutableArrayaddObjectsFromArray 方法

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
}
}

关于ios - 结合 NSMutableArray 与 [Swift Array],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39077493/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com