【问题标题】:Put all methods into .h file automatically将所有方法自动放入 .h 文件中
【发布时间】:2012-02-08 11:28:25
【问题描述】:

在实现文件 (.m) 中,我有 30.. 方法。如何将他们的描述(全部)自动放入 .h 文件中?

【问题讨论】:

  • 你应该把所有的方法都放在 .h 中吗?最好有一个脚本来接受所有方法并将它们放在私有类别中

标签: iphone objective-c methods


【解决方案1】:

使用正则表达式很难正确完成接缝,但您可以使用 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
  • 我认为您将脚本保存为富文本文档。尝试另存为纯文本。您也可以从gist.github.com/1771131下载它
  • 感谢它现在运行良好,但我仍然有问题.. 这个脚本的目标是“{”在方法宣布后立即开始,例如:-(void)testMethod{ 但在我的情况下,这个大括号在方法头之后开始(在按下返回之后)。获取此类方法时脚本总是停止..
【解决方案2】:

编写一段代码,将提取所有方法定义(使用正则表达式检测它们),然后将其添加到 h 文件和“\;\n”。

【讨论】:

  • 问题是制作这样的程序
【解决方案3】:

Accessorizer 程序(在 Mac App Store 上售价 5 美元)专门用于解决 Xcode 中这些令人讨厌的繁琐工作问题。它可以生成原型以及属性合成、访问器、初始化等。

警告:根据我的经验,它的边缘有点敏感和粗糙。例如,它可能没有意识到一个函数在多行注释中,因此为它提供了一个不需要的原型。但即使考虑到这些怪癖,它也为我节省了超过 5 美元的时间。

他们的网站:http://www.kevincallahan.org/software/accessorizer.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 2011-05-26
    • 1970-01-01
    • 2015-10-08
    • 2013-12-09
    • 1970-01-01
    相关资源
    最近更新 更多