格式化Objective-C代码

bri*_*ear 7 iphone xcode objective-c ios

我将Safari中的Objective-C示例剪切并粘贴到Xcode中,但格式化却搞砸了.我知道我可以为{和\ r进行多次查找和替换,但是有一个按钮可以修复吗?Xcode选项/插件.TextEdit插件等?

答:我在TextWrangler中创建了一个AppleScript

tell application "TextWrangler"
activate
replace "- (" using "\\r- (" searching in text 1 of text document "untitled text" options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
select find window
select text 1 of find window
replace ";" using ";\\r" searching in text 1 of text document "untitled text" options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
select find window
replace "{" using "{\\r" searching in text 1 of text document "untitled text" options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
select find window
replace "//" using "\\r//" searching in text 1 of text document "untitled text" options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
end tell
Run Code Online (Sandbox Code Playgroud)

小智 3

在 Xcode 4 中重新缩进应该可以解决您可能遇到的大多数格式问题。但是,如果您对结果仍然不满意,则必须转向第三方。

我曾经经常更改代码格式。我去更新一些旧代码,却发现它是使用已弃用的、现在不熟悉的格式编写的。

我一直在使用Uncrustify将旧文件改编为新格式。它有很多选择,但仍然缺乏。然而,对于大多数情况来说它应该足够了。