如何在char和NSString中修复此错误

时间:2017-06-20 16:58:02

标签: ios unix terminal deb theos

我试图把NSString放在一个修改过的路径上,因为我不能使用路径" / subfolder" 我的代码是:

NSString *bath =[NSString stringWithFormat:@"%@/path", path];
pid_t pid;
int status;
const char *argv[] = {"mkdir", bath, NULL};
posix_spawn(&pid, "/bin/mkdir", NULL, NULL, (char* const*)argv, NULL);
waitpid(pid, &status, WEXITED);`

https://i.stack.imgur.com/0ws5N.jpg

1 个答案:

答案 0 :(得分:0)

NSStrings需要先转换为C字符串,然后才能将它们包含在argv数组中。

这样做:

NSString *bath =[NSString stringWithFormat:@"%@/path", path];
const char *cBath = [bath UTF8String];
if (cBath == NULL)
{
    print("cBath is null ; why is path also null?");
    return;
}
pid_t pid;
int status;
const char *argv[] = {"mkdir", cBath, NULL};
posix_spawn(&pid, "/bin/mkdir", NULL, NULL, (char* const*)argv, NULL);
waitpid(pid, &status, WEXITED);`