Error when creating NSManaged Subclass in Xcode 8.2 beta
The issue here is that as of Xcode 8, new projects using Core Data and new Core Data data models are configured to use automatic code generation by default. That means you don’t have to explicitly generate code for your Core Data entities any more, Xcode will take care of that for you. If you leave automatic code generation on and also manaully generate Swift classes for your entities, you’ll see an error like this.
You can disable automatic code generation for an entity in the entity inspector, or you can remove the manually-generated code from your project. Either of the two should fix this.
Cannot create NSManagedObject subclass
You are manually generating a NSManagedObject
subclass, which has already been generated by Xcode.
Find detailed solutions here.
How to make Xcode core data code generated files to be public accessed
You should be able to make the automatically generated header files be publicly accessible by copying them from the derived sources folder to the public headers folder of your framework.
You can do this by adding an extra Run Script step to the target of your framework. Copy the following script into the script editor and replace Your_Framework with the name of your frameworks executable.
#set -o xtrace
find "${DERIVED_SOURCES_DIR}/CoreDataGenerated/Your_Framework" -type f -name "*.h" -exec cp {} "${BUILT_PRODUCTS_DIR}/${PUBLIC_HEADERS_FOLDER_PATH}" \;
All this script does is list every header file in the derived sources directory (find $SOURCEPATH -type f -name "*.h"
) and then calls copy on each one in turn -exec cp {} $DESTPATH \;
If you have any problems uncommenting the first line (i.e. removing the #) and then looking at the build log should make it easier to diagnose.
Duplicate Symbol Error in NSManagedObject Subclass
You are generating files which have already been generated for you by Xcode and thus get duplicate declarations. Details about this feature (new in Xcode 8) can be found in this WWDC video.
Two possible fixes:
1) Use the Xcode generated ManagedObject
subclasses (the recommended, modern approach)
- Delete all generated NSManagedObject subclasses from your project, if exists.
- Set
Codegen
toClass Definition
in your.xcdatamodel
for all entities - Make sure
Module
is empty ("Global Namespace" in light gray) (workaround an Apple bug, see this answer)
- Clean project
- Clean DerivedData folder (Optional. To be on the save side)
- build
Note:
Never add the automatically generated files to your project. Even you do not see the generated files in your project, Xcode has a reference to it, so you are able to write extensions and such. For instance:
extension MyEntity {
func doSomething() {
//
}
}
Also, you can command+click to the generated file within Xcode.
2) Trigger subclass generation manually (a rather paranoid but bullet-prove approach, ignoring the new Xcode features)
- Delete all generated NSManagedObject subclasses from your project, if exists.
- Set
Codegen
toManual/None
in your.xcdatamodel
for all entities - Clean project
- Clean DerivedData folder
- Restart Xcode
- Manually generate
NSManagedObject
subclasses (in "Editor" menu) - Make sure those files are added to your project
- build
NSManagedObject subclasses duplicate declaration
You are generating files which have already been generated for you by Xcode and thus get duplicate declarations.
Find detailes in this answer.
Core Data Giving Error
The problem here is that your accessor and your ivar have the same name. That's where the underbar ivar convention comes from. Here, you're not using the accessor to access your property, you're using the backing variable directly, so it never gets initialize. Instead, make sure you always go through your accessor methods and you won't have a problem. So, rewrite the offending method (and any others that use the managedContextObject
property with something like the following:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated]; // it's good practice to call the super methods, even if you're fairly certain they do nothing
// Get a reference to the managed object context *through* the accessor
NSManagedObjectContext* context = [self managedObjectContext];
// From now on, we only use this reference in this method
NSFetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription* entity = [NSEntityDescription entityForName:@"Remind" inManagedObjectContext:context]; // <- use the local reference we got through the accessor
[request setEntity:entity];
NSError* error = nil;
NSArray* array = [context executeFetchRequest:request error:&error];
if( !array ) {
// Do something with the error
NSLog(@"Error Fetching: %@", error);
}
[self setDesitnationsArray:[array mutableCopy]];
[destinationsTableView reloadData];
}
You might want to change your ivars to something you won't be tempted to use or that will be immediately apparent that you haven't gone through the accessors, like _managedObjectContext
or even _privateContext
or whatever will stick out to you until you get used to accessing properties through the accessors. If you don't like the Objective-C syntax for accessing properties, you could use the dot syntax, but you must always remember to go through self
, for example, self.managedObjectContext
. I don't like this method as people forget that it's not a direct property access and it is using the accessors, so they think it's okay to interchange the dot syntax for a direct access, when it's not (like in your case).
Related Topics
Present Modal View Controller in Half Size Parent Controller
iOS 7 Sprite Kit Freeing Up Memory
Iphone: Hide Uitableview Search Bar by Default
Check If a Uiscrollview Reached the Top or Bottom
Class Amsupporturlconnectiondelegate Is Implemented in Both
Restrict to Certain iOS Target Devices for App Store Submission
How to Resize Uiimageview Based on Uiimage's Size/Ratio in Swift 3
Changing the Development Language in Xcode
How to Run/Test My Flutter App on a Real Device
Swift Compiler Shows Expected Declaration Error
Detect First Launch of iOS App
How to Use Iboutletcollection to Connect Multiple Uiimageviews to the Same Outlet
Uitextview That Expands to Text Using Auto Layout
Tabbaritems and Setting Their Image Sizes
Disable Uipageviewcontroller Bounce
How to Find File Uti for File, Withouth Pathextension, in a Path in Swift
iOS Perform Action After Period of Inactivity (No User Interaction)