Cast from 'Int?' to unrelated type 'NSNumber' always fails

9.8k views Asked by At

When I try to do the line below, I dont get a warning (not an error). What is this am I am doing something bad?

I am trying to cast the integer earningsSoFar to a NSNumber because I want to get the .stringValue out of it.

I want to understand what is the warning mean here and how to do this right.

self.tv_salaryNumber.text = (earningsSoFar as! NSNumber).stringValue

enter image description here

1

There are 1 answers

0
Leo On BEST ANSWER

You can cast Int to NSNumber in this way

let a:Int? = 10
let b = a! as NSNumber

So,in your code,just try

self.tv_salaryNumber.text = (earningsSoFar! as NSNumber).stringValue

Also,as zeneak said,you can make it easier in his way