-3

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

Community
  • 1
  • 1
atomikpanda
  • 1,845
  • 5
  • 33
  • 47
  • 3
    Seing the many errors in this code and your other questions, I strongly suggest to start with a good book about programming. – Eiko Oct 30 '12 at 00:01

2 Answers2

2

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.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • It should. What does it do instead of what you expect? – Alex Wayne Oct 29 '12 at 23:49
  • 1
    You're using a pointer when you shouldn't. The `length` method returns a primitive NSInteger (not an object). See my answer. – DrummerB Oct 29 '12 at 23:50
  • @AlexWayne I added my output. – atomikpanda Oct 29 '12 at 23:51
  • 1
    @s0ulp1xel Actually, none of that code makes sense. The types are wrong, the methods are wrong (hence the problem in your output), pointers where they shouldn't be, wrong/missing format specifiers... – Eiko Oct 30 '12 at 00:03
  • You're also randomly mixing up dot syntax and bracket syntax. `stringValue` and `length` are both methods, not properties. – DrummerB Oct 30 '12 at 00:07
  • @DrummerB I did google it but I guess I didn't find what your answer is stating. Thanks for the help – atomikpanda Oct 30 '12 at 10:59
1

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];
Kyle
  • 434
  • 2
  • 10