开发者

Objective C error: Passing argument 1 of 'setStringValue:' from incompatible pointer type

开发者 https://www.devze.com 2023-01-06 08:15 出处:网络
Ok here is part of the code that is causing the error: char charlieReturn[10000]; char开发者_高级运维lieReturn[10000] = system(\"osascript /applications/jarvis/scripts/getTextCharlieResponce.scpt\");

Ok here is part of the code that is causing the error:

    char charlieReturn[10000];
char开发者_高级运维lieReturn[10000] = system("osascript /applications/jarvis/scripts/getTextCharlieResponce.scpt");


 self.charlieOutput.stringValue = charlieReturn;

The getTextCharlieResponce.scpt returns something like this: "Hi my name is charlie" and maybe sometimes it will be longer than that. The script returns it plain text. I need help FAST!

Thanks in advance! :D

Elijah


Unfortunately there are many problems in your code.

  1. The C function system does not return the standard output of the script as char*.

  2. Even with a function which returns char*, you can't assign it to a array of char as you did:

     char* str="aeiou";
     char foo[100];
     foo[100]=str;   /* doesn't work */
     foo=str;   /* doesn't work either */
    
  3. Cocoa's string class, NSString*, is not a C string char*, although you can easily convert between the two:

     NSString* str=[NSString stringWithUTF8String:"aeiou"];
    

If you want a string out of a call to an Apple script, you need to do the following:

  1. Prepare an NSAppleScript:

    NSDictionary* errorDict;
    NSAppleScript* script=[[NSAppleScript alloc] 
                            initWithContentsOfURL:[NSURL fileURLWithPath:@"path/to/script" ]
                                            error:&errorDict];
    
  2. Execute and get a reply:

    NSAppleEventDescriptor* desc=[script executeAndReturnError:&errorDict];
    NSString* result=[desc stringValue];
    
  3. Release the script:

    [script release];
    

Learn C & Objective-C and have fun!


You're trying to assign a character array to what is presumably an NSString. Try this instead:

self.charlieOutput.stringValue = [NSString stringWithUTF8String:charlieReturn];
0

精彩评论

暂无评论...
验证码 换一张
取 消