Abhinav Abhinav - 1 year ago 129
iOS Question

.xcdatamodeld file not showing data model structure in Xcode

I have .xcdatamodeld file in my project but it not opening up into the data model.

When I click on the file in the left sidebar in Xcode, it continues to display the contents of the previously selected file instead of displaying the data model. Likewise, when I right click on the file and choose "Open As >", I cannot select to display it as a Data Model—nothing comes up. I checked out the code from the SVN repo and even the filer owner is not able to see it in his workspace.

Answer Source

This is a symptom that I have seen from time to time. In at least one case, this was due Xcode having the wrong path stored in the path component of the XCVersionGroup section of the project.pbxproj file, and having an apparently unnecessary additional name component in that section. It would probably be possible to manually edit this file, but I'm always reluctant to monkey around with the project file directly, and fortunately there's an easier way.

  1. Navigate to your .xcdatamodeld file in the Finder. The easiest way to do this is Right click/Ctrl-click on the .xcdatamodeld file and select "Show in Finder".
  2. Drag the file from the Finder and drop it immediately above or below the current reference to the .xcdatamodeld in the Project Navigator in your Xcode project.
  3. Click OK to accept the import, ensuring that you are adding it to the correct targets. (Which targets are correct will depend on your project.)
  4. You will now have two references to the same file in your Project Navigator. Clicking on either of them will now bring up the desired data model structure.
  5. Remove the duplicate reference to the model file. Click on one of the file references (doesn't appear to matter which, though it may be better to remove the old one) and press Delete.
  6. In the resulting confirmation dialog, be sure to select "Remove Reference" only, and do NOT move the file to the trash. (The two references point to only one actual file in the file system. Moving to trash would thus trash the only copy.)

You're now left with just one reference to the data model, and it works.

One thing that appears to trigger this bug is moving the .xcdatamodeld file in the file structure on disk. Even after correctly re-associating the file with the new location (i.e., it no longer appears in red in the sidebar), the one section of the Project file mentioned above is not correctly updated, and thus the file is not treated fully correctly as an Xcode Data Model file.