2

The pod I would like to use does not have the prefix @objc at the beginning of the classes I want to use.

Can I still use the pod in an objective-C project?

--

PS: My question is more general than How to import and use Swift Pod Framework in Objective-C Project (which focuses on an issue with #import statements)

Community
  • 1
  • 1
Colas
  • 3,473
  • 4
  • 29
  • 68
  • Possible duplicate of [How to import and use Swift Pod Framework in Objective-C Project](http://stackoverflow.com/questions/27995691/how-to-import-and-use-swift-pod-framework-in-objective-c-project) – JAL Dec 22 '16 at 17:23

2 Answers2

4

All classes in ObjC must conform to the objc_object structure. In the vast majority of cases, that means it's a subclass of NSObject. There are a few other ways that classes can conform to the objc_object structure: subclassing NSProxy or just by cleverly laying out their own structure to happen to match it and adding hooks, often in the system libraries, to deal with the differences, like in toll free bridging or libSystem. Swift objects by default do not do this, so they aren't visible to ObjC.

Adding @objc isn't what you need. In fact, that generally won't compile. You need to make the class a subclass of NSObject. That's a significant change that you can't make with compiler options. It changes memory layout, message dispatching, and other behaviors. You need to change the source code and recompile at least.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
1

As Apple guideline says,

Private declarations are not exposed to Objective-C unless they are explicitly marked with @IBAction, @IBOutlet, or @objc as well.

So, if you have an "open" keyword before class definition, it should be ok.

P.S.: At least, i've got no troubles importing TTGSnackbar pod in my Objective-C project

Artem Zaytsev
  • 1,621
  • 20
  • 19
  • Nope it is just a `public class`. This project: https://github.com/alehed/SyntaxKit – Colas Dec 22 '16 at 17:22
  • public and open are equivalent, so it's fine – Artem Zaytsev Dec 22 '16 at 17:25
  • When I want to use it in an objective-C class, the classes of the Swift project are not available. Maybe I should create an intermediary Swift class. – Colas Dec 22 '16 at 17:27
  • Importing swift-header should be enough. E.g.: #import "ProductModuleName-Swift.h". What do you mean by they are not available? – Artem Zaytsev Dec 22 '16 at 17:30
  • 1
    Well, in SyntaxKit/SyntaxKit.podspec there are no option specified for spec.module_name. Try to add in this file following line: spec.module_name = "SyntaxKit". clean and rebuild project as well – Artem Zaytsev Dec 22 '16 at 17:39
  • 1
    It does not work from an `ObjC` class. It seems I can only access the classes that subclass `NSObject`. – Colas Dec 22 '16 at 18:03