简体   繁体   中英

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

Getting an error "Cannot invoke 'enumerate' with an argument list of type '(String)' " when I'm following Neil North's game tutorial for iOS Swift. Is this relating to an old method of some sort in 1.2? Any ideas?

   convenience init(atlasName: String, tileSize: CGSize,
        tileCodes: [String]) {
            self.init(tileSize: tileSize,
                gridSize: CGSize(width: tileCodes[0].characters.count,
                    height: tileCodes.count))

            atlas = SKTextureAtlas(named: atlasName)

            for row in 0..<tileCodes.count {
                let line = tileCodes[row]

                // ERROR IS HERE
                for (col, code) in enumerate(line) {
                    if let tile = nodeForCode(code) {
                        tile.position = positionForRow(row, col: col)
                        if tile.name == "scenery" {
                            tile.position = CGPoint(x: tile.position.x, y: tile.position.y - (tileSize.height/2))
                        }
                        addChild(tile)
                    }
                }
            }
    }

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

You should call enumerate on the collection itself. But in Swift 2 String is not a collection anymore - it has a characters property instead.

So to iterate over your String you should call enumerate on the characters property:

for (col, code) in line.characters.enumerate() {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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