-3

NSInteger を NSString stringWithFormat に入れるにはどうすればよいですか:

私のコード:

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

psこれを試しましたが、うまくいきませんでした。

出力

4

2 に答える 2

2

指定子を使用し%dます。

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

編集:

あなたの問題は、あなたが求めているものとはまったく異なります。最初にエラー メッセージを含めなかったのはなぜですか? 時間の無駄...

stringValue問題は通話のどこかにあるに違いないことは明らかです。あなたがそれをググったなら、あなたは確かにこのエラーメッセージが送信先のオブジェクトを意味することを発見したでしょう、stringValueそのようなメソッドを持っていません. executeAndReturnErrorインスタンスを返しNSAppleEventDescriptorます。次に、ではなくstringValueを返す which を呼び出します。その後、にメッセージを送信しています(そのようなメソッドはありません)。編集したコードを参照してください。NSStringNSAppleEventDescriptorstringValueNSString

于 2012-10-29T23:48:09.963 に答える
1

%d2つのこと、指定子を使用したいので*、長さ宣言からを削除する必要があります

NSInteger length = [clipboardTXT.stringValue length];
NSString *mystring = [NSString stringWithFormat:@"%d", length];
于 2012-10-29T23:56:00.763 に答える