简体   繁体   中英

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

在此输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM