I need to create an array whose objects are of type Range. But creating an array like below produces an error "Reference to generic type 'Range' requires arguments in <...>"
var ranges:Array<Range> = []
Basically, what I'm trying to accomplish is to create a list of ranges that will serve as a step function to generate a random type. I've done this without using array, but as I add more types I feel the need to just loop them over an array. My problem is that ranges somehow cannot be stored in an Array in Swift. Here's my old code that worked.
let twisterUpperRange = UInt32(roundf(twisterRate * 1_000))
let bombUpperRange = UInt32(roundf(bombRate * 1_000)) + twisterUpperRange
let blindUpperRange = UInt32(roundf(blindRate * 1_000)) + bombUpperRange
let randomNumber = arc4random_uniform(1_000) + 1
var powerupType:PowerupType
switch randomNumber {
case 0...twisterUpperRange:
powerupType = PowerupType.TwisterType
case twisterUpperRange...bombUpperRange:
powerupType = PowerupType.BombType
case bombUpperRange...blindUpperRange:
powerupType = PowerupType.BlindType
default:
powerupType = PowerupType.NormalType
}
return powerupType
The error means that Swift Range
is generic a generic type on the type of the interval ends:
var ranges : Array<Range<UInt32>> = []
let blindRate = 0.3
let twisterRate = 0.5
let bombRate = 0.2
let twisterUpperRange = UInt32(twisterRate * 1_000)
let bombUpperRange = UInt32(bombRate * 1_000) + twisterUpperRange
let blindUpperRange = UInt32(blindRate * 1_000) + bombUpperRange
ranges.append(0...twisterUpperRange)
ranges.append(twisterUpperRange...bombUpperRange)
ranges.append(bombUpperRange...blindUpperRange)
print(ranges)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.