I am not able to import a swift NSManaged subclass to any other swift source code file. I created one Entity and then i created NSManaged subclass for that entity.
But i am not able to import that file to any other source code file. I might be missing some settings in the Xcode.
Please check this image.
You shouldn't write imports for source files, only for libraries and frameworks. Your class USer should be available. Btw, check it's spelling
Related
Cannot get Xcode to generate the bridging file so it's currently empty and cannot find any guide/docs on what needs to be in the file. The empty bridging file is being imported in the .m where the class is needed. Swift file using #objcmembers on class definition.
How and what should go into the bridging file. Have looked for two days trying to make this work with no success.
Is there a way to turn back on the automatic generation of the bridging file in Xcode?
Any docs on the contents of the bridging file?
Don't confuse the two kinds of header. The "bridging header" is for Swift to see Objective-C code. It isn't relevant here.
The way Objective-C sees Swift code is through the generated interface header. You can see its name in your app target build settings:
So what that tells you is that if you #import "MomApp2-Swift.h" in your .m file, your Objective-C code will see your code that is exposed from Swift.
If you want to see what is in that generated interface header, then after you've done that import, Command-click on the file name.
To use a Swift class on objective-c you need to be sure that your class inherits from NSObject, has an #objc annotation on class definition and other elements that you want to use need to be marked with #objc. #objcmembers marks all elements visible from an Objc class located on file that imports the "project-Swift.h"
You may not need all class members visible from Objective-c
This question probably has an answer elsewhere, but I've only found answers for outdated versions of Xcode or projects that use Swift.
I am using Xcode 10 and reading iOS Programming The Big Nerd Ranch Guide, 4th Edition. I am using such an old version because this is the resource that my work provided to me.
Currently I am trying to add a method to a CoreData generated class for Objective-C.
With CodeGen set to Category/Extension with my entity selected in CoreData the generated class files are:
BNRItem+CoreDataProperties.h/m
BNRItem+CoreDataClass.h/m
BNRItem+CoreDataClass.h includes BNRItem+CoreDataProperties.h, and BNRItem+CoreDataProperties.h includes BNRItem.h (which does not exist). I assume that I need to create BNRItem.h but I don't know what file to import into BNRItem.h since either BNRItem+CoreDataProperties.h or BNRItem+CoreDataClass.h would seem to create a circular include cycle.
In which file do I add an instance method to a CoreData generated class for Objective-C in Xcode 10?
The convention is to use #class in the .h file and #import in the .m file, except when #import is necessary in the .h file. The latter occurs when referring to a superclass or protocol.
The idea is to speed up recompiles. If ClassA.h imports ClassB.m, and you change only ClassB.m during development, then because ClassA.m imports ClassA.h which imports ClassB.m, ClassA.m will need to be recompiled during the next build, even though it is unchanged.
Of course, all of this goes away in Swift :)
This is likely not the actual answer, but I fixed this by changing the CodeGen to Manual/None and editing the files that it generated.
What about leaving the code generation option to Class Definition and create an extension (a category in Objective-C) of your entity?
The #import statement was introduced to resolve the cyclic dependencies created by #include.
So it doesn’t matter much if you are using #import, but if you’re still concerned you can also use #class forward declaration
This has always been my struggle, but just now I ran into this Apple article.
The second way, is the way you are trying to use. The unexpected step is to move the properties file to the trash. And actually it seems the <class>+CoreDataClass files have to be renamed to just <class>.h and .m. After that you can use those files to extend your class and the property files keep being generated.
So the steps to create your managed object class are:
Choose Category/Extension in the data model inspector for your entity.
Generate your class with the Editor > Create NSManagedObject Subclass... menu.
Move the created <class>+CoreDataProperties.h and .m files.
Additional step: rename the data class files as just described.
I have swift 4 project, recently pull from another branch. But whenever i want to import any class which is already exist in project show me the error "No such module...". Please suggest me what is wrong.
You don't need to import files in Swift as they are globally available. In Swift you can only import module, e.g. any framework.
In Swift, if a class already exist in project then you don't need to import it; you can directly use its properties by creating its instance. but if you have problem with it then you need to check target membership for the class. it should be checked for the project like in my example in pics
Can anyone suggest me a solution to import AppDelegate or any other custom classes in Cocoapod classes
I had to import a UIColor category in my cocoa pod library, so what I did, I added it as an existing file in the library unchecking Copy items if needed. This will not create another copy of the file.
It is working well. May be you have to do the same.
I have a framework with Swift and Objective-C in it. I have the statement at the top of my Objective-C class #import "MyFrameworkHeader.h" which I thought would expose my swift code to my Objective-C class however the compiler still says the symbols don't exist, how can I expose my Swift classes to my Objective-C classes within the same Framework?
Ugh, after smacking my head for a few hours then finally posting this question, within a few minutes I found the answer:
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_82
Under the header: "Importing Swift into Objective-C"
To get Objective-C to register your swift code you should go into your build settings and then see/set your project name, also see if the compatibility header option is checked to yes.
after that is set just go into your file and type #import "-Swift.h". I don't think you can import individual files with swift and Objective-C, you import all of your swift files at once. It's important that you make sure all of your swift files have no errors or you will get an error about this header not being defined. You might have to build your project with no errors for the file to get generated at least once.
I think what you're looking for is a Bridging Header.
This is a file that you import your Objective-C headers into that get exposed to Swift.
There are 2 ways to make one (as far as I know):
The easiest way, in my opinion, is to add an existing Obj-C file to your swift project, or vice-versa. Xcode should ask you if you want to automatically configure a bridging header. Choose yes, and Xcode should make a file called something like 'project name'-bridging-header.h. In this file, import your files, so #import "MyFrameworkHeader.h" should do it.
Make your own empty file with File > New File (Cmd+N) > Source > Header File. Call it whatever, and import your files like in the previous one. Before it will work, you have to enter the name of your bridging header file into a field in your Project Settings (In the first option, Xcode will do it for you).