作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在将 Int32 转换为 String 时遇到困难。我试图遵循:
String(cartItem?.quantity)
“\(cartItem?.quantity)”
但没有运气。
cart.quantity
是 Int32 类型。
quantity 是 CoreData 模型中 cart 的一个属性。
最佳答案
问题不明确,但我认为这个问题归结为您不能使用可选值初始化字符串。
所以要么按照@matt 的建议进行操作,要么强制打开 cartItem
String(cartItem!.quantity)
或者提供一个默认值
String(cartItem?.quantity ?? 0)
当然,如果你需要处理你可能没有购物车的事实,那么最好这样做
if let cart = cartItem {
let str = "\(cart.quantity)" //or String(cart.quantity)
//do stuff with str
} else {
//handle no cart state
}
关于swift - 如何在 Swift 4 中将 Int32 转换为 String?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56311227/
我是一名优秀的程序员,十分优秀!