简体   繁体   English

iOS:无法从范围获取第一项值<CGFloat>

[英]iOS: Can't get first item value from Range<CGFloat>

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. 使用Range,一切正常。 However when I try to get values from float range I get error "Ambiguous reference to member 'first'". 但是,当我尝试从浮动范围获取值时,出现错误“对成员'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 让范围:范围= 1.0 .. <3.22

let first: CGFloat = range.first (!)(Ambiguous reference to member 'first') 首先:CGFloat = range.first(!)(对成员'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 ). firstCollection协议的方法( lastBidirectionalCollection的方法)。 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. 仅当基础元素类型是整数类型或具有整数跨度的符合Strideable协议的任何其他类型时, Range才是(双向)集合。 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. 一定范围的浮点数不是集合,因此没有firstlast方法。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM