"separator expected" in tutorial code

Hi guys,


I'm following the beginner's tutorial on building a meal rating app. I've managed to work my way through all steps with the helpful comments on this website to debug some issues myself, but now i've run into a bug that i can't seem to fix.


It's about this piece of code:


    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
        button.backgroundColor = UIColor.redColor()
        button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(_:)), forControlEvents: .TouchDown)
        addSubview(button)
    }


xCode gives me all sorts of error messages in this code. It says it expected a separator in front of the #selector expression. It shouldn't, according to the tutorial, and if i do that, it gives me 7 more bugs. Even though, when my despair grew high, i simply copy/pasted the code from the tutorial (to be found here). It still gave me the error message.


A list of the error messages that i get:


line 6

Expected ',' separator

Expected expression in list of expressions


I then tried closing xCode and opening the whole project again, and this time 3 more error messages showed up for line 5:

Expected expression in list of expressions

Expected ',' separator

Missing argument for parameter 'action' in call

You can find two screenshots of my error messages here:

screenshot 1

screenshot 2


In the end i tried downloading the example script file, but the same error message appeared in that file.


If anyone could give me an answer to what i'm doing wrong here, i would be immensely grateful.

If this is about the Food Tracker tutorial - it has well known bugs out of the box, so don't expect much as-is other than a minefield.


Search for past threads on: food tracker

Have you checked the signature of RatingControl.ratingButtonTapped ?


Is there a single argument ?

I managed to get it to work by replacing it with

button.addTarget(self, action: "ratingButtonTapped:", forControlEvents: .TouchDown)

instead of

button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(_:)), forControlEvents: .TouchDown)

The errors you have described are often seen when you opened Swift 2.2 files with an older version of Xcode.

The tutorial has recently been updated for Swift 2.2 which comes with Xcode 7.3.

You need to use the latest release version of Xcode, 7.3, to follow the courses of the latest tutorial. Or else you need find another tutorial somewhere else matching to your Xcode.

I'm guessing this must be it. I completely wiped my macbook today and reinstalled everything clean. The code works now.

Could you elaborate on this? What does your change mean?

Unfortunately, especially for starters, Swift is a currently developing language, and just .1 update may contain some destructive changes.

harukaze's first code is written in Swift 2.1 (or older) which worked till Xcode 7.2.1.


Anyway, you need some effort (even if you are an experienced programmer in other languages) to keep up to date with the latest Swift, and swift.org would be another good place to ask questions or discuss about Swift. Have a glance at it if not yet.

"separator expected" in tutorial code
 
 
Q