1

I am new to Xcode. I have variables x and y as random numbers from 1 to 100. I want the screen to print x and y when a button is pressed. How do I do that?

my code is:

- (IBAction)printtwonumbers:(id)sender;
{
x = arc4random() %100;
y = arc4random() %100;
label1 setText: [x];
label2 setText:[x];
}

3 Answers 3

3

XenElement is right if you want to print to the log. If you want to set the text of the labels though then your syntax is incorrect. In fact, if you are using that code you are probably getting a bunch of warnings and/or errors.

int x = arc4random() %10;
int y = arc4random() %100;
[label1 setText: [NSString stringWithFormat:@"%i", x];
[label2 setText: [NSString stringWithFormat:@"%i", y];

Take a look at the compiler warnings and you'll see why this code is what you need.

Sign up to request clarification or add additional context in comments.

Comments

0
NSLog(@"Message Here: %d", x);

etc.

Comments

0

As noted by XenElement, NSLog is the basic method for printing to the console. printf also works as expected but there's little reason to use it.

I like to use Marcus Zarra's DLog macro. It gives a little more information than NSLog and it only prints to the console when you're running a debug build. The source is available on Marcus' blog.

To set the DEBUG flag, go to the build settings for the debug build and enter DEBUG under "Preprocessor Macros".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.