Objective C - OSX NSInterger to NSString
Hi i am reading a book by aaron hillegass on cococa programming and I doing one of the mini tasks he asks us to do.
the task is to create an application that has one window open and has 1 input text field, a button and a label.
when a user inputs some text and presses the button, the label displays the text and the length of the text inputted.
Here is what I have got so far
//retrieve text from textfield
NSString *string = [textFieldInput stringValue];
//retrieve length of text and store 开发者_如何学编程in NSInteger called length
NSInteger length = [string length];
//store length in string format
NSString *string_length = [NSString stringWithFormat:@"%d", length];
//join strings
NSString *full_string = [string stringByAppendingString:(@"has ",string_length,@" characters")];
//set label text
[textField setStringValue:full_string];
however the actual string is shown and the characters string is shown, just not the string_length. any suggestions and am i going about this in the right way? Thanks.
NSString *fullString = [string stringByAppendingFormat:@"has %@ characters", string_length];
//retrieve text from textfield
NSString *string = [textFieldInput stringValue];
NSString *fullString = [string stringByAppendingFormat:@" has %d characters", [string length]];
//set label text
[textField setStringValue:fullString];
Your usage of stringByAppendingString:
is wrong.
If I understand you correctly, you are trying to pass a list of strings that should be appended to string
but that method only takes a single string argument.
You can try the following:
NSString* fullString = [string stringByAppendingString:[NSString stringWithFormat:@"%@ %@ %@", @"has ", string_length, @" characters"]];
精彩评论