最短的代码迅速创建随机数数组? [英] shortest code to create an array of random numbers in swift?
问题描述
我想创建一个随机数数组(Int,Int32)
I want to create an array of random numbers (Int, Int32)
我尝试了以下操作:
map(1...1000) { arc4random() }
但是它返回以下错误:
error: type 'ClosedInterval<T>' does not conform to protocol 'SequenceType'
我在做什么错了?
出于某种原因,对我的初次尝试进行细微调整似乎效果很好
For some reason, a minor tweak to my first attempts seems to work just fine
map(1...1000) { $0 - $0 + arc4random() }
现在的问题是,我不理解为什么这种调整后的方法行得通,为什么最初的方法不能...
The problem, now, is that I don't understand why this tweaked approached works and why the initial approach does not...
任何想法?
推荐答案
Swift愿意推断类型,但在放弃之前只能处理如此多的歧义。
Swift is willing to infer types, but it can only deal with so much ambiguity before it gives up.
在 map(1 ... 100){arc4random()}
的情况下,存在太多不确定性关于类型是什么。具体来说,是整数文字1和100。它们可以是 Int
, Int8
, UInt32
…有时Swift会默认使用 Int
,但是如果语句中有更多歧义,它可能会失去平衡并拒绝。
In the case of map(1...100) { arc4random() }
, there’s too much uncertainty about what the types are. Specifically, the integer literals 1 and 100. They could be Int
, Int8
, UInt32
… Sometimes Swift will default to Int
but if there’s more ambiguity in the statement it can get thrown off-balance and refuse to.
在这种情况下,在同一条语句中它试图找出 {arc4random()}
的类型。应该可以推断出返回类型为 UInt32
,因为这就是 arc4random
返回的内容。但是输入类型是什么?您不必在表达式中使用它,所以没关系,但是Swift必须将其制成某种东西,而且它也不知道是什么。
In this case, in the same statement it’s trying to figure out what the type of { arc4random() }
is. It ought to be able to infer that the return type is UInt32
because that’s what arc4random
returns. But what’s the input type? You aren’t using it in the expression, so it doesn’t matter, but Swift has to make it something, and it doesn’t know what.
原因编写 {$ 0-$ 0 + arc4random()}
可修复,这有助于确定 $ 0
的类型–必须是 UInt32
,因为那是唯一可以添加到 arc4random
输出中的东西。这样就使得闭包的类型 UInt32-> UInt32
。这意味着 1
和 100
的类型也必须为 UInt32
。这意味着 1 ... 10
必须是 Range
The reason writing { $0 - $0 + arc4random() }
fixes it is that helps determine what the type of $0
is – it has to be a UInt32
because that’s the only thing that you can add to the output of arc4random
. So that makes the type of the closure UInt32 -> UInt32
. Which means the type of the 1
and the 100
must also be UInt32
. Which means 1...10
must be a Range
最后,您收到有关 ClosedInterval
不符合 SequenceType
的投诉的原因是,有两种可能 ...
运算符的返回类型– 范围
和 ClosedInterval
。您想要一个 Range
,但是Swift不知道–它不能使 Range
或 ClosedInterval
工作。 Swift编译器错误倾向于列出许多无法使用的选项之一,通常不是您要尝试的选项,这可能会造成混淆。
Finally, the reason you’re getting a complaint about ClosedInterval
not conforming to SequenceType
is because there are two possible return types for the ...
operator – Range
and ClosedInterval
. You want a Range
but Swift doesn’t know that – it can’t make Range
or ClosedInterval
work. Swift compiler errors have a tendency to list one of the many possible options that won’t work, usually not the one you were trying for, which can be a bit confusing.
解决此问题而不采用 $ 0-$ 0
技巧的更好方法可能是这样的:
The better way to fix this without resorting to the $0 - $0
trick is probably something like this:
let a = map(1...100) { _ in arc4random() }
_
表示您不关心闭包的输入。提供一个输入参数(甚至是一个匿名参数)似乎足以让Swift放松一点,并为两个整数文字默认为 Int
。
The _
indicates you don’t care about the input to the closure. Supplying an input argument (even an anonymous one) seems to be enough to let Swift relax a little bit and default to Int
for the two integer literals.
这篇关于最短的代码迅速创建随机数数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!