How to print a variable in Xcode - xcode

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];
}

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.

NSLog(#"Message Here: %d", x);
etc.

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".

Related

CGPoint giving the same value for x and y?

This is a strange bug I'm experiencing when testing on an old iPod. I'm trying to get location values on touchesMoved, but the y always outputs value equal to x. If I log using NSStringFromCGPoint, then I get the correct output. How come accessing x and y separately is not working?
CGPoint location = [singleTouch locationInView:self.view];
NSLog(#"Location using NSStringFromCGPoint %#", NSStringFromCGPoint(location)); // output: {27, 136}
NSLog(#"Location using x and y %f %f",location.x, location.y); // output: 27, 27
It appears the problem didn't haven't anything to do with CGPoint but rather with the plist parameters.
This question solved my issue.

Integer variable to label. Xcode 4.2

This is probably a noob question but I've been trying to figure it out for a few hours now and I can't. I want to make a label show the number of an integer variable.
What I've done is that I've declared a variable x as integer, I give that variable the number 100 and then I am trying to show that number to a label.
I tryed this but it isn't working.
x = 100;
label.text = [[NSString alloc] initWithFormat: #"%.2f", x];
This takes place inside a button.
Thanks in advance
You have to use '%d' instead of '%.2f'. '%f' is for float, not int.

How do I get "Page Attributes" option in Cocoa print dialog?

The program I'm writing runs under OS X 10.5 Leopard. My target has its Base SDK and Deployment Target both set to Mac OS X 10.5. When I initiate printing, my print dialog doesn't show the Page Attributes option in which the user can select page size and orientation.
Other programs running under Leopard do show this option:
Here's the code that initiates printing:
-(void)print {
NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
TemperaturePressurePrintView *printView = [[TemperaturePressurePrintView alloc] initWithFrequencies:frequencies];
if (printView) {
[[NSPrintOperation printOperationWithView:printView printInfo:printInfo] runOperation];
[printView release];
}
}
What do I need to do to get Page Attributes to show up in my print dialog?
This was a tough thing to search for because the results were mostly about using the print panel, not programming one. I finally found a clue on Cocoabuilder where it mentions NSPrintPanelOptions and NSPrintPanel's -setOptions: method.
This code accomplishes what I need:
-(void)print {
NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
TemperaturePressurePrintView *printView = [[TemperaturePressurePrintView alloc] initWithFrequencies:frequencies];
if (printView) {
NSPrintOperation *op = [NSPrintOperation printOperationWithView:printView printInfo:printInfo];
[[op printPanel] setOptions:[[op printPanel] options] | NSPrintPanelShowsPageSetupAccessory];
[op runOperation];
[printView release];
}
}
It's a few years after the original answer and macOS Sierra seems to have introduced a bug into the behaviour of panels that have the 'NSPrintPanelShowsPageSetupAccessory' option set. Invalid values, such as a ridiculously large scale, cause crashes instead of displaying an alert sheet.
Fortunately there is a workaround. Using
NSPrintPanelShowsPaperSize | NSPrintPanelShowsOrientation | NSPrintPanelShowsScaling
instead seems to result in a panel that works fine.

How do you set a label text as something from an NSMutableArray?

I am trying to understand how to set a label to be the text from an array when you press the button. When I press the button, the label disappears, and then nothing comes up. No crashes in code.
Relevant code:
-(void)setupArray {
wordArray = [[NSMutableArray alloc] init];
[wordArray addObject:#"test1"];
[wordArray addObject:#"test2"];
[wordArray addObject:#"test3"];
}
- (IBAction)start:(id)sender {
int value = (arc4random() % 3) + 1;
self.typeThis.text = [self.wordArray objectAtIndex:value];
}
typeThis is the label name, and I think I have hooked up everything already, i.e. set up the buttons/delegates/etc...I don't understand why it isn't working. Can anybody help?
considering you have bound everything properly and you are not under ARC. Here is a thing that might cause you the issue.
when you are allocating wordArray you can try using following code snippet.
NSMutableArray tempArray = [[NSMutableArray alloc] init];
self.wordArray = tempArray;
[tempArray release];
if you are under ARC you can try self.wordArray = [NSMutableArray array];
then add objects to self.wordArray i.e.[self.wordArray addObject:#"test1"];. Here is some explanation about arc4random().
EDIT :
Here's a public spec for Automatic Reference Counting and a quote from the public iOS 5 page:
Automatic Reference Counting (ARC) for Objective-C makes memory
management the job of the compiler. By enabling ARC with the new Apple
LLVM compiler, you will never need to type retain or release again,
dramatically simplifying the development process, while reducing
crashes and memory leaks. The compiler has a complete understanding of
your objects, and releases each object the instant it is no longer
used, so apps run as fast as ever, with predictable, smooth
performance.
It is possible to detect if ARC is enabled. Simply add the following snippet to any file that requires ARC.
#ifndef __has_feature
#define __has_feature(x) 0 /* for non-clang compilers */
#endif
#if !__has_feature(objc_arc)
#error ARC must be enabled!
#endif
More info :
http://clang.llvm.org/docs/LanguageExtensions.html#__has_feature_extension
HTH.
Your '+1' is giving you a result between 1 and 3, and your indexes are from 0 to 2, so I'd expect it to go wrong one time in 3.
Is this under ARC? If so, is wordArray declare as strong?

How do I make 2 labels add together with each other?

Hi I'm kinda new to Xcode and I'm trying to make an app where you press a button and the number will go up; and I have 2 buttons and 2 labels. I've got it to where the 2 labels will count up, but now I'm wanting the numbers from both labels to add together and show in a different label. Is there any line I can add to the buttons to make them just count up in the other label as well or do I need to have a separate action and/or button?
Thanks
Straight up:
int sum = [[label1 text] intValue] + [[label2 text] intValue];
label3.text = [NSString stringWithFormat:#"%#", sum];
Should work, just make sure to replace the pointers I used with the ones you're using.
esqew's answer would do the trick, but the format specifier is incorrect.
If the variable sum is in fact an int ...
label3.text = [NSString stringWithFormat:#"%#", sum];
should be:
label3.text = [NSString stringWithFormat:#"%d", sum];
%# is for Objective-C objects, an int is not an Objective-C object.
Reference:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html

Resources