1

The goal: I want to have a computed property which returns an array with different type of objects based on NSMutableArray and swift array combination.

Two problem here:

  1. Computed property contains code with an NSMutableArray
  2. I don't know how to combine two arrays. NSMutableArray + [AnyObjct]

I have an Objective-C class CentralManager with a method storedDevices that returns NSMutableArray with some objects. Example:

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

Also as I use swift not sure that my computed property should return NSMutableArray, maybe it's better to return [AnyObject]

2 Answers 2

1

You can use addObjectsFromArray method of NSMutableArray for that

var allDevices: NSMutableArray {
   get {
       var blueToothDevices = CentralManager.shared().storedDevices
       let devices = blueToothDevices.addObjectsFromArray(wifiDevices) 
       return devices
   }
}

Edit: If you want allDevices to be of swift array and your blueToothDevices contains WifiDevice type of object you can use [WifiDevice] like this way.

var blueToothDevices = CentralManager.shared().storedDevices
var devices = blueToothDevices.objectEnumerator().allObjects as! [WifiDevice]
let devices = devices + wifiDevices

For Any Object

var allDevices: [AnyObject] {
    get {
        var blueToothDevices = CentralManager.shared().storedDevices
        var blueTooths = blueToothDevices.objectEnumerator().allObjects
        blueTooths = blueTooths + wifiDevices
        return blueTooths
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for answer! let devices = devices + wifiDevices brings this issue `Binary operator '+' cannot be applied to operands of type '[BluetothDevice]' and '[WifiDevice]'
blueToothDevices.addObjectsFromArray(wifiDevices) return devices throws this complier issue: Cannot convert return expression of type 'Void' (aka '()') to return type 'NSMutableArray'
I have told you it only works if blueToothDevices contains WifiDevice object, for this situation you need to convert both array to [AnyObject] and than concatenate them with + or either use my first answer.
thanks for details, so in my question CentralManager.shared().storedDevices is an NSMutableArray which can contain any object right? In Objective-C that type of array can return to you even NSNumber, but let's assume that I expect BluetoothDevice objects which I want to set to let blueToothDevices and also I have wifiDevices array which contains [WiFiDivice] model as in my question, so the question is regarding how can I combine these two arrays. Can you show in your answer how to do it, thanks! appreciate any help.
0

Let's See Simple Example :

NSMutableArray

Take one NSMutableArray with two Object "A" and "B"

 let mutableArray = NSMutableArray()
 mutableArray.addObject("A")
 mutableArray.addObject("B")
 print(mutableArray)

Swift Array :

Take Swift Array with two Object "C" and "D"

 var swiftArray = [String]()
 swiftArray = ["C" , "D"]

concat two Array

let mutableSwift = mutableArray.objectEnumerator().allObjects as? [String]
swiftArray = swiftArray + mutableSwift!

print(swiftArray)

Finally You Swift Array

["C", "D", "A", "B"]

2 Comments

thanks for the answer, but in your case, you have the same types, in my example CentralManager.shared().storedDevices can return what ever NSMutableArray can store, but actually it will be BluetoothDevice model and my other swift array contains WifiDevice model, so I am not sure that I can simpe + this arrays.
You can Keep AnyObject. and when you use then Check object is BluetoothDevice or WifiDevice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.