Tôi đã biểu thức này trả về một UInt32
:Swift convert UINT để int
let randomLetterNumber = arc4random()%26
Tôi muốn để có thể sử dụng số trong này nếu tuyên bố:
if letters.count > randomLetterNumber{
var randomLetter = letters[randomLetterNumber]
}
Vấn đề này là giao diện điều khiển đang cho tôi điều này
Playground execution failed: error: <REPL>:11:18: error: could not find an overload for '>' that accepts the supplied arguments
if letters.count > randomLetterNumber{
~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
Vấn đề là UInt32
không thể được so sánh với số Int
. Tôi muốn truyền randomLetterNumber
đến Int
. Tôi đã thử:
let randomLetterUNumber : Int = arc4random()%26
let randomLetterUNumber = arc4random()%26 as Int
Những cả gây could not find an overload for '%' that accepts the supplied arguments.
Làm thế nào tôi có thể đúc các giá trị hoặc sử dụng nó trong câu lệnh if?
Cảm ơn điều đó, trình khởi tạo Int() dường như thực hiện thủ thuật. – 68cherries
Bạn có thể đọc thêm về chuyển đổi loại số trong ** [Tài liệu Swift của Apple] (https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/TheBasics.html#//apple_ref/ doc/uid/TP40014097-CH5-XID_420) **. –
Cảm ơn. Tôi đã có một vấn đề tương tự với arc4random_uniform (someArray.count) đúc sửa lỗi arc4random_uniform (UInt32 (someArray.count)) – nacross