Newbie formatting strings problem
G'day All,
Apologies for such a question but something escapes me.
Why does:
#import "controller.h"
@implementation controller
-(IBAction)PrintFloat:(id)sender
{
float myVar;
myVar = [txtFloatIn floatValue];
[txtStringOut setStringValue:(@"%f", myVar)];
}
@end
Produce:
incompatible type for argument 1 of 'setStringValue:
If I use NSLog to print the string it formats perfectly but I can't stick the string in an NSTextField.
All advice greatly appreciated.
Cheers,
A.
G'day All,
This worked:
#import "controller.h"
@implementation controller
-(IBAction)PrintFloat:(id)sender
{
float myVar;
myVar = [txtFloatIn floatValue];
[txtStringOut setStringValue:[NSString stringWithFormat:@"The number is %f",myVar]];
}
@end
Thank god for the net.
Cheers,
A.
Yup, you have to format it for him. setStringValue expects a string ! So, you could express it in two lines as:
NSString* sTemp = [NSString stringWithFormat:@"bla bla ...];[txtStringOut setStringValue:sTemp] ;
or - as you've discovered, you can one line it as:
[txtStringOut setStringValue:[NSString stringWithFormat:.....];
or even (with greater clarity and simplicity):
txtStringOut.stringValue = [NSString stringWithFormat:.....];
// maybe Obj/C v3.0 will one day support a keyword like newt (meaning new temporary), then we wouldn't need [ square brackets ] everywhere and say clearly and simply:
txtStringOut.stringValue = newt stringWithFormat:.............. ; // wishful thinking