Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c .m文件中的Shell命令?_Objective C_System - Fatal编程技术网

Objective c .m文件中的Shell命令?

Objective c .m文件中的Shell命令?,objective-c,system,Objective C,System,我尝试使用system()在.m文件中运行shell命令,但没有效果。如何从Xcode中的.m文件运行shell命令 #import "AppController.h" @implementation AppController - (void)awakeFromNib { [output setStringValue:@"Awaiting orders, sir."]; } - (IBAction)convertFile:(id)sender { NSString *st

我尝试使用system()在.m文件中运行shell命令,但没有效果。如何从Xcode中的.m文件运行shell命令

#import "AppController.h"


@implementation AppController
- (void)awakeFromNib
{
    [output setStringValue:@"Awaiting orders, sir."];
}

- (IBAction)convertFile:(id)sender
{
    NSString *string = [input stringValue];
    NSString *string2 = @"tar czvf ProjectFiles.tar.gz ";



    NSString *stringCmd = [NSString stringWithFormat:@"%@ %@", string2, string];

    system(stringCmd);
}
@end

NSTask是另一种选择。查看

任务是另一种选择。请检查

您的代码失败,因为系统需要一个C字符串(char*),而您的stringCmd是一个NSString*

尝试使用:

system([stringCmd cStringUsingEncoding:NSASCIIStringEncoding]);
还要注意,系统在shell中运行该命令,因此要注意潜在的安全问题。 如果输入字符串为@“tt.txt;echo \“aa\”,则代码将运行

tar czvf ProjectFiles.tar.gz tt.txt; echo "aa"
这可能不是您想要的。

您的代码失败,因为系统需要一个C字符串(char*),而您的stringCmd是一个NSString*

尝试使用:

system([stringCmd cStringUsingEncoding:NSASCIIStringEncoding]);
还要注意,系统在shell中运行该命令,因此要注意潜在的安全问题。 如果输入字符串为@“tt.txt;echo \“aa\”,则代码将运行

tar czvf ProjectFiles.tar.gz tt.txt; echo "aa"

这可能不是你想要的。

是的。您是如何使用它的?
system()
works。请张贴相关代码。它是。您是如何使用它的?
system()
works。请发布相关代码。所以-(void)setLaunchPath:(NSString*)path?NSTask确实是系统的一个很好的替代方案。对于使用示例,您可以在stackoverflow上搜索NSTask,例如So-(void)setLaunchPath:(NSString*)path?NSTask确实是系统的一个很好的替代品。对于使用示例,您可以在stackoverflow上搜索NSTask,例如echo“aa”只是一个示例。它也可以是“rm-rf/”或其他破坏性命令。echo“aa”只是一个例子。它也可以是“rm-rf/”或其他破坏性命令。