Q:

Swift: Having a "for in loop" where the range is determined by a return statement

I was building a custom UI and I  realize that for some reason I cannot do this.
protocol notImportant{
   SegementButtons(segmentControl : VerticalSegmentControl) -> Int
}
 //trying to use the function later in this fashion below

for index in 0...delegate?.segementButtonsCount(self)

Now I know there are many other solutions. 
First of all, is this valid or must I provide a concrete number or variable?
Continued
Xcode shows an error
        Binary operator '...' cannt be applied to oraands of type Int and Int?

I type cast the return value to an Int, changing the error to 
        Type Int does not conform to protocol SequenceType

Now it would be pretty cool if I could make this work without Xcode cutting itself. 

A:

delegate is an optional, therefore the type of the expression
delegate?.segmentButtonsCount(self)

is also an optional (which is nil if delegate == nil).
You can use optional binding to unwrap the delegate 
if let theDelegate = delegate {
     for index in 0 ..< theDelegate.segmentButtonsCount(self) {
         // do something ...
     }
}

or use the nil-coalescing operator ?? to provide a 
default value:
 for index in 0 ..< (delegate?.segmentButtonsCount(self) ?? 0) {
     // do something ...
 }

Note that since array indices are zero-based, you probably want to use the range operator ..< which
excludes the end element.

