Adding Core Data to existing iPhone project
All the CoreData header files are imported in App_Prefix.pch
, so the CoreData classes will be available throughout your Project, so you don't have to manually import the header in the files you need them.
So open up Xcode and look for some file like App_Prefix.pch
, by default it's in the Other Sources
group. After the UIKit
import statement, add the following line:
#import
And you should be ready to go.
Xcode 4
For projects created in Xcode 4, the prefix file can be found in the Supporting Files
group in the Project navigator. It's called 'projectname-Prefix.pch' by default.
Xcode 6+
Starting with Xcode 6, the precompiled header file is no longer included by default. This is because of the introduction of Modules, which take away the need to use precompiled headers. While it is still possible to manually add a PCH file to globally include the CoreData headers, consider specifying the CoreData dependency using @import CoreData;
* in every file that uses CoreData. This makes dependencies explicit and more importantly will avoid this question's problem in the future.
* Modules need to be enabled for this to work.
How to add Core Data to existing Xcode 9 Swift 4 iOS 11 project?
go to File > new file... select core Data under iOS and select Data Model
you'll still need some code which xcode auto generates whenever you select core data during project creation.
to get it, just create new project with core data option checked and copy all the code written under ** //Mark: - Core Data Stack** comment in AppDelegate.swift
and add
import CoreData
above
OPTIONAL
And don't forget to change the name of the app after copying the completion block for lazy var persistentContainer. Change the name of your app on this part *NSPersistentContainer(name: "SHOULD-BE-THE-NAME-OF-YOUR-APP") And managedObjectModel function of the code you just copied**
Implement CoreData into an existing project using Swift
You're getting an nil
value there because the file it's looking for doesn't exist in your main bundle.
You need to copy your data model file from the other project you created to your main project. The file would be called something like My_App.xcdatamodeld
and should be located in the same folder that your Xcode project file is in.
Note: The URLForResource
line is looking for My_App.momd
; that file is created by Xcode from My_App.xcdatamodeld
when it compiles your project.
Adding CoreData into an existing project using Objective-C
I am assuming you have selected core data option while creating your object. Your object context is null because it is store into AppDelegate. So you need to get context reference from appdelegate like below.
NSManagedObjectContext *context = ((AppDelegate*)[[UIApplication sharedApplication] delegate]).persistentContainer.viewContext;
NSArray *usernames = [context executeFetchRequest:fetchRequest error:&requestError];
Adding core data into existing iOS project
Easiest way to do this is to create a new project (Empty project) and make sure Core Data is added in. Then, just look over the differences, there should be four big ones:
- Add in all the stuff that is created in the AppDelegate
- Add in the Core Data framework
- Add your data object
- Modify the pch file
Adding core data to existing tabbed application (ios swift, Xcode6)
The easiest way to do so is just creating a new application with Core Data enabled, copy and paste the code from AppDelegate.swift and create a new Core Data Model (Cmd + N > iOS - Core Data > Data Model
). The only thing you have to do is replace the database name from your demo project with the name you chose for the Core Data Model you created. To do so, just search for your old project name in the code you pasted and replace it with the name of your new Core Data Model.
I also tested what you said and the missing Core Data checkbox for tabbed applications should be an Xcode 6 bug but it does not really matter which template you choose for the application. The Core Data code is the same across all the templates, the only difference between these options is the interface that is generated by Xcode.
Related Topics
What Is an "Unwrapped Value" in Swift
Ios 6: How to Restrict Some Views to Portrait and Allow Others to Rotate
How to Create Ns_Options-Style Bitmask Enumerations in Swift
Nsurlconnection and Basic Http Authentication in Ios
How to Pass Data Using Notificationcenter in Swift 3.0 and Nsnotificationcenter in Swift 2.0
Modal View Controllers - How to Display and Dismiss
Importing Project-Swift.H into a Objective-C Class...File Not Found
Array from Dictionary Keys in Swift
Resizing Uitableview to Fit Content
Warning :-Presenting View Controllers on Detached View Controllers Is Discouraged
Xcode 6 Beta 2 Issue Exporting .Ipa: "Your Account Already Has a Valid iOS Distribution Certificate"
How to Add Image and Text in Uitextview in Ios
Subclass Uiapplication With Swift
Transparent iOS Navigation Bar
Changing Root View Controller of a iOS Window
Picking Two Different Images in the Same View Controller Using Imagepickercontroller in Swift