Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

ios - cannot convert value of type 'Range<Int32>' to expected argument type 'Range<_>'

@IBAction func Guess(_ sender: UIButton) {
        var R1 = (Range1.text!as NSString).intValue
        var R2 = (Range2.text! as NSString).intValue
        //print(R1, " ", R2)
        
        var answer = Int.random(in: R1..<R2)
    

I want to generate random number from the range given by the user through TextField.

question from:https://stackoverflow.com/questions/66068365/cannot-convert-value-of-type-rangeint32-to-expected-argument-type-range

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The issue there is that NSString's intValue property returns an Int32 what you need is integerValue which returns an Int.

Note that when coding in Swift you should always use Swift native types. You don't need to cast your string to NSString to convert it to integer. You can simply initialize a new integer from your string using Int initializer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...