I am having an issue using a custom pure Swift framework in another project.
Some notes (as I've believe I've thorougly searched for all possible answers) :
I have my classes declared as public in the framework.
I have successfully built and ran the framework with a Swift target application but only from within the framework project.
I have included my framework in Linked Framework and Libraries and Embedded Binaries.
What i am trying to do is build a pure Swift single view application project by importing only the product framework. The error I am getting is "MyClass is unavailable: cannot find Swift declaration for this class" (which as mentioned above is public) . Also I have an public enumaration with a similar error: "Use of undeclared type 'MY_ENUM'"
Example code below:
import Foundation
import UIKit
import MyFramework /// don't know if this is needed.
public class ViewController: UIViewController{
var myclass:MyClass? ///here is the above error ,it's initialized in the viewDidLoad() function
var myEnum:MY_ENUM = MY_ENUM.MY_ENUM_VALUE /// 2nd enumeration error.
override public func viewDidLoad(){
super.viewDidLoad()
self.myClass = MyClass(arguments) ///same error as above
self.myClass?.myFunction /// ViewController does not have a member 'MyClass' error
/// more code here with errors regarding the class and enum.
}
I've also tried using an objective-C Bridging Header (though I believe this is wrong) and importing my framework header.
#import <MyFramework/MyFramework.h>
Is it possible for a solution to the above or is a restriction from Swift and I am trying something in vain?
One final note: I've included some other headers from another Objective-C framework in my framework because it was the only way to build it as a custom Swift framework. The classes there are visible to the Swift application.
P.S.If more code is needed I'll be happy to provide.
Solved the issue.
Had to put the .swift files together with the .h files as public headers in my custom framework (don't understand why though) and build again.
Maybe the path of your framework is wrong?
Related
I've got a framework built in both Objective C and Swift. I've upgraded it from Swift 3 to Swift 4.2, and now, after exporting it through Aggregate target and importing the generated framework inside my Objective C demo app, it doesn't show me any Swift class.
I've already checked if -swift.h is correctly builded, and I can see inside it all Swift classes. However, even if I import the umbrella header inside my demo app, I can't see them.
Thanks
By default Swift generates code that is only available to other Swift code, but if you need to interact with the Objective-C runtime – all of UIKit, for example – you need to tell Swift what to do.
That’s where the #objc attribute comes in: when you apply it to a class or method it instructs Swift to make those things available to Objective-C as well as Swift code. So, any time you want to call a method from a UIBarButtonItem or a Timer, you’ll need to mark that method using #objc so it’s exposed – both of those, and many others, are Objective-C code.
class MyController: UIViewController {
#objc func authenticateUser() {
}
To make this class accessible from Objective-C, we need to add the #objc keyword just before the class declaration. This tells the compiler to do some magic behind the scenes (namely, create an invisible header file)
#objc class SwiftViewController: UIViewController {
// ...
}
Reference - https://www.hackingwithswift.com/example-code/language/what-is-the-objc-attribute
There are lots of question about how to use swift code in objective c. I had swift based project. there is Objectice C file where i need to use swift code.
Xcode did not create ProjectName-Swift.h automatically. So i created manually and check the following things.
Generated interface header name : ProjectName-Swift.h
Product Module Name : myproject
Defines Module : YES
Embedded Content Contains Swift : YES
Install Objective-C Compatibility Header : YES
Add #objc in swift class
import UIKit
import SwiftyJSON
#objc class User: NSObject, NSCoding
{
}
Then Import ProjectName-Swift.h in objective c file. But gives error Unknown Type name User
I had tried with add #class User It gives error forward reference using #class
How can I fix this erros
Hopefully you have already solved the problem after reading the documentation referenced by #AnupamMishra, but just in case: try removing from the project the ProjectName-Swift.h file that you created manually. It hides the file of the same name auto-generated by Xcode and not listed in your project. The file is still there, somewhere deep in the DerivedData directory.
Another observation: you didn't have to set Objective-c Generated Interface Header Name in Build Settings. Xcode would generate one and name it myproject-Swift.h by default, since myproject is your Product Module Name.
I'm trying to use my new ViewController.swift file in my existing objective C project.
Below is swift file code
import UIKit
class TutorialViewController: UIViewController{
}
Below is Objective C code
#import "TutorialViewController-Swift.h" //"TutorialViewController-Swift.h" file not found
I'm unable to import swift code. I had followed all the steps in this
Please let me know, where am I making mistake. Is it only applicable for NSObject class.
Thanks in advance
You can't import a swift class directly to Objective C class like that way. By default Xcode generates a swift bridging header for this purpose. You need to import that header. Normally that header file uses the following naming convention:
<#your module name #>-Swift.h
Or you can get the value from your target's build settings:
Choose your target
Go to Build Settings tab
Go to Swift Compiler - Code Generation category
Check the value of Objective-C Generated Interface Header Name
Import that header in your objective-c class to use all your swift classes
As per document, When you import Swift code into Objective-C, you rely on an Xcode-generated header file to expose those files to Objective-C. This automatically generated file is an Objective-C header that declares the Swift interfaces in your target. It can be thought of as an umbrella header for your Swift code. The name of this header is your product module name followed by adding "-Swift.h".
By default, the generated header contains interfaces for Swift declarations marked with the public modifier. It also contains those marked with the internal modifier if your app target has an Objective-C bridging header. Declarations marked with the private modifier do not appear in the generated header. Private declarations are not exposed to Objective-C unless they are explicitly marked with #IBAction, #IBOutlet, or #objc as well. If your app target is compiled with testing enabled, a unit test target can access any declaration with the internal modifier as if they were declared with the public modifier by prepending #testable to the product module import statement.
You don’t need to do anything special to create the generated header file—just import it to use its contents in your Objective-C code. Note that the Swift interfaces in the generated header include references to all of the Objective-C types used in them. If you use your own Objective-C types in your Swift code, make sure to import the Objective-C headers for those types before importing the Swift generated header into the Objective-C .m file you want to access the Swift code from.
For more details follow this Importing Swift into Objective-C
I have a build error when trying to subclass a custom Objective-C class (a subclass of UIViewController) in Swift.
When I try to subclass in Swift, I get the build errors in the picture below. All of them relate to the use of the word class as an argument in the OCMapper library (where I've opened an issue as well).
Some more notes:
In the project, I both import and use Objective-C code in the Swift code and import and use Swift code in the Objective-C code.
I import the compiled Module-Swift.h only in .m and .mm files and forward declare classes that I need in .h files.
I've attempted to create a Module-Swift-Fixed.h class where I forward declare and/or import the custom Objective-C class headers (as recommended here), but that hasn't made a difference.
Has anyone seen anything like this before or have a solution?
I have as yet not been able to trace where in the language spec this is documented, but I suspect you have come across the same problem that I recently faced in objective-c since moving to Xcode 6.4.
I had a message (method) defined as follows
- (BOOL)canProcessClass:(Class) class {
return [class isSubclassOfClass:[NSSet class]];
}
with the same compile error as you mentioned Expected identifier. The fix was simple - just rename the the class argument to something like classToProcess. Which would give you the following
- (BOOL)canProcessClass:(Class) classToProcess {
return [classToProcess isSubclassOfClass:[NSSet classToProcess]];
}
Hence just rename the arguments in your Swift code to not use the (key)word class and you should be fine.
If anyone can point me to the language spec that documents this I would really appreciate it. As far as I'm aware you shouldn't use Class, but I haven't able to find anything about class except the obvious that it is a message (method) available on classes.
After moving some code into an external framework I've been trying to import and use the framework in my app. I've added the framework as a dependency in my app.
My framework is called DiceKit. In one of the classes, just to test things out, I've added import DiceKit to the top of my file. This is not throwing any errors.
When I try to access the classes that should be in the framework, I get a Use of Unresolved Identifier error.
import UIKit
import DiceKit
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
println (Die(12).roll()) // `Die` doesn't exist at compile time
}
}
What could be causing the classes in the framework to not be compiled? I have made sure that all the classes and methods are marked with public and I haven't changed any build settings from the default in my framework.
I'm using XCode 6.3 Beta
Thanks for your help!
In your DiceKit custom framework
You should declare your Die class as public.
public public public all the things! Or at least, the things that others need to use from the framework.