可可教程中的字符串格式

时间:2011-05-15 05:07:48

标签: objective-c cocoa string

我正在做一个Cocoa教程,我需要计算一个字段中的字符,然后输出类似'the_string_i_am_count'的字符有21个字符。

我设法获取字符串,计算它并输出计数但我不知道如何输出计数以及字符串和其他信息。

我该怎么做?

-(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];
}

半工作代码:

-(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 个答案:

答案 0 :(得分:1)

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