Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Objective-C Basics Practicing with Immersive Examples Immersive example 1 - Ping-Pong

Ryan Maneo
Ryan Maneo
4,342 Points

Is it bad that I haven't the slightest clue where to start?

For the pingPong example, I honestly don't know where to start. I haven't watched how he did it, because I feel like I should know or be able to figure it out! Also, arc4random doesn't work... not sure why: "Too many arguments to function call, expected 0, have 1"

I've been trying to figure it out for DAYS now. Should I watch how he does it? Or would that be cheating and make me retain less information? :/

2 Answers

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Ryan Minnick,

I definitely think trying to work something out on your own is a great learning experience. However, after a certain point it just makes more sense to move forward. As long as you gave it a valiant effort, sometimes just accepting the answer is the right solution. You wouldn't believe how many things I was hung up on for weeks while learning and just moved for them for them to click on their own later.

As for arc4random not working, make sure you are using:

int winner = arc4random()%2 + 1;

This version of arc4random doesn't accept any parameters so to achieve a result of 1 of 2 numbers you need to do a modulo on the resulting number. This is essentially a check between even or odd. I just add a 1 to the result to make it more readable and discard the zero base. Instead of 0 or 1 as results, our possible results are now 1 or 2. I think this correlates better to player 1 and player 2.


UInt32 upperbound = 2;
int winner = arc4random_uniform(upperbound) +1;

Using arc4random_uniform will return a random number less than the specified upper bound. So this will return us a number of 0 to 1 randomly. I add one to the result to make the code more understandable. If the number is 1 then player 1 won the round. If the number is 2 then player 2 wins the round.

Hopefully this is enough to let you move forward a bit. Good Luck!