Overloads for '...' Exist with These Result Types: Closedrange<Bound>, Countableclosedrange<Bound>

Overloads for '...' exist with these result types: ClosedRange Bound , CountableClosedRange Bound

As of Swift 3, ..< and ... produce different kinds of ranges:

  • ..< produces a Range (or CountableRange, depending on the underlying type) which describes a half-open range that does not include the upper bound.
  • ... produces a ClosedRange (or CountableClosedRange) which describes a closed range that includes the upper bound.

If the randomInRange() calculates a random number in the given range,
including the upper bound, then it should be defined as

func randomInRange(range: ClosedRange<Int>) -> Int {
// ...
}

and you can call it as

let lo = 1
let hi = 10
let r = randomInRange(range: lo ... hi)

I am trying to update my for loop for Swift 3 and I can't figure it out. I keep getting errors

Instead of forcibly trying to rewrite your original C-style for loop, consider what you're trying to achieve and attempt to re-write it in "native" Swift from scratch. How about breaking out of your loop once your true condition is met, instead of keeping it in the loop signature? E.g.

for i in 1...5 {
print(i)
if i == 3 { break }
} // 1 2 3

Applied to your example

func checkWinnerMove()
isWinner = false
for i in 0 ..< winningCombinations.count {
if gameState[winningCombinations[i][0]-1] == activePlayer &&
gameState[winningCombinations[i][1]-1] == activePlayer &&
gameState[winningCombinations[i][2]-1] == activePlayer {
isWinner = true
break
}
}
}

The explicit by index access of the (unknown for us) gameState and winningCombinations sequences is quite "unswifty" w.r.t. in the dangers of runtime exceptions for indices out of range for the sequences. So bear in mind that there are safer ways to perform such access.


For future reference, consider reading How to create a Minimal, Complete, and Verifiable example: since we (the potential answerer's of your question) don't have access to/the full information regarding isWinner, winningCombinations, gameState or activePlayer, we can't verify your example. Also, since the question cover a concept, it could be boiled down to a more minimal form. (Welcome to StackOverflow!)

Usage of Range operator in Data.subdata

  • ..< is the half-open range operator, which can either create a Range or CountableRange (depending on whether the Bound is Strideable with an Integer Stride or not). The range that is created is inclusive of the lower bound, but exclusive of the upper bound.

  • ... is the closed range operator, which can either create a ClosedRange or CountableClosedRange (same requirements as above). The range that is created is inclusive of both the upper and lower bounds.

Therefore as subdata(in:) expects a Range<Int>, you cannot use the closed range operator ... in order to construct the argument – you must use the half-open range operator instead.

However, it would be trivial to extend Data and add an overload that does accept a ClosedRange<Int>, which would allow you to use the closed range operator.

extension Data {
func subdata(in range: ClosedRange<Index>) -> Data {
return subdata(in: range.lowerBound ..< range.upperBound + 1)
}
}

let x = Data(bytes: [0x0, 0x1])
let z : UInt8 = x.subdata(in: 0...1).withUnsafeBytes {$0.pointee}

No '/' candidates produce the expected contextual result type 'CGPoint' - Swift 3

You're just passing in a CGFloat, center is expecting a CGPoint

e.g.

context?.addArc(center: CGPoint(x: frame.size.width/2, y: frame.size.height/2) ...

Observing Multiple NSKeyObservingKeyValueoptions swift

Change

 self.avplayerItem.addObserver(self, forKeyPath: "status", options:[.Initial | .New], context: nil)

to:

 self.avplayerItem.addObserver(self, forKeyPath: "status", options:[.Initial, .New], context: nil)

How to use max with swift3?

This is a consequence of SE-0054 Abolish ImplicitlyUnwrappedOptional type which has been implemented in Swift 3:

However, the appearance of ! at the end of a property or variable declaration's type no longer indicates that the declaration has IUO type; rather, it indicates that (1) the declaration has optional type, and (2) the declaration has an attribute indicating that its value may be implicitly forced. ...

If the expression can be explicitly type checked with a strong optional type, it will be. However, the type checker will fall back to forcing the optional if necessary.

Now one could argue that the compiler should fall back to unwrapping
the optional in

bubbleWidth = max(CGFloat(15), bubbleWidth)

but for some reason that works only with a float literal

bubbleWidth = max(15, bubbleWidth)

and I am not sure if this is a bug or not. Alternatively, unwrap the value explicitly

bubbleWidth = max(CGFloat(15), bubbleWidth!)

or – better – provide a default value with the nil-coalescing operator ??:

bubbleWidth = max(CGFloat(15), bubbleWidth ?? 0)

No '|' candidates produce the expected contextual result type 'NSTextStorageEditActions'

@dfri is correct. To illustrate another example of using the pipe, "|", is when doing the autoResizingMask for UIImageView as follows:

imageView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight, ...]

Of course you'd replace the ... with other UIViewAutoresizing options.

Good luck!



Related Topics



Leave a reply



Submit