4

对于 NSLogger 项目,我们希望实现直接跳转回 XCode 到文件中发出日志条目的行的功能。人们会期望使用这样的命令行工具会很容易:

xed --line 100 ~/work/xyz/MainWindowController.m

但这会导致意外错误:

2011-10-31 17:37:36.159 xed[53507:707] 错误:错误域=NSOSStatusErrorDomain 代码=-1728“操作无法完成。(OSStatus 错误-1728。)”(例如:说明符要求第 3 个,但只有 2 个。基本上,这表示运行时解析错误。) UserInfo=0x40043dc20 {ErrorNumber=-1728, ErrorOffendingObject=}

另一个想法是使用 AppleScript 来告诉 XCode 执行所需的步骤,但我找不到可行的解决方案。

因此,任何达到预期效果的解决方案都将不胜感激。

参考 GitHub 上的 NSLogger 问题:https ://github.com/fpillet/NSLogger/issues/30

4

1 回答 1

0

xed工具似乎工作正常:

xed --line 100 /Users/Anne/Desktop/Test/TestAppDelegate.m

错误

例如:说明符要求第 3 个,但只有 2 个

上面的错误只是表明请求的行超出了范围。

解决方案

在执行之前检查行号是否实际存在xed

快速编写的示例

// Define file and line number
NSString *filePath = @"/Users/Anne/Desktop/Test/TestAppDelegate.m"; 
int theLine = 100;

// Count lines in file
NSString *fileContent = [[NSString alloc] initWithContentsOfFile: filePath];
unsigned numberOfLines, index, stringLength = [fileContent length];
for (index = 0, numberOfLines = 0; index < stringLength; numberOfLines++)
    index = NSMaxRange([fileContent lineRangeForRange:NSMakeRange(index, 0)]);

// The requested line does not exist
if (theLine > numberOfLines) {
    NSLog(@"Error: The requested line is out of range.");

// The requested line exists
} else {

    // Run xed through AppleScript or NSTask
    NSString *theSource = [NSString stringWithFormat: @"do shell script \"xed --line %d \" & quoted form of \"%@\"", theLine, filePath];        
    NSAppleScript *theScript = [[NSAppleScript alloc] initWithSource:theSource];
    [theScript executeAndReturnError:nil];

}

笔记

确保正确计算行数: Counting Lines of Text

于 2011-11-01T22:22:56.773 回答