views:

105

answers:

2

I'm in my second day of learning Objective-C, Cocoa and IB. This is probably something really simple but I cannot work it out.

Basically, I have a form with a NSTextField, when the user types in this field and clicks an OK button the application will display an alert saying Hello followed by the value of text field.

It's all working apart from the string concatenation. I'm using the following code to concatenate the string "Hello" and the NSTextField value:

NSString *nameText = [NSString stringWithFormat:@"Hello %s", [nameTextField stringValue]];

When the user clicks the OK button an alert displays "Hello ‡√Ÿpˇ"!

+1  A: 

Camsoft,

The NSString of Obj-C is an object, correct the format call with:

NSString *nameText = [NSString stringWithFormat:@"Hello %@",[nameTextField stringValue]];

Note the %@ instead of %s .

Frank

Frank C.
Bingo! I see, that makes sense. Thanks!
Camsoft
+1  A: 

stringValue returns NSString object and %s expects c-string parameter. Try to use %@ instead:

NSString *nameText = [NSString stringWithFormat:@"Hello %@", [nameTextField stringValue]];
Vladimir