Im trying to write a Swift framework with a protocol used for a delegate method. I've made the protocol public, but when I add my framework to my project, the project spits out the following error
ViewController.swift:15:83: No type named 'MyControllerDelegate' in module 'MyModule'
import MyModule
class ViewController: UIViewController, MyModule.MyControllerDelegate {
Any help in these regards would be appreciated.
Update: not sure if this makes any difference but some of the object types in the protocol are defined by another framework...
Related
I read this documentation for importing swift code into objective c.
https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_swift_into_objective-c
I have a few questions.
Can I put #objc annotation for a Struct?
Do I need to inherit all the class that I want to export to obj to be child of NSobject ? I am getting error 'error: only classes that inherit from NSObject can be declared #objc'
When I export a swift class with #objc, I need to add #objc to all its parent classes, protocol and interface and also class and structure in all its methods, is that correct?
No. Objective-C cannot see a native Swift struct.
Yes. Objective-C classes must basically be derived from NSObject. Objective-C can be made aware of the existence of other classes, but it cannot do anything useful with them.
You can mark the class with #objcMembers, in which case you will give everything within it full visibility to Objective-C.
I have used InteractiveSideMenu which is swift library. Now I want to adopt swift protocol into my objective-c class for that I have to make my objective class's base class to swift class which is somehow disallow.I don't know how to achieve this. can somebody help me out on this?
#interface my_objectiveC_Class : UIViewController
{
}
Now, instead of UIViewController class I have to make it some swift class like MenuViewController but its not allowed to make swift class as base class of objective-c class.
Basically I need to adopt swift protocol inside my objective-c class.
To use any Swift code into Objective-C or Objective-C code into Swift you need a Bridging-Header to let your compiler translate between two languages.
See this AppleDoc
This question isn't about bridging headers and all that. I've got all that working. My question is specifically about what I need to do to my Swift classes to get them to show up in Obj-C code.
Take, for example, this simple class:
class MyClass {
var value: String = ""
}
If I have this class in my Project, it doesn't get included in the MyProject-Swift.h file that gets auto-generated. My understanding is that in order to use a Swift class in Objective-C, my class needs to derive from a class that Objective-C knows about. This is where I start to doubt the actual requirements.
If my class were derived from a UIViewController, then no problem. But if this is just a model object, then it's not deriving from anything. While it is entirely possible to easily make my class derive from NSObject, and thus, it gets properly imported into the Obj-C code, deriving from NSObject can cause other issues down the road.
So if I don't want to make my class derive from NSObject, what can I do to make it visible to my Obj-C files? Is there a doc I just couldn't find that explains how to do this?
As far as I am aware currently, Only Swift classes that inherit from NSObject can be declared #objc and bridged into an Objective-C project.
Without that conformance/inheritance, you'll end up missing some crucial functionality to Objective-C like message sending.
All of that being said, an Objective-C class has to inherit from a parent class and the default root class is NSObject. You almost definitely want to just inherit and make your class a PONSO.
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?
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.