我有一個程序,它應該引起2檔,以使用FileMerge
進行比較。如何強制的命令行參數字符串被引用
這並不工作,但偶爾會失敗。我懷疑這是當作爲參數傳遞的路徑包含空格字符。
下面的代碼片段構建任務並啓動它。
NSTask *task = [[NSTask alloc] init];
NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
[task setStandardInput:[NSPipe pipe]]; //The magic line that keeps your log where it belongs
NSFileHandle *file = [pipe fileHandleForReading];
[task setLaunchPath: @"/bin/sh"];
NSArray *arguments = [NSArray arrayWithObjects:
@"-c" ,
[[NSUserDefaults standardUserDefaults] stringForKey:PREF_COMPARE_COMMAND],
@"Compare", // $0 place holder
source,
target,
nil];
[task setArguments:arguments];
[task setEnvironment:[NSDictionary dictionaryWithObject:@"/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin" forKey:@"PATH"]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(pipeReadCompletionNotification:)
name:NSFileHandleReadCompletionNotification
object:file];
[file readInBackgroundAndNotify];
[task launch];
我已經嘗試了很多選項,嘗試使用引號將空格或引號括起來,但沒有成功。我歡迎任何建議。
典型的參數作爲運行的結果是: -
"-c",
"opendiff $1 $2",
Compare,
"/Users/ian/temp/Indian Pacific/RailRes Travel Documentation1.pdf",
"/Users/ian/temp/Indian Pacific/RailRes Travel Documentation.pdf"
我試圖
[source stringByReplacingOccurrencesOfString:@" " withString:@"\\ "],
[source stringByReplacingOccurrencesOfString:@" " withString:@"\ "],
的第一實際插入\\
第二產生一個編譯錯誤unknown escape sequence
我試圖肯Thomases的建議(知道我的名字沒有'
)
[[@"'" stringByAppendingString:source] stringByAppendingString:@"'"],
[[@"'" stringByAppendingString:target] stringByAppendingString:@"'"],
不幸的是這導致了爭論
"-c",
"opendiff $1 $2",
Compare,
"'/Users/ian/temp/Indian Pacific/RailRes Travel Documentation1.pdf'",
"'/Users/ian/temp/Indian Pacific/RailRes Travel Documentation.pdf'"
,並以同樣的方式失敗。 /Users/ian/temp/Indian does not exist
編輯_______________________工作守則_____________________________________
NSArray *arguments = [NSArray arrayWithObjects:
@"-c" ,
[NSString stringWithFormat:@"%@ '%@' '%@'", @"opendiff", source, target],
nil];
您是否嘗試用「\」替換「」? – danh