Is it possible to make system call in Objective-C?
I have the following code:
if (!system("ls -l")) {
NSLog(@"Successfully executed");
} else {
NSLog(@"Error while executing the command");
}
How to get the output?
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is a perfect candidate for using
-[NSFileManager contentsOfDirectoryAtPath:error:]instead of wrapping a built-in shell function and parsing the output. For general-purpose commands where you need the output — and where there is no equivalent functionality “for free” in Cocoa (it just takes time to learn the available APIs) — NSTask is generally a far better alternative thansystem().