XCode Cant' Edit CoreData Model

前端 未结 8 1423
长发绾君心
长发绾君心 2020-12-18 23:44

After adding a CoreData Model to my existing project using

File > New > File... > Core Data > Data Model

I am unable to edit the m

8条回答
  •  一生所求
    2020-12-19 00:18

    Our real problem was that Xcode couldn't open/validate the data model when creating a new version. Meaning: having first one .xcdatamodel and then Editor > Add Model Version... > MyProject 2 (as .xcdatamodel). After what, the MyPoject.xcdatamodeld was created but couldn't be opened.

    After hours investigating we figured out that the .xcurrentversion file and the XCVersionGroup in the project file were missing or invalid. One of the reason was that the xcdatamodeld version should be in the root folder.

    Easy Fast Solution 1:

    Create a new Model Version and:

    1. Set the path to the root folder
    2. Specify the Group to be the subdirectory where the previous version is.

    The new .xcdatamodeld will now work on Xcode but can't be moved anywhere else than the root folder.

    Complicated Solution 2:

    To have a wroking .xcdatamodeld in a subfolder, hou have to modify manually the project file follwoing those steps:

    PS: This is the most complicated and hacky answer I evcer wrote. I'm sorry :(

    1. Create a new Model Version (even though it invalidates the data model) in the same directory of the previous version.
    2. Make sure the new .xcdatamodeld file is specified in your target(s).
    3. Open the Package Content in Finder and drag-and-drop all xcdatamodel versions to the project.
    4. Open the project.pbxproj file in a text editor.
    5. Search for the new files for all data models and copy their fileId.

    Example:

    75F3199D1B9D80D50030FF46 /* MyProject 2.xcdatamodel in Sources */ = {isa = PBXBuildFile; fileRef = 75F319981B9D80D50030FF46 /* MyProject 2.xcdatamodel */; };
    // fileId = 75F3199D1B9D80D50030FF46 /* The first id at the beginning of the line */
    
    1. Move those lines from the PBXBuildFile section to the beginning of the PBXFileReference one and update them like this (reuse the fileId).

    Example:

    75F3199D1B9D80D50030FF46 /* MyProject 2.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = "MyProject 2.xcdatamodel"; sourceTree = ""; };
    
    1. Now search the file MyPoject.xcdatamodeld in the PBXBuildFile section and copy its fileRef.

    Example:

    75F319961B9D7FA50030FF46 /* MyProject.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 75F319931B9D7FA50030FF46 /* MyProject.xcdatamodeld */; };
    // fileRef = 75F319931B9D7FA50030FF46
    
    1. Finally, at the very end of the project.pbxproj file create the XCVersionGroup section and reuse the fileRef from the xcdatamodeld and all fileId.

    Example (without the // comments):

    /* Begin XCVersionGroup section */
            75F319931B9D7FA50030FF46 /* MyProject.xcdatamodeld */ = { // fileRed
                isa = XCVersionGroup;
                children = (
                    75F3199D1B9D80D50030FF46 /* MyProject 2.xcdatamodel */, // fileId
                    75F319A11B9D80D50030FF46 /* MyProject.xcdatamodel */, // fileId
                );
                currentVersion = 75F3199D1B9D80D50030FF46 /* MyProject 2.xcdatamodel */; // fileId of the current version
                name = MyProject.xcdatamodeld;
                path = Path/To/MyProject.xcdatamodeld; // set the correct path
                sourceTree = "";
                versionGroupType = wrapper.xcdatamodel;
            };
    /* End XCVersionGroup section */
        };
        rootObject = 7564EB681B4AB1560065394B /* Project object */;
    } /* EOF */
    
    1. You should now be able to open the xcdatamodeld file in Xcode now from the desired subfolder.
    2. Now remove the extra references of the single xcdatamodel files from xcode (the ones created in the beginning).

    Sorry for the long answer... but problem solved :D

提交回复
热议问题