Enumerate Is Unavailable Call the Enumerate Method on the Sequence

Swift 2.0 : 'enumerate' is unavailable: call the 'enumerate()' method on the sequence

Many global functions have been replaced by protocol extension methods,
a new feature of Swift 2, so enumerate() is now an extension method
for SequenceType:

extension SequenceType {
func enumerate() -> EnumerateSequence<Self>
}

and used as

let mySwiftStringArray = [ "foo", "bar" ]
for (index, string) in mySwiftStringArray.enumerate() {
print(string)
}

And String does no longer conform to SequenceType, you have to
use the characters property to get the collection of Unicode
characters. Also, count() is a protocol extension method of
CollectionType instead of a global function:

let myString = "foo"
let stringLength = myString.characters.count
print(stringLength)

Update for Swift 3: enumerate() has been renamed to enumerated():

let mySwiftStringArray = [ "foo", "bar" ]
for (index, string) in mySwiftStringArray.enumerated() {
print(string)
}

enumerate is unavailable call the enumerate method on the sequence

In Swift 2, enumerate is not a global function anymore, it's an extension of SequenceType.

Call it directly on the sequence to enumerate like this:

for (index, key) in row.enumerate() {
// ...
}

Update enumerate function in Swift 2

You obviously know that tilesArray is of type [[Int]], so do the optional binding on the proper type

init (filename: String) {
if let dictionary = Dictionary<String, AnyObject>.loadJSONFromBundle(filename) {
if let tilesArray = dictionary["tiles"] as? [[Int]] {
for (row, rowArray) in tilesArray.enumerate() {
let tileRow = NumRows - row - 1
for (column, value) in rowArray.enumerate() {
if value == 1 {
tiles[column, tileRow] = Tile()
}
}
}
}
}
}

Error when using reduce() in Swift 2.0

You fix this the same way that you fixed your problem with enumerate(). In Swift 2, reduce has been removed as a global function and has been added as an instance method on all objects that conform to the SequenceType protocol via a protocol extension. Usage is as follows.

var hashValue: Int {
return blocks.reduce(0) { $0.hashValue ^ $1.hashValue }
}

Cannot invoke 'enumerate' with an argument list of type '(String)'

Yes, String is no longer part of the collection type in swift 2.
You now want to use line.characters

For more reading on this matter, please see https://developer.apple.com/swift/blog/?id=30

How to enumerate an enum with String type?


Swift 4.2+

Starting with Swift 4.2 (with Xcode 10), just add protocol conformance to CaseIterable to benefit from allCases. To add this protocol conformance, you simply need to write somewhere:

extension Suit: CaseIterable {}

If the enum is your own, you may specify the conformance directly in the declaration:

enum Suit: String, CaseIterable { case spades = "♠"; case hearts = "♥"; case diamonds = "♦"; case clubs = "♣" }

Then the following code will print all possible values:

Suit.allCases.forEach {
print($0.rawValue)
}


Compatibility with earlier Swift versions (3.x and 4.x)

If you need to support Swift 3.x or 4.0, you may mimic the Swift 4.2 implementation by adding the following code:

#if !swift(>=4.2)
public protocol CaseIterable {
associatedtype AllCases: Collection where AllCases.Element == Self
static var allCases: AllCases { get }
}
extension CaseIterable where Self: Hashable {
static var allCases: [Self] {
return [Self](AnySequence { () -> AnyIterator<Self> in
var raw = 0
var first: Self?
return AnyIterator {
let current = withUnsafeBytes(of: &raw) { $0.load(as: Self.self) }
if raw == 0 {
first = current
} else if current == first {
return nil
}
raw += 1
return current
}
})
}
}
#endif


Related Topics



Leave a reply



Submit