문제

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