0

I am doing a Cocoa tutorial where I need to count the characters in a field, then output something like 'the_string_i_am_count' has 21 characters.

I have managed to get the string, count it, and output the count but I have no idea how to output the count along with the string and the other info.

How would I do this?

-(IBAction)countCharacters:(id)sender
{
    //i had to connect this to the class also to make it get the value.
    //NSString *string = [inputField stringValue];

    //get the number of chars
    NSUInteger length = [[inputField stringValue] length];

    [outputField setIntValue:length]; //string];
    //[outputField setStringValue: @"'s' has %d characters.", string, length];
}

Semi working code:

-(IBAction)countCharacters:(id)sender
{
    //i had to connect this to the class also to make it get the value.
    NSString *string = [inputField stringValue];

    //get the number of chars
    NSUInteger length = [[inputField stringValue] length];

    [outputField setStringValue:[NSString stringWithFormat:@"'%s' has %d characters.", string, length]];
}

1 Answer 1

1
[outputField setStringValue:[NSString stringWithFormat:@"%@ has %@ characters.", string, length]];
Sign up to request clarification or add additional context in comments.

4 Comments

Could you update your original post with the updated code that you are using? This way I could help you further.
There you go. string is being set because if I do something like string = @"test"; and output it then it works fine. Just not in the stringWithFormat
Use my code that I used above. NSString isn't an %s, it's a %@ because it is a Obj-C object.
Yea it crashes if I use %@ for the int but %@ works for the string thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.