简体   繁体   English

Xcode重构文件名

[英]Xcode refactor name of file

As we know we can change name of file created in Xcode by option "Refactor -> Rename" (By selecting class name after @interface keyword). 众所周知,我们可以通过“重构->重命名”选项更改Xcode中创建的文件的名称(通过在@interface关键字之后选择类名称)。

It works well, but I found that it doesn't change/rename the class name from header comment. 它运行良好,但是我发现它不会更改/重命名标题注释中的类名称。 I know this is minor one, but just wanted to know that, is this default behaviour of "Rename" option OR Am I doing anything wrong? 我知道这是次要的,但只是想知道,这是“重命名”选项的默认行为,还是我做错了什么? What is solution for reflecting it in header comment also? 在标题注释中反映它的解决方案是什么?

Thanks 谢谢

You have to manually search and replace all occurrences of the old filename. 您必须手动搜索并替换所有出现的旧文件名。 Xcode's refactor tools are quite limited. Xcode的重构工具非常有限。 Xcode can't automatically rename tokens in comments. Xcode无法自动重命名注释中的标记。

If you often refactor code you might want to look at AppCode , an Objective-C IDE that has way better refactoring tools. 如果您经常重构代码,则可能需要查看AppCode ,它是一种拥有更好重构工具的Objective-C IDE。 AppCode can rename tokens in comments. AppCode可以在注释中重命名标记。

Refactoring only looks for the symbol in code... Which means that you're not doing anything wrong. 重构仅在代码中寻找符号……这意味着您没有做错任何事情。

If the name is something like AMGMainViewTableController , you should be able to do a "find and replace" (Edit->Find->Find and Replace in Workspace) without problems... But I'd avoid doing that with shorter, less specific class names that may be part of a larger one, such as AMGMainViewTable , which would conflict with AMGMainViewTableController . 如果名称类似于AMGMainViewTableController ,则应该可以进行“查找并替换”(在工作区中“编辑”->“查找”->“查找并替换”)...但是我会避免使用较短,不太具体的内容类名称可能是较大名称的一部分,例如AMGMainViewTable ,它将与AMGMainViewTableController冲突。

I have written a bash script that you can add to Xcodes behaviors and manually execute it when needed. 我已经编写了一个bash脚本,您可以将其添加到Xcodes行为中,并在需要时手动执行它。 Of course it can be used in build phases as well. 当然,它也可以在构建阶段中使用。

rename.sh 重命名

cd $XcodeWorkspacePath
cd ..

find * -type f \( -name '*.h' -or -name '*.m' -or -name '*.mm' -or -name '*.swift' \) -exec sh -c '
  for file do
    filename=$(basename "$file")
    if egrep -m1 "(\/\/  .*(\.h|\.m{1,2}|\.swift)+ *$)" "$file"
    then
      if grep -Fxq "//  $filename" "$file"
      then
        echo "$file - Correct"
        continue;
      else
        sed -E -i.bak "s/(\/\/  .*(\.h|\.m{1,2}|\.swift)+ *$)/\/\/  $filename/" "$file"
        echo "$filename - Wrong\n^^^^^^^^^^^^^^^^^^^^^^^^^^"
      fi
    fi
  done
' sh {} + &> rename.out 

Don't forget to make the script executable 不要忘记使脚本可执行

chmod +x chmod + x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM