I have a Range with float numbers. I need to get the first and the last elements of the range. With Range everything works fine. However when I try to get values from float range I get error "Ambiguous reference to member 'first'". How do I get values from float range?
I tried casting, unwrapping - same error. Looked at posts with similar problem but nothing seems to work.
let range: Range = 1.0..<3.22
let first: CGFloat = range.first (!)(Ambiguous reference to member 'first')
I expect to get the first item from the range, instead I get an error.
first
is a method of the Collection
protocol (and last
a method of BidirectionalCollection
). A Range
is a (bidirectional) collection only if the underlying element type is an integer type, or any other type that conforms to the Strideable
protocol with an integer stride. Example:
let intRange = 1..<10
print(intRange.first!) // 1
print(intRange.last!) // 9
A range of floating point numbers is not a collection, so there is no first
or last
method. But you can retrieve the lower/upper bound:
let range: Range<CGFloat> = 1.0..<3.22
print(range.lowerBound) // 1.0
print(range.upperBound) // 3.22