-2

I'm runtime errors on my existing project when migrating versions of Xcode. Can someone help with this? Is my typecasting wrong?

Code:

let x    = d[ChildItem.KEY_LOOK] as! NSArray as Array
let y    = d[ChildItem.KEY_COOR] as! NSArray as Array
let item = ChildItem(x as! [Float], y as! [Float])

Error:

Thread 5: Fatal error: Unable to bridge NSNumber to Float

I know how to cast an NSNumber to a Float as I found on SO (Unable to bridge NSNumber to Float in JSON parsing):

    if let n = d.value(forKey: "probability") as? NSNumber {
    let f = n.floatValue }

How can I do this for an NSNumber array?

In each of the d[] keys there are JSON strings like this:

d[ChildItem.KEY_LOOK] = [465918.2, 5681518.0,4462.3203]

Fra
  • 393
  • 7
  • 19
  • What is this? `let 1 = ...` doesn't look like valid syntax. – Joakim Danielson Aug 14 '18 at 17:05
  • Right - fixed now. – Fra Aug 14 '18 at 17:24
  • 1
    I have to say, this is really ugly code. First of all, you should avoid force casting, that causes crashes. Then you're force-casting to `NSArray` and then to `Array`, when casting to `NSArray` is unnecessary. `KEY_LOOK` and `KEY_COOR` are plain ugly to look at, I would create a `Key` enum in `ChildItem` and add constants in there, so it'd look like this `ChildItem.Key.Look`. And finally, we don't know how `d` is declared, or how you got it's contents, so we can only guess what your error is, you should provide us with that info and avoid using NS-prefixed classes when working with Swift. – EmilioPelaez Aug 14 '18 at 17:31
  • Please post the contents of `d`. – vadian Aug 14 '18 at 17:44
  • @Vadian - you can see contents of 'd' at the SO post: https://stackoverflow.com/questions/49610727/unable-to-bridge-nsnumber-to-float-in-json-parsing – Fra Aug 14 '18 at 18:18
  • I mean the `d` in your first example. And `value(forKey...as NSNumber)` is horrible code. – vadian Aug 14 '18 at 18:26
  • @Vadian - d content: d["s_i_coord"]. What it is is going through a JSON tree. – Fra Aug 14 '18 at 18:33
  • Sorry, I'm out. Without knowing the **real** contents of `d` (and its static type) I can't help. – vadian Aug 14 '18 at 18:40
  • What happens if you skip the first cast to NSArray and just have `let x = d[ChildItem.KEY_LOOK] as? Array` – Joakim Danielson Aug 14 '18 at 18:40
  • @JoakimDanielson Type cast to `Array` is pointless. A JSON array is at least `[Any]`, in many cases `[[String:Any]]` and in this particular case it's supposed to be `[Float]`. – vadian Aug 14 '18 at 18:43
  • @vadian, yes of course. I didn't think that through, I guess I just got frustrated by the double casting. – Joakim Danielson Aug 14 '18 at 18:45
  • I also tried: let y = d[ChildItem.KEY_COOR] as! [Float] and get same error. – Fra Aug 14 '18 at 18:47
  • Unless you clarify your question and show us what kind of data you have in `d` for those two keys this is pointless. Also in your question you say you're having problem compiling the project but this is a runtime error, very confusing and what is this about "...migrating versions if Xcode"? – Joakim Danielson Aug 14 '18 at 18:53
  • @Joakim Danielson - see above – Fra Aug 14 '18 at 18:55
  • Where? I don't understand. – Joakim Danielson Aug 14 '18 at 18:59
  • @Joakim Danielson - up in the original post. – Fra Aug 14 '18 at 19:48

2 Answers2

0

you need to use the floatValue property of NSNumber, not casting directly.

-1

Let's say you have an array of numbers you got from the server calls like this

d[ChildItem.KEY_LOOK] = [465918.2, 5681518.0,4462.3203] // from your example

you can do the following

//As from your example that can be directly convert to Array of Doubles like this
if let arrayOfDoubles = d[ChildItem.KEY_LOOK] as? [Double] {
        debugPrint(arrayOfDoubles)
}

And if for some reason you want those to be [NSNumber] then you can do like this

if let arrayOfNumbers = d[ChildItem.KEY_LOOK] as? [NSNumber] {

        //to make this array of double
     let doubleArray = arrayOfNumbers.map { $0.doubleValue }

        //to make int
     let intArray = arrayOfNumbers.map { $0.intValue }

     ... and so on
}
kathayatnk
  • 975
  • 1
  • 7
  • 9