Lefora Free Forum
login join
Loading
452 views

Newbie formatting strings problem

Page 1
1–3
rookie - member
6 posts

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.



   

rookie - member
6 posts

G'day All,

This worked:

Code:
#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.

?
288 posts

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

__________________
Page 1
1–3

Locked Topic


You must be a member to post in this forum

Join Now!