-1

I've read several posts here and here and I understand the "why". None of the suggestions in these posts functioned for me, but doing the conversion a step at a time did. This feels like a hack to me and like I'm missing something.

I've included the final bit of code here.

My Scenario:

  1. Step 1: Store CoreData Object as Primitive Int32 (not shown in code)
  2. Step 2: Fetch NSNumber Object (Showing just the needed code)
  3. Step 3: NSNumber to Int
  4. Step 4: Cast Int to Int32

    var finalQTY:Int32
    
    //Step 2
    if let retQTY = managedObject.valueForKey("totQty") as? NSNumber { 
    //Step 3       
    let preQTY = retQTY as Int
    //Step 4
    finalQTY = Int32(preQTY)
            }
    

Is there a better way? I feel like I am missing something? (Using Int is not an option because of the size of the numbers involved. I used QTY here but that was just as an example). Your feedback and responses are greatly appreciated as I continue to learn Swift.

Community
  • 1
  • 1
  • Why does the "Use scalar properties for primitive data types" suggestion from http://stackoverflow.com/a/29447590/1187415 not work for you? – Martin R May 21 '16 at 03:33
  • It does work if I change my datatype to Int. But since I need Int32 because of the size of the variable and there is no bridging between NSNumber and Int32 it fails and I get error messages. Thank you for looking at my question. My solution works, it just felt off. Going to mark this as solved and move on. :-) – Undead-Earth .com May 21 '16 at 16:47

1 Answers1

0

It appears there isn't a cleaner way to do this other than the two step process to get it from NSNumber to Int 32.

Thanks to everyone who pinged me on it.