Search code examples
objective-cstringquotesquotation-marks

Alternates to quotation marks in Objective C


I have this line of code in a method:

system("osascript -e 'tell app "System Events" to restart'");

As you can see, I've got two quotation marks, and since these terminal commands have to be so specific, I need to know another way to run a system command from ObjC. I've already tried using '' and the / method but that didn't work out.


Solution

  • You need to "escape" the quote characters to tell the compiler that the should be part of the string rather than delimiters of the string. You say you "tried using … the / method", but you got the wrong character. You escape characters using the backslash, not the forward slash:

    -(IBAction)reboot:(id)sender{
        system("osascript -e 'tell app \"System Events\" to restart'");
    }