I want to insert a CoreML model into my Swift project. But whenever I insert a model and build my project I run into "Swift Compiler Errors" with the auto generated model class files for the mlmodel. There are four errors regardless of any model I add to the project
In the auto-generated file there is a Line:
try! self.init(contentsOf: type(of:self).urlOfModelInThisBundle)
It throws the errors -
1. Incorrect argument label in the call (have 'of:', expected 'rawValue:')
2. Value of type 'type?' has no member 'urlOfModelInThisBundle'
Solved it. Got around the issue by disabling auto generation of the class file. I created a manual Swift file and pasted the same code from the generated file. Just changed up the syntax of that error line into:
try! self.init(contentsOf: model_className.urlOfModelInThisBundle)
This worked perfectly and now I am able to use the model in my project.
But this still doesn't explain why the error popped up in the first place.
Related
This is an ASP.NET (Framework v. 4.5.2) website currently running reliably in production. A new requirement called for adding a new column to an existing SQL Server table (apProduct) which has been done. I then made the necessary changes to the .edmx file, controllers, views, etc., and everything is compiling fine. However I'm now getting the runtime error below:
When I debug and set a breakpoint where apDepartment is first getting created, I expand the created objects and see this:
My attention is drawn to: "The metadata for 'EntityFrameworkDynamicProxies-AnkoMVC' is invalid."
Here is where apProduct is defined, clearly within the AnkoMVC.Models namespace:
Lastly, here is the DbContext class:
Can anyone offer suggestions on how to overcome this?
I found the problem in the #model reference in many of the views. Here is the faulty code:
This is the corrected #model reference:
Thanks!
Thanks for having a look :)
I am using xcode 8.2.1 and the language is obj-c.
I simply created two entities(Photo and Photographer) and after I did editor - create NSManagedObject Subclass there went error in the files generated.
One is in Photo+CoreDataClass.m saying "Photographer.h file not found".
Another one is in Photographer+CoreDataClass.m at the line saying "Photo.h file not found".
All the files that were generated were:
Photographer+CoreDataClass.h/m,
Photo+CoreDataClass.h/m
Photographer+CoreDataProperties.h/m
Photo+CoreDataProperties.h/m
The tools version minimum is Xcode 7.3 and codegen is None/Manual
Could anyone tell me how to fix this?
Ps, the entity's relationship is that one Photographer to-many Photo, and Photo to-one Photographer
Just rename your files
Photographer+CoreDataClass.h/m,
to
Photographer.h/m
and
Photo+CoreDataClass.h/m
to
Photo.h/m
then copy the properties from the other two classes into the renamed ones then remove them.
I got rid of this error by setting Module to empty for the Class section after you select .xcmodeld file
I created a database with Core Data on iOS. I initially set up a unique constraint in my Conversation entity. However, after removing it, I was getting an error: "In the uniqueness constraints attribute for entity Conversation, comma is not a valid property".
I looked at every field in my DataModel.xcdatamodeld but could not find any solution.
If you have set any constrains (using Attribute name) previously for your Entity and later if you would have deleted/renamed that particular attribute, then it will throw such error. You need to rename or delete that constraint property by double clicking it with respect to your data model business flow.
Refer screenshot for reference.
I had the same issue with Xcode 8.1. I did not want to delete all the many attributes painfully set up on all 6 of my entities, and found another solution. Let's assume your model file is called "foobar.xcdatamodeld".
Edit the XML directly in the model. For this, you need to get to the file 'contents' inside the model file. The path to it is: foobar.xcdatamodeld/foobar.xcdatamodel/contents. To get to it in Finder, control-click on the "xcdatamodeld" package to "Show Package Contents", then again on the "xcdatamodel" package. You can then drag the file 'Contents' into your text editor of choice. You will see the constraints appear as follows in the XML:
<uniquenessConstraints>
<uniquenessConstraint>
<constraint value="property_name"/>
</uniquenessConstraint>
</uniquenessConstraints>
You can edit "property_name" to match the new value, or remove the constraints altogether.
Once edited, try to clean and recompile. In my case, the error was still there, so I had to do a little "renaming dance" with Xcode. I deleted the model from the Xcode project, then renamed the model file to a different name (including the "xcdatamodel" file inside the "xcdatamodeld" file), and added it back to Xcode. I repeated that again with the old name again so I was back to the initial name.
Its looks like a bug in Xcode 8. I have deleted all the attributes from the entity, cleaned the project and then added the attributes again. After that, the error was gone. Maybe this will help someone.
I inspected the XML file and noted that the "comma,separated,properties" was in the XML. I used Product->Clean (command+shift+K) to clean the project. Cleaning in of itself didn't help. I then tried File->Save (command+S).
File->save updated the XML and cleared up the situation for me.
I was trying to create NSManagedObject subclasses (2 related entities) automatically in Xcode. They are generated like this:
However, before I do anything further, when I tried to build and run it, a link error occur, as shown:
duplicate symbol _OBJC_CLASS_$_Photo in:
/Users/Kefeng/Library/Developer/Xcode/DerivedData/Photomania-aellrakjngugnzcgrleiytvrfvyt/Build/Intermediates/Photomania.build/Debug-iphonesimulator/Photomania.build/Objects-normal/x86_64/Photo+CoreDataClass.o
duplicate symbol _OBJC_METACLASS_$_Photo in:
/Users/Kefeng/Library/Developer/Xcode/DerivedData/Photomania-aellrakjngugnzcgrleiytvrfvyt/Build/Intermediates/Photomania.build/Debug-iphonesimulator/Photomania.build/Objects-normal/x86_64/Photo+CoreDataClass.o
duplicate symbol _OBJC_CLASS_$_Photography in:
/Users/Kefeng/Library/Developer/Xcode/DerivedData/Photomania-aellrakjngugnzcgrleiytvrfvyt/Build/Intermediates/Photomania.build/Debug-iphonesimulator/Photomania.build/Objects-normal/x86_64/Photography+CoreDataClass.o
duplicate symbol _OBJC_METACLASS_$_Photography in:
/Users/Kefeng/Library/Developer/Xcode/DerivedData/Photomania-aellrakjngugnzcgrleiytvrfvyt/Build/Intermediates/Photomania.build/Debug-iphonesimulator/Photomania.build/Objects-normal/x86_64/Photography+CoreDataClass.o
ld: 4 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I tried several times by creating new projects and do the same thing. My original intention is to add some custom methods into those to subclasses. But when I add anything into e.g. Photo+CoreData.h/m, the same error as above showed up.
I found some answers about the "double include" or "save files to the wrong directory", but I didn't do that. Anybody have any idea about this?
If you do not generate managed object subclass automatically, then don't forget to check "Codegen" settings for an Entity in Data Model Inspector:
Edit:
Thanks to some help from #iPeter, found the following:
After doing Editor > Generate NSManagedObject files, if you trash the files BEFORE building, your project should build no problems.
Then #import "myManagedObjectName+CoreDataClass.h" (where the MO name is the one in the entity inspector in core data) into any classes where you require those Managed Objects.
In other words, you don't require any of the actual ManagedObject files in your folder. Xcode keeps the generated ones in your Derived Data folder.
If for some reason you need those files to remain in your file directory, the following workaround will work.
Go to your Target and delete the CoreDataClass sources in your Compile Sources.
Leaving you with this:
Most of the new attributes / relationships I added after the initial generation of ManagedObject subclasses were available as properties after a build. In one case where I renamed an existing relationship, I had to do Editor > Generate NSManagedObject Subclasses again, then I trashed the new files in my folder, built, and everything worked OK.
Just wrote a blog post that includes this info for anybody interested.
You should delete all these entities, change "Codegen" settings to "Manual/None" for them in Data Model Inspector, and generate entities again. It works good! You don't need to remove +CoreDataClass.h files from Compile Source. You don't need to mark entities as abstract. You don't need to generate classes by yourself. You should change only "Codegen" settings and regenerate entities automatically.
late post ... but for me was simply a copy paste of an entity, Xcode does not seem to change the original class name associated with the entity (observed on Xcode 9.0.1)
You need to remove the CoreData related sub class generated by yourself, and you can keep the #import line with those classes, now all those files will be generated automatically when build the project.
I recently tried the above answer and it failed.
Somehow, Xcode was still generating the files.
I solved it using the following method:
Mark all entities as Abstract
Build
Remove Abstract flag
Build
Success !
This sounds like a bug in Xcode...
I'm trying to keep my code as readable as it possible by keeping methods and files as short as I can and using nested classes for namespacing. It works fine except some really strange moment.
I have some class used for namespacing.
class Space { }
All classes used within that one are implemented in their own files as extensions.
extension Space {
class SomeClass {
// implementation
}
}
One of those SomeClasses have a number of quite sophisticated initialisers, so I have split them up to their own files as well and implemented it as follows:
extension Space.SomeClass {
convenience init(fromSomeSource source: SourceClass) {
self.init()
// other implementation
}
}
The problem is that some of those files works just fine, but some of them throwing 'SomeClass' is not a member type of 'Space' and I don't know why.
All of them are pretty similar. The only difference is implementation of an initialiser itself. All files are held in the same place and I have no idea why some of them works fine and some not.
I tried to move code from not working files into files that works fine and that works – Xcode agrees to see the code and said nothing against it. But when the very same code lies in its own file – Xcode or compiler doesn't want to understand that SomeClass is really a member of Space.
I tried to clean the build, including manual dumping of ~/Library/Developer/Xcode/DerivedData folder. Nothing helps.
Surely I can put it all in a single file and it will work fine, but what the reason why it so picky in my case?
I've tried to create a new file and move there all contents from one of the bad ones. It works, but only with certain file names. Some names gives the same error again, but it seems that if name is totally new and not similar to any of the existing ones - it works. Magic?
I've encountered similar issue, it seems like the complier is trying to process the file where you extend the nested class before the one where it's defined. Therefore you have this error saying that that Space has no member SomeClass.
The solution I've found is to go to your target settings, open Build Phases.
There, in Compile Sources section you should put the file where you define the nested class above files where you extend it.
This solution seems to even play well with your observation that when you recreate the file it sometimes compiles, because when you recreate the file its position in Compile Sources changes.