Error Message: 'expected identifier in function declaration'

I'm teaching myself to program using the book Swift for Beginners: Develop and Design. In one lesson I get the error message, "expected identifier in function declaration". The version of Xcode I'm using is more recent than the one the book was written for and I'm having some difficulty figuring out what the error is. Any help would be appreciated.

By the way, the code the book had me enter is,

extension Int {

func repeat(work: () -> ()) {

for _ in 0..<self {

work()

}

}

}


repeat {(

print("repeat this string")

)}

repeat is a reserved keyword.


Change repeat with sthing else, like myRepeat.

Change repeat [to something] else, like myRepeat.

That’s good advice.

Note that Swift does let you use keywords, like

repeat
, as identifiers; you just have to enclose them in backticks. So, the following code will work:
extension Int {
    func `repeat`(work: () -> ()) {
        for _ in 0..<self {
            work()
        }
    }
}

5.`repeat` {
    print("repeat this string")
}

However, that’s a bit of a pain to use so I recommend that you go with Claude31’s suggestion (-:

Also note that Swift 3 will make most keywords much more context sensitive so, while you’ll still need the backticks on the declaration, you’ll be able to skip them in many call sites (including this one). See:

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

WWDC runs Mon, 13 Jun through to Fri, 17 Jun. During that time all of DTS will be at the conference, helping folks out face-to-face. http://developer.apple.com/wwdc/

This is to Claude 31 as well. Thank you both for your help. This got me past where I was stuck.

Apologies for not thanking you sooner but my computer crashed so it took some time.

Again, thank you.

Error Message: 'expected identifier in function declaration'
 
 
Q