How can I put my NSInteger into my NSString stringWithFormat:

my code:

-(IBAction)main:(id)sender
{
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:@"return the clipboard"];


NSAppleEventDescriptor *clipboardTXT = [[script executeAndReturnError:nil]stringValue];

NSInteger *length = [clipboardTXT.stringValue length];

NSString *mystring = [NSString stringWithFormat:@"%", length];


[mybutton setTitle:mystring];
}

p.s. I've tried this but it didn't work for me.

output

有帮助吗?

解决方案

Use the %d specifier.

- (IBAction)main:(id)sender
{
    NSAppleScript *script = [[NSAppleScript alloc] initWithSource:@"return the clipboard"];


    NSAppleEventDescriptor *clipboardTXT = [script executeAndReturnError:nil];

    NSInteger length = [[clipboardTXT stringValue] length];

    NSString *mystring = [NSString stringWithFormat:@"%d", length];

    [mybutton setTitle:mystring];
}

EDIT:

Your problem is a totally different one then what you're asking. Why didn't you include the error message the first time? Time waster...

It obviously points out that the problem has to be somewhere around your stringValue calls. If you had googled it you certainly would've found that this error message means the object you're sending stringValue to, doesn't have such a method. executeAndReturnError returns an NSAppleEventDescriptor instance. You then call stringValue which will return an NSString, not an NSAppleEventDescriptor. After that you're sending a stringValue message to an NSString (it doesn't have such a method). See my edited code.

其他提示

2 things, you want to use the %d specifier and you need to remove the * in your length declaration

NSInteger length = [clipboardTXT.stringValue length];
NSString *mystring = [NSString stringWithFormat:@"%d", length];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top