Rename xcdatamodel file

2019-01-13 16:09发布

What do in need to do to rename the .xcdatamodel file. Renaming .xcdatamodel filename, along with changing ...

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"newfilename" withExtension:@"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];    
return __managedObjectModel;

and

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"newfilename.sqlite"];

doesn't work! I get following error:

2011-04-11 17:19:54.164 ProjectName[5826:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot create an NSPersistentStoreCoordinator with a nil model'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00fa15a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x010f5313 objc_exception_throw + 44
    2   CoreData                            0x00013853 -[NSPersistentStoreCoordinator initWithManagedObjectModel:] + 419
    3   ProjectName                         0x00003471 -[ProjectNameAppDelegate persistentStoreCoordinator] + 257
    4   ProjectName                         0x000031a4 -[ProjectNameAppDelegate managedObjectContext] + 100
    5   ProjectName                         0x000028ca -[ProjectNameAppDelegate makeSplitViewController] + 762
    6   ProjectName                         0x00002d44 -[ProjectNameAppDelegate application:didFinishLaunchingWithOptions:] + 68
    7   UIKit                               0x001f3c89 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163
    8   UIKit                               0x001f5d88 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 439
    9   UIKit                               0x00200617 -[UIApplication handleEvent:withNewEvent:] + 1533
    10  UIKit                               0x001f8abf -[UIApplication sendEvent:] + 71
    11  UIKit                               0x001fdf2e _UIApplicationHandleEvent + 7576
    12  GraphicsServices                    0x018f9992 PurpleEventCallback + 1550
    13  CoreFoundation                      0x00f82944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    14  CoreFoundation                      0x00ee2cf7 __CFRunLoopDoSource1 + 215
    15  CoreFoundation                      0x00edff83 __CFRunLoopRun + 979
    16  CoreFoundation                      0x00edf840 CFRunLoopRunSpecific + 208
    17  CoreFoundation                      0x00edf761 CFRunLoopRunInMode + 97
    18  UIKit                               0x001f57d2 -[UIApplication _run] + 623
    19  UIKit                               0x00201c93 UIApplicationMain + 1160
    20  ProjectName                         0x00002599 main + 121
    21  ProjectName                         0x00002515 start + 53
)
terminate called after throwing an instance of 'NSException'

Note: I'm using Xcode4.

9条回答
三岁会撩人
2楼-- · 2019-01-13 16:50

You can't change the name of any file in the application bundle after the app is built. Once the app bundle is fixed, it can't be changed at all.

So, if you want to change the name of the model file on device, especially on user's devices, well, you can't.

However, if you are still in development just change the name of the file in Xcode by clicking on the file's name and changing it.

查看更多
劫难
3楼-- · 2019-01-13 16:55

In Xcode 4.2, I renamed my model (*.xcdatamodeld) file in project navigator (it automatically renamed *.xcdatamodel file inside a package, and both files in my filesystem).

In initWithContentsOfURL: of the NSManagedObjectModel, I used a new name.

This resulted in what @Mustafa is describing in the question (nil model).

So I tried adding a new version of my model and it solved the problem (even though I had no changes in my model).

查看更多
贪生不怕死
4楼-- · 2019-01-13 16:59

In XCode 4.3.2, it successfully renames the model file but I had to remove it from the project and add it again to make it work. Otherwise it was throwing

查看更多
混吃等死
5楼-- · 2019-01-13 17:01

Thanks to all of you for helping me finally nail this one down. Here's how I fixed this after renaming my project in Xcode 4.2:

1) Delete the reference to the *.xcdatamodeld file in your project. If it's like mine, an editor won't load when you select it. This tells me a link was broken in the rename.

2) Right-click on the .xcdoeproj file for your project and select 'Show Package Contents'.

3) Drag the *.xcdatamodeld file you find there back into your project. Done.

My app buil

查看更多
成全新的幸福
6楼-- · 2019-01-13 17:07

There is a hidden file called .xccurrentversion which should be in the same location as your whatever.xcdatamodeld file. Inside there is a reference to your model which needs to be updated with the new name.

In some cases, you'll have to remove the .xcdatamodeld file from the project and add it again to make it work.

查看更多
▲ chillily
7楼-- · 2019-01-13 17:09

In my case I had a file called Model.xcdatamodeld in my project, I decided renaming it to MyModel.xcdatamodeld through XCode. It did correctly, however the inner file was still called Model.xcdatamodel. I could not rename it through XCode, so I did through my friend Finder. The problem from now on was that Xcode wasn't opening this file anymore. Then what I did was just searching for it on the project.pbxproj and renamed the file name. Now Xcode could open it perfectly.

1D707FAE1FA9EBAB00A6D123 /* Model.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = MyModel.xcdatamodel; sourceTree = "<group>"; };

Of course that I also had to rename it on the AppDelegate.

查看更多
登录 后发表回答