Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Hamza Ansari
34 PointsHow do I connect a button to this function?
I'm trying to make it so that a button executes this function called "incrementBy", which is supposed to add a vote to the poll choice that the button represents and return the results of the poll to the user. How do I do that?
class Counter {
var voteCount: Int = 0
func incrementBy(amount: Int, numberOfTimes times: Int) { voteCount += amount * times
println(" \(voteCount) votes")
}

Hamza Ansari
34 PointsI know that much, but I don't now how to incorporate this function into the IBAction once I link the button into my assistant editor. Can I just write that entire code above as is below my IBAction?

Dan Merchant
Courses Plus Student 22,188 Pointsin your viewDidLoad, you should instantiate a counter for each kind of vote with something like this:
var blueVoteCounter = Counter() var redVoteCounter = Counter()
then in your viewController:
@IBAction func blueVoteCounter() {
blueVoteCounter.incrementBy(1, numberOfTimes: 1)
blueCounterLabel.text = String(blueVoteCounter.voteCount) // this should work to have a count next to in in a
IBOutlet blueLabelCount
}

Hamza Ansari
34 PointsNow I'm getting this error that says, '0 -> 0' does not have a member named 'incrementBy' on the line that says countOption1.incrementBy(1, numberOfTimes: 1). What do you suppose that means? Here's my new code below:
class Counter {
var voteCount: Int = 0
func incrementBy(amount: Int, numberOfTimes times: Int) { voteCount += amount * times
println(" \(voteCount) votes")
}
var countOption1 = Counter()
var countOption2 = Counter()
}
func didReceiveMemoryWarning() {
didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
@IBAction func countOption1() {
countOption1.incrementBy(1, numberOfTimes: 1)
}
@IBOutlet weak var totalVotes1: UILabel!
totalVotes1.text = String(countOption1.voteCount)
}
I'm also getting an "Expected declaration" error on the totalVotes1.text = String(countOption1.voteCount) line, but not sure why.
6 Answers

Dan Merchant
Courses Plus Student 22,188 Pointsthis worked in my playground, if it doesn't work in your app, that means you're messing up the different variables in scope. as in, something isn't global, or available to your current scope.
class Counter {
var voteCount: Int
init() {
voteCount=0
}
func incrementBy(amount: Int, numberOfTimes times: Int) {
voteCount += amount * times
println(" \(voteCount) votes")
}
}
var countOption1=Counter.init()
countOption1.incrementBy(1, numberOfTimes: 1)

Hamza Ansari
34 PointsThanks, I greatly appreciate the help. You don't happen to know how I can save all this as a PFObject do you? The problem I'm running into is saving it all as a PFObject so that the votes can be saved on my database on Parse.com, which is why I formatted my variables like so:
var voteCount = PFObject(className:"VoteCount")
voteCount["votes"] = 0
voteCount["optionName"] = "crepes"
voteCount.saveInBackground()
I just can't save the voteCount variable as a PFObject without getting an error, but does it work for you?

Dan Merchant
Courses Plus Student 22,188 Pointstry changing your class to this:
class Counter {
var voteCount: Int
init() {
voteCount=0
}
func incrementBy(amount: Int, numberOfTimes times: Int) { voteCount += amount * times
println(" \(voteCount) votes")
}
you might want to play around with it in a playground to suss out the problems, that's how I figured this out. I'm not an expert on this, but this should move you in the right direction.

Dan Merchant
Courses Plus Student 22,188 PointsNot sure how that works, I'm a little mystified by pdf's, I've seen objective c sample code that works, but there's some behind the scenes magic that makes this stuff work that is beyond me.

Dan Merchant
Courses Plus Student 22,188 PointsI had a mistype there, you probably don't want to name the action the same as your instance of Counter. That may or may not be the problem.

Hamza Ansari
34 PointsOK, do you mean the part that says countOption1.incrementBy(1, numberOfTimes: 1)? I should change the countOption1 on that line to something else is what you're saying?

Dan Merchant
Courses Plus Student 22,188 Pointsthis is the code from a post I made above.
class Counter {
var voteCount: Int
init() {
voteCount=0
}
func incrementBy(amount: Int, numberOfTimes times: Int) {
voteCount += amount * times
println(" \(voteCount) votes")
}
}
var countOption1=Counter.init()
countOption1.incrementBy(1, numberOfTimes: 1)
paste this into a playground. play around with it until it works the way you expect it to. then add it to your app. This should give you a better understanding of how you want to code to accomplish your objectives.

Hamza Ansari
34 PointsThanks again. That works in the Playground, but I just don't know where to stick it in the view controller so that a button executes it. I tried placing all that within an IBAction but I get the same error again. Do I need to place it in the viewDidLoad or below the closing of the didReceiveMemoryWarning? Sorry for all these questions! You've helped me out a lot so far.

Hamza Ansari
34 PointsHow did you define the 'incrementBy' member? When I try to create the IBAction the way you wrote it, I'm getting an error that says '0 -> 0' does not have a member named 'incrementBy'.
@IBAction func countOption1() {
countOption1.incrementBy(1, numberOfTimes: 1)
}
Dan Merchant
Courses Plus Student 22,188 PointsDan Merchant
Courses Plus Student 22,188 Pointsselect your storyboard, select the interlocking circles icon for the assistant editor which will put the view controller code on the right.
control click/drag from the button in the storyboard to your code and let go. choose action, then transfer the code you have into that func.
This is shown in the swift fun facts tutorial for app building.