0

I had problems with an app I'm building and got numerous errors with cocoapods and incompatible build settings . I decided to start again and build the app back up from scratch. I managed to copy almost all files from the old to the new app and got it building successfully. I read a few SO pages (ie copy coredata, another that imply that I can also easily copy over the coredata xcdatamodel file and seeing I don't have any significant amount of data in the model I decided to try it (its the data structure that I'm trying not to have to write out again rather than the data itself).

I dragged and dropped the old xcdatamodeld file from the old app to the rebuilt app but when I ran again it gives me an error on the line

 let modelURL = NSBundle.mainBundle().URLForResource("myAppName", withExtension: "momd")!

with error message "fatal error: unexpectedly found nil while unwrapping an Optional value"

I then noticed that when I copied over the myAppName.xcdatamodeld file from the old app to the new rebuild ... it copies it over as myAppName.xcdatamodel ie without the d at the end. Ive tried deleting and copying over again and it does the same each time. Any ideas on what the problem is and how I can copy the coredata structure over sucessfully?

Community
  • 1
  • 1
lozflan
  • 835
  • 10
  • 23
  • Its certainly possible to copy a Core Data model from one project to another, I have done this on many occasions. Just make sure you copy the bundle correctly. I usually copy the actual file (a bundle if you have versions) to the new project location and then add the existing model using XCode. I have'nt done it recently though and do recall some issues if the bundle isn't correctly added to the XCode project. – Duncan Groenewald Aug 07 '15 at 12:23
  • BTW if you have lost the 'd' then most likely you never copied the bundle across properly. If you right click on the file in finder you should get a 'Show Package Contents' menu, if you don't then try and figure out what you did wrong. – Duncan Groenewald Aug 07 '15 at 12:27
  • thx duncan. ive just tried it again and doing the same thing. ie i created a brand new project in xcode. named it abc. ticked use coredata. a new project was created with abc.xcdatamodeld file (viewed from project navigator). I then opened another xcode project, clicked on the xcdatamodeld file, dragged it across and dropped it into the new abc project. The result is that the "dragged across" xcdatamodel file drops the d at the end of the file name ... both in the project navigator and also in finder when i navigate to the abc folder. I'm not sure what I'm doing wrong here ....any suggestions – lozflan Aug 11 '15 at 05:50
  • When you say you dragged it across - I assume that's in XCode. Try copying the model bundle to the new project using Finder and then in XCode in the new project do the following: - File -> Add Files to "newProject" and select the model bundle you copies across to the new project folder. – Duncan Groenewald Aug 11 '15 at 23:11
  • It sounds like you're not using source control for your project. otherwise you'd have been able to revert your bad changes. I'd advise using some kind of DVCS, such as Git, before moving forward. – Nick Dec 15 '15 at 21:08
  • i was using source control but the problems were more cocoapods related when i updated or upgraded cocoapods. i tried reverting to earlier versions but the cocoapods errors didnt correct themselves – lozflan Jan 17 '16 at 22:49

2 Answers2

0

i ended up recreating the coredata store manually but if i need to do next time ill try copying it via finder rather than xcode as you suggested. thx for help and detailed instructions

lozflan
  • 835
  • 10
  • 23
0

I've just experienced the same issue. The problem here is that when you drag&drop model from Xcode project to Xcode project it will copy wrong extension like xcdatamodel instead of xcdatamodeld. In other words 'd' char is missing (that's because you copied package content not the package itself, see Show Package Content on that file in Finder).

When you copy it from Finder to desired project - everything is fine, that 'd' is in place and it will have a match when looking it up in a bundle with 'momd' extension.

Viktor Kucera
  • 6,177
  • 4
  • 32
  • 43