零合并运算符错误 "Cannot invoke initializer for type 'String' with an argument list of type '(Int64?)'"
Error "Cannot invoke initializer for type 'String' with an argument list of type '(Int64?)'" with nil-coalescing operator
我收到 nil-coalescing 运算符的错误 "Cannot invoke initializer for type 'String' with an argument list of type '(Int64?)'"。
stackoverflow上也有类似的问题,但是没有给我需要的解决方法
我的代码在注释中有错误:
let cell = tableView.dequeueReusableCell(withIdentifier: "countCell", for: indexPath)
cell.textLabel?.text = String(detailItem?.count) ?? "1" // Cannot invoke initializer for type 'String' with an argument list of type '(Int64?)'
let cell = tableView.dequeueReusableCell(withIdentifier: "countCell", for: indexPath)
cell.textLabel?.text = String(Int(detailItem?.count)) ?? "1" // Cannot invoke initializer for type 'Int' with an argument list of type '(Int64?)'
let cell = tableView.dequeueReusableCell(withIdentifier: "countCell", for: indexPath)
cell.textLabel?.text = String(Int(detailItem!.count)) ?? "1" // Cannot invoke initializer for type 'Int' with an argument list of type '(Int64?)'
let cell = tableView.dequeueReusableCell(withIdentifier: "countCell", for: indexPath)
cell.textLabel?.text = String(detailItem?.count ?? "1") // Cannot convert value of type 'String' to expected argument type 'Int64'
你可以简单地做,
let int64Value: Int64 = 123
let str = String(int64Value)
编辑:
因为 detailItem?.count
是 optional
,你需要 unwrap 它传递给 String
initializer。您可以使用 nil 合并运算符 展开并提供默认值,即
cell.textLabel?.text = String(detailItem?.count ?? 1)
let tmp = Int64(10)
print(String(tmp))
我收到 nil-coalescing 运算符的错误 "Cannot invoke initializer for type 'String' with an argument list of type '(Int64?)'"。
stackoverflow上也有类似的问题,但是没有给我需要的解决方法
我的代码在注释中有错误:
let cell = tableView.dequeueReusableCell(withIdentifier: "countCell", for: indexPath)
cell.textLabel?.text = String(detailItem?.count) ?? "1" // Cannot invoke initializer for type 'String' with an argument list of type '(Int64?)'
let cell = tableView.dequeueReusableCell(withIdentifier: "countCell", for: indexPath)
cell.textLabel?.text = String(Int(detailItem?.count)) ?? "1" // Cannot invoke initializer for type 'Int' with an argument list of type '(Int64?)'
let cell = tableView.dequeueReusableCell(withIdentifier: "countCell", for: indexPath)
cell.textLabel?.text = String(Int(detailItem!.count)) ?? "1" // Cannot invoke initializer for type 'Int' with an argument list of type '(Int64?)'
let cell = tableView.dequeueReusableCell(withIdentifier: "countCell", for: indexPath)
cell.textLabel?.text = String(detailItem?.count ?? "1") // Cannot convert value of type 'String' to expected argument type 'Int64'
你可以简单地做,
let int64Value: Int64 = 123
let str = String(int64Value)
编辑:
因为 detailItem?.count
是 optional
,你需要 unwrap 它传递给 String
initializer。您可以使用 nil 合并运算符 展开并提供默认值,即
cell.textLabel?.text = String(detailItem?.count ?? 1)
let tmp = Int64(10)
print(String(tmp))