user1406716
user1406716

Reputation: 9705

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

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

Upvotes: 5

Views: 10056

Answers (1)

Leo
Leo

Reputation: 24714

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

Upvotes: 7

Related Questions