简体   繁体   中英

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).

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 can't automatically rename tokens in comments.

If you often refactor code you might want to look at AppCode , an Objective-C IDE that has way better refactoring tools. AppCode can rename tokens in comments.

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 .

I have written a bash script that you can add to Xcodes behaviors and manually execute it when needed. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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