iHackerMe
iHackerMe

Reputation: 591

Can I use NSString inside system("");?

I am currently working on a tweak where the user writes the argument of a command (e.g. sbalert from sbutils) via a preference bundle. I am able to save it to an NSString but not able to use it as the argument of the sbalert command. Is this possible? Is there and alternative? My code is

int main(int argc, char **argv, char **envp) {

NSString *string1 = @"Hello World";
NSLog(@"%@", string1);
system(" sbalert -t %@", string1);

    return 0;
}

// vim:ft=objc

Note that this is a test, so the NSString is not equal to the text in the Preference Bundle still theos gives me an error while compiling.

Upvotes: 0

Views: 267

Answers (1)

user529758
user529758

Reputation:

No. The system function doesn't take a format string, and even if it did, you couldn't use the %@ format specifier - that's for Cocoa only and not available in the C standard library. You have to preformat your string like:

NSString *cmd = [NSString stringWithFormat:@"sbalert -t '%@'", string1];
system([cmd UTF8String]);

Upvotes: 8

Related Questions