Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.Start your free trial
Renaming Swift classes in Xcode?
Short version: fixing typo in name of class causes strange errors that are hard to track down. Long version: read ahead.
When I first started this course, we were asked to create a new class: WeeklyTableViewController. I made a typo at the time, and accidentally created WeeklyTableTableViewController instead. Since then, I went around to every place I could find and fixed it, deleting the second "Table" and figuring it would be fine as long as they all matched.
Then, I tried to put outlets from UI elements into the WeeklyTableViewController, and I got this error message: “Could not insert new outlet connection: Could not find any information for the class named WeeklyTableViewController”
Okay, I must have missed one and my references are off, right? One of them still says TableTable and it's using the wrong path. So, I search more carefully for references to the type, and the only one I find that still says TableTable is under the Symbol Navigator (left tab) where it still lists "WeeklyTableTableViewController". I click on that, and there are options for refactor -> rename. Great, exactly what I want. When I click it, though, I get this error:
"Rename requires a symbol such as a class, a function, or a local variable, but the selection does not represent such a symbol. Select an appropriate symbol and try again."
So, I thought maybe it wasn't recognizing it because the whole TableTable thing didn't match all the other ones that I had already fixed. So, I go back into the class description and I change the declaration back to TableTable so that this spot can find it, and I even go in the finder and change the name back to TableTable, and then I try the same thing at the same place in the symbol navigator. Now I get a different error:
“Can’t refactor Swift code. Xcode can only refactor C and Objective-C code."
Since this wasn't working anyway, I figured I might as well fix again the spots I had changed from Table back to TableTable: renamed the class declaration, and fixed the title in Finder. I look back soon after and notice that the title is fixed in Symbol Navigator. Okay, I don't know what I did, but it looks better, so let's try it out. Maybe it's fixed.
I go back to the storyboard to drag my outlet again and see what happens. First, I notice that the assistant editor can't figure out where to go -- it offers me the option of UIViewController or WeeklyTableTableViewController from automatic, even though the object type is clearly labeled WeeklyTableViewController. Of course it fails if you click on the TableTable one because there is no such object right now. I can navigate manually to decide where the assistant editor should go, and I navigate to WeeklyTableViewController (the correctly named path) and it pops up no problem. Then I try to ctrl-click and drag to create the new outlet and I get a slightly different error:
“Could not insert new outlet connection: The file “WeeklyTableTableViewController.swift” couldn’t be opened because there is no such file."
Yes, I know there's no such file, because I renamed it, but it shouldn't be looking for that one. It should be looking for the renamed version, with one Table instead of two, and I can't figure out how to change where it's looking for the file. I don't know where else Swift and/or Xcode have put that file name that I either don't have access to or haven't found yet.
At this point, it's a persistent problem. I can see two ways around it: 1) Delete the whole class file and start it over. It hasn't been that long since I created it, and I guess it wouldn't take too long to modify it again from the UITableViewController parent class. Or 2) Just put everything back to WeeklyTableTableViewController and ignore the fact that the name doesn't make sense. The computer doesn't care, and it will still run this way.
Both of these would work, but they seem like silly workarounds for something as simple as a name change. Surely there's a better way! How am I supposed to change a class name once it's part of an Xcode project?
Edit: I've also tried the find all command, and did not find any remaining references with TableTable still, but errors persist.
I searched around Google, and it took longer than it should have, but I eventually solved the issue on my own. There were a lot of places frustratingly listing steps I had already done, which did not solve my problem, but here's a link to the site that did solve it: http://www.informit.com/articles/article.aspx?p=2234244
"Pinning Down File Paths" was the specific part that mattered for me.
My specific steps were to open the WeeklyTableViewController and look in the right sidebar, under "identity and type'. I had to change the 'location' dropdown from 'Relative to project' to 'Absolute path'. Then, I could drag in my outlets and it all now works as it should. Interestingly, I can now change it back to 'Relative to project' and it still knows where the file is. This is also strange because it must have been finding the file accurately in some places, because I was still able to edit it, but it was looking in the wrong location in other places. One way or another, though, it all works now.