Reputation: 143
I have an array that holds the following values: [0.1, 0.2, 0.18, "other"]. I recognize adding the "other" makes this an NSObject array. I want to be able to retrieve the first three values as doubles when they are selected so I can multiply them with other doubles. However, when I try to do it simply, it says "Can not put binary operator * with double and NSObject". How can I typecast or convert the NSObjects to doubles?
Upvotes: 2
Views: 1780
Reputation: 6475
You just need to cast it... The simples solution is something like:
let arr: [NSObject] = [0.1, 0.2, 0.18, "other"]
let result = (arr[0] as! Double) * (arr[1] as! Double)
But you should avoid force casting and find some better solution.
You can also flatMap
your array and get only proper values in result like:
let newArr = arr.flatMap { $0 as? Double }
But you have to be careful with this solution as you now depend on Foundation and if you have a slightly different values in array eg. [0.1, 0.2, 0.18, "other", false]
you'll get a wrong result.
And adding "other"
doesn't qualify this array to be NSObject
. It can actually be of Any
type as well, but this wont't change anything in case of casting values to proper types, but will add some security and will simplify things cause you won't be dependent on Foundation.
Upvotes: 1
Reputation: 17534
generally the same result as vacawama's answer has, but without help of Foundation
var myarr: Array<Any> = [0.1, 0.2, 0.18, "other", true]
for d in myarr {
if let d = d as? Double {
print(d)
}
}
prints
0.1
0.2
0.18
if you need extracted array of doubles, you can use
let doubles = myarr.flatMap { $0 as? Double }
print(doubles)
prints
[0.1, 0.2, 0.18]
declaration
var arr: Array<Any>
or var arr:[Any]
force your code to use pure Swift, even though you have imported Foundation before.
If you need to convert values in your array to Double values (if possible), the easiest way I can see right now (please see vacawama's notes below) is something like
let myarr:[Any] = [0.1, 0.2, 0.18, "other", true, 17, "22.3", "-0.2e-1","27"]
let doubles = myarr.flatMap { Double("\($0)") }
print(doubles) // [0.1, 0.2, 0.18, 17.0, 22.3, -0.02, 27.0]
Upvotes: 2
Reputation: 154583
For your Double
s to be be stored in an array of NSObject
s, they are converted to NSNumber
s.
If you iterate through your array, selecting values that are NSNumber
s using where
and assign them to a value which is of type AnyObject
, you can call doubleValue
on them to retrieve the Double
value from the NSNumber
:
var myarr = [0.1, 0.2, 0.18, "other"]
for d:AnyObject in myarr where d is NSNumber {
print(d.doubleValue * 2)
}
Output:
0.2
0.4
0.36
Explanation:
This works because any object (an instance of a class
) can be assigned to a value of type AnyObject
, and when AnyObject
is used as a concrete value, all known @objc
properties and methods are available. Since NSNumber
has the doubleValue
property, and we have already checked that d
is an NSNumber
, we can call doubleValue
knowing it won't crash at runtime.
Upvotes: 2
Reputation: 38012
You can force cast using as!
:
// let values = [0.1,0.2,0.3,"other"]
let val1 = values[0] as! Double
let val2 = values[1] as! Double
let val3 = values[2] as! Double
let other = values[3] as! String
print(val1 * val1)
print(val2 * val2)
print(val3 * val3)
print(other)
Upvotes: 4