How to rename the class name in swift?

前端 未结 7 1867
攒了一身酷
攒了一身酷 2021-02-12 03:33

I am trying to rename the class name in swift but it gives me error Can\'t refector swift code. I want to know the best way to rename a file that will reflect in t

相关标签:
7条回答
  • 2021-02-12 04:12

    Now it's possible in Xcode 9.

    Right click on class name and choose Rename.

    0 讨论(0)
  • 2021-02-12 04:17

    Currently you can't refactor in Swift. However, you can do Find menu > Find and Replace in Project. (Carefully)

    Edit: Xcode 9 allows you to refactor.

    Highlight your property/function and choose Editor Menu -> Refactor -> Rename

    Wait a second or two for Xcode 9 to scan your project for all occurrences and then type the new name.

    0 讨论(0)
  • 2021-02-12 04:22

    EDIT: Starting Xcode 9 you're now to easily do it. Highlight the code that you want to rename, right click and choose refactor -> rename. Or you can also use the Editor menu on top of the Xcode and choose refactor -> rename.

    Solution Below XCode 9:

    1. click Find -> Find and Replace in Workspace

    1. Rename it, for example I am renaming ChatViewController with ChatVC then hit Enter. This usually rename the naming in storyboard too, So it's really convinient, but just double check if you want to really make sure it's changed.

    1. Lastly, Dont forget to rename the file itself in the project navigator

    0 讨论(0)
  • 2021-02-12 04:25

    After doing what @Caroline has pointed out, you should also change the name of your .swift file because it does not change using Find > Find and replace.... For doing that, you can click on the name of a .swift class and rename it as you want.

    I am using XCode 8 and it is the best option I could find to change the name of my class in the whole project.

    0 讨论(0)
  • 2021-02-12 04:28

    AppCode by Jetbrains can refactor/rename both Swift and Objective-C classes quite easily; It's not free but it does a great job.

    0 讨论(0)
  • 2021-02-12 04:35
    1. Find & Replace by cmd + shift + F
    2. Rename class name.
    3. If you use storyboard/Nib file, don't forget change the class you assigned in Show the identify inspector to new class name.

    TIP Set scope under the search field for Find & Replace can make this work clear.

    0 讨论(0)
提交回复
热议问题