【发布时间】:2012-02-08 11:28:25
【问题描述】:
在实现文件 (.m) 中,我有 30.. 方法。如何将他们的描述(全部)自动放入 .h 文件中?
【问题讨论】:
-
你应该把所有的方法都放在 .h 中吗?最好有一个脚本来接受所有方法并将它们放在私有类别中
标签: iphone objective-c methods
在实现文件 (.m) 中,我有 30.. 方法。如何将他们的描述(全部)自动放入 .h 文件中?
【问题讨论】:
标签: iphone objective-c methods
使用正则表达式很难正确完成接缝,但您可以使用 awk 完成:
https://gist.github.com/1771131
#!/usr/bin/env awk -f
# print class and instance methods declarations from implementation
# Usage: ./printmethods.awk class.m or awk -f printmethods.awk class.m
/^[[:space:]]*@implementation/ {
implementation = 1;
}
/^[[:space:]]*@end/ {
implementation = 0;
}
/^[[:space:]]*[\-\+]/ {
if(implementation) {
method = 1;
collect = "";
}
}
/[^[:space:]]/ {
if(implementation && method) {
p = index($0, "{");
if(p == 0) {
if(collect == "")
collect = $0
else
collect = collect $0 "\n";
} else {
method = 0;
# trim white space and "{" from line end
gsub("[\{[:space:]]*$", "", $0);
collect = collect $0;
# trim white space from start
gsub("^[[:space:]]*", "", collect);
print collect ";"
}
}
}
【讨论】:
awk: syntax error at source line 1 source file printmethods.awk context is >>> {\ <<< rtf1\ansi\ansicpg1251\cocoartf1038\cocoasubrtf350 awk: illegal statement at source line 2 source file printmethods.awk awk: illegal statement at source line 2 source file printmethods.awk 2 missing }'s
-(void)testMethod{ 但在我的情况下,这个大括号在方法头之后开始(在按下返回之后)。获取此类方法时脚本总是停止..
编写一段代码,将提取所有方法定义(使用正则表达式检测它们),然后将其添加到 h 文件和“\;\n”。
【讨论】:
Accessorizer 程序(在 Mac App Store 上售价 5 美元)专门用于解决 Xcode 中这些令人讨厌的繁琐工作问题。它可以生成原型以及属性合成、访问器、初始化等。
警告:根据我的经验,它的边缘有点敏感和粗糙。例如,它可能没有意识到一个函数在多行注释中,因此为它提供了一个不需要的原型。但即使考虑到这些怪癖,它也为我节省了超过 5 美元的时间。
他们的网站:http://www.kevincallahan.org/software/accessorizer.html
【讨论】: