添加負數時我遇到了一個非常簡單的問題,但 Apple 檔案并沒有真正涵蓋這一點。
SwiftUI 代碼:
Text(String(-50 5))
這會引發錯誤,Compiling failed: Ambiguous use of operator '-'. 但是,Text(String(-50))不會引發任何錯誤。
我嘗試將 -50 轉換為 Int 并將其包裝在括號中。此外,如果我分配-50 5給一個變數,然后在 Text() 中使用該變數,它作業正常。我怎樣才能讓它顯示-45,是什么導致了錯誤?
注意:在 macOS Monterey 上使用 XCode 13.2.1
編輯:人們要求截圖,因為似乎不是每個人都會出現這個問題。這是一個全新專案中的問題。

uj5u.com熱心網友回復:
查看 Canvas 診斷:
Swift.Float16:3:31: note: found this candidate
prefix public static func - (x: Float16) -> Float16
^
Swift.Float:2:31: note: found this candidate
prefix public static func - (x: Float) -> Float
^
Swift.Double:2:31: note: found this candidate
prefix public static func - (x: Double) -> Double
^
Swift.Float80:2:31: note: found this candidate
prefix public static func - (x: Float80) -> Float80
并告訴 Xcode 你想使用哪種型別:
例如
Text(String(-Int(50) 5))
或者
Text(String(-50.0 5))
uj5u.com熱心網友回復:
看到錯誤,它可能代碼無法將 -50 重新轉換為 int。
Swift.Float16:3:31: note: found this candidate
prefix public static func - (x: Float16) -> Float16
^
Swift.Float:2:31: note: found this candidate
prefix public static func - (x: Float) -> Float
^
Swift.Double:2:31: note: found this candidate
prefix public static func - (x: Double) -> Double
^
Swift.Float80:2:31: note: found this candidate
prefix public static func - (x: Float80) -> Float80
所以我測驗了一些這樣的例子。
let a = -Int(50) 3
let b = -50.0 3
let c:Int = -50 3
Text(String(-Int(50) 3))
Text(String(a))
Text(String(b))
Text(String(c))
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/415469.html
標籤:
