我有一个创建Objective-C代码的OSX应用程序。换句话说,写入两个文件,.h和.m。NSString writeToFile失去缩进
要写入文件,我使用NSString writeToFile原子地为true,编码NSUTF8StringEncoding。
尽管我的两个文件是正确创建的文本,所有格式都会丢失。即使导入到Xcode中,所有内容都与左侧对齐。虽然我知道我可以选择文本并使用Ctrl + i缩进它,但这不是我想要的解决方案。
现在,当我将NSString复制到剪贴板并将其粘贴到Xcode中时,格式保持不变。
有没有人知道一种方法,我可以保持在Xcode中的文本缩进的方式,而不必写有关打开多少个大括号的规则?这里是什么,我到目前为止做了一些修改示例代码:
NSString *code = @"for (int i = 0; i < 10; i++) {\n";
code = [code stringByAppendingString:@"NSLog(@\"HelloWorld\");\n"];
code = [code stringByAppendingString:@"}\n"];
// If I run the two lines below, then my NSString is copied to the clipboard. When I paste into Xcode, formatting stays as it should.
[[NSPasteboard generalPasteboard] clearContents];
[[NSPasteboard generalPasteboard] setString:code forType:NSStringPboardType];
// If I run the code below instead, then two files are created. But they do not keep the formatting.
NSSavePanel *panel = [NSSavePanel savePanel];
[panel setNameFieldStringValue:@"MyClass"];
[panel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
if (result == NSFileHandlingPanelOKButton) {
NSError *errorH = nil;
NSError *errorM = nil;
NSString *pathH = [[[panel URL] path] stringByAppendingString:@".h"];
NSString *pathM = [[[panel URL] path] stringByAppendingString:@".m"];
[code writeToFile:pathH atomically:true encoding:NSUTF8StringEncoding error:&errorH];
[code writeToFile:pathM atomically:true encoding:NSUTF8StringEncoding error:&errorM];
if (!errorH && !errorM) {
NSLog(@"Success");
} else {
NSLog(@"Error Saving Files");
}
}
}];
编辑:没有得到一个明确的办法典雅做到这一点后,我根本就写我自己的一些格式的代码来模仿格式DONE由Xcode。它不工作100%正确...例如,当我加入多个项目使用数组:
@[@"string1",
@"string2",
@"string3"];\n
这并不正确停留缩进到阵列开始在那里。我敢肯定还有其他一些我的代码无法处理的情况。虽然不是很大。如果稍后想到解决方案,我可以实施它。但是现在,格式化并不像预期的那么漂亮。这里有一个方法,以及一个辅助方法,任何人都可以用它来实现类似的功能(处理标签和大括号只)
- (NSString *) formatWithIdentationForExport : (NSString *) theString {
NSString *s = @"";
NSMutableArray *fileLines = [[NSMutableArray alloc] initWithArray:[theString componentsSeparatedByString:@"\n"] copyItems: YES];
int numberOfCurlyBraces = 0;
for (int i = 0; i < fileLines.count; i++) {
NSString *currentLine = fileLines[i];
int numberOfOpenBracesInLine = [self getNumberOfOccurancesOf:@"{" inString:currentLine];
int numberOfCloseBracesInLine = [self getNumberOfOccurancesOf:@"}" inString:currentLine];
numberOfCurlyBraces -= numberOfCloseBracesInLine;
for (int j = 0; j < numberOfCurlyBraces; j++) {
currentLine = [NSString stringWithFormat:@"\t%@", currentLine];
}
currentLine = [currentLine stringByAppendingString:@"\n"];
s = [s stringByAppendingString:currentLine];
numberOfCurlyBraces += numberOfOpenBracesInLine;
}
return s;
}
- (int) getNumberOfOccurancesOf : (NSString *) substring inString : (NSString *) str {
int count = 0, length = (int)[str length];
NSRange range = NSMakeRange(0, length);
while(range.location != NSNotFound) {
range = [str rangeOfString:substring options:0 range:range];
if(range.location != NSNotFound) {
range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
count++;
}
}
return count;
}
什么是NSUTFStringEncoding?你为自己创造了它吗? –
哎呦,我在那个上犯了一个错字。它是NSUTF8StringEncoding。我尝试了其他一些编码选项,但都没有改变格式。但他们做到了这一点,所以Xcode不再为我的代码着色代码。所以我坚持使用NSUTF8StringEncoding。 – Kjell
让我们看看你在代码中做了什么。我过去编程生成了一个实现文件。原始格式保持不变。所以你做错了什么。 –