Shortest code to create an array of random numbers in swift?

I want to create an array of random numbers (Int, Int32)

I tried the following:

map(1...1000) { arc4random() } 

but it returns the following error:

error: type 'ClosedInterval<T>' does not conform to protocol 'SequenceType'

What am I doing wrong?


For some reason, minor tweaking of my first attempts seems to work just fine

map(1...1000) { $0 - $0 + arc4random() }

Now the problem is that I don’t understand why this customized work and why the initial approach is not ...

Any ideas?

+4
source share
1 answer

Swift wants to infer types, but it can handle so much ambiguity until it surrenders.

map(1...100) { arc4random() }, . , 1 100. Int, Int8, UInt32... Swift Int, , .

, { arc4random() }. , UInt32, arc4random. ? , , Swift -, , .

{ $0 - $0 + arc4random() } , , $0 - UInt32, , arc4random. , UInt32 -> UInt32. , 1 100 UInt32. , 1...10 Range

, , ClosedInterval SequenceType, , ... - Range ClosedInterval. Range, Swift - Range ClosedInterval. , , , , .

, $0 - $0, , :

let a  = map(1...100) { _ in arc4random() }

_ , . ( ), , , Swift Int .

+9

Source: https://habr.com/ru/post/1570242/


All Articles