小编arc*_*igh的帖子

如何在arc4random_uniform()的范围之间创建随机数?

所以我在这个代码中的目标是随机滚动两个骰子,因为我们都知道你的常规骰子只有6个边,所以我导入了Foundation来访问arc4random_uniform(UInt32).我试图使用(1..7)的范围来避免随机获得0,但是返回了一个我不太喜欢的错误.我试着这样做:

dice1 = arc4random_uniform(UInt32(1..7))
Run Code Online (Sandbox Code Playgroud)

然而那又归来了

找不到接受提供的参数的'init'的重载

我希望这是足够的信息,你在那里惊人的debs帮助我:)

请注意我只是在操场上练习快速练习.我不必学习如何做到这一点; 在我开始构建实际的应用程序之前,我只是在修补:D

//imports random number function
import Foundation
//creates data storage for dice roll
var dice1: UInt32 = 0
var dice2: UInt32 = 0
//counter variable
var i = 0
//how many times snake eyes happens
var snakeeyes = 0
 //how many times a double is rolled
var `double` = 0
//rolls dice 100 times
while i < 100{
    //from here
    //sets dice roll
Run Code Online (Sandbox Code Playgroud)

这将返回错误"Range $ T3"无法转换为UInt32

   dice1 = arc4random_uniform(1..7)
   dice2 = arc4random_uniform(1..7) …
Run Code Online (Sandbox Code Playgroud)

range arc4random swift

127
推荐指数
9
解决办法
15万
查看次数

{(Int)}与UInt8不同

目前使用beta 5版本的swift并且必须对+=运营商进行更改

func dealCards1() -> [Int] {
    for i in 0...25{
        comp1PlayDeck += shuffledDeck[i]
    }
    return comp1PlayDeck
}
Run Code Online (Sandbox Code Playgroud)

这引发了'[(Int)]' is not identical to 'UInt8'我不太清楚是做了什么改变但是这很令人困惑.

int uint swift xcode6

3
推荐指数
1
解决办法
2743
查看次数

快速比较 switch 语句中的元组与 or

如何让 switch 语句中的元组具有多种可能性?注意我已经尝试过

var duel = (comp1CurrCard, comp2CurrCard)
    switch duel {

        case (1||14||27||40, 1||14||27||40):
            println("ace duel")

        case (2,15,28,41),(2,15,28,41):
            println("2 duel")
    }
Run Code Online (Sandbox Code Playgroud)

comp1CurrCardcomp2CurrCard都是 Int 类型。本质上我想要的是 if comp1CurrCard== 1 || 14 || 27|| 40 && comp2CurrCard== 1 || 14 || 27|| 40 比 println("王牌决斗")

但是我不知道如何最好地做到这一点,我确实知道我希望使用 switch 语句,因为它似乎是解决它的最佳方法

我知道由于错误我做错了一些事情:P 感谢任何帮助!

tuples switch-statement swift

1
推荐指数
1
解决办法
1756
查看次数

标签 统计

swift ×3

arc4random ×1

int ×1

range ×1

switch-statement ×1

tuples ×1

uint ×1

xcode6 ×1