I'm now using Swift class in my existing Object-C project to integrate sign in with apple.In my Swift class, I'm using ASAuthorizationControllerPresentationContextProviding delegate. However, when ProjectName-Swift.h is generated, an issue in ProjectName-Swift.h file shows that Can't find protocol declaration for 'ASAuthorizationControllerPresentationContextProviding'. Works well in my pure swift project but can't work with mixed project I have.
ProjectName-Swift.h
Related
I created a Cocoa Touch Framework, this framework contains mostly Objective-C codes, my goal is to keep these objective-c codes private, so I added an adapter class in Swift as a middleman between the consumer and my objective-c codes. To do this, I created module to import my objective-c classes and allow my swift class to access these objc classes. I used this guide and clang doc.
So with this I am able to access my objc codes inside my Swift class.
import PrivateObjc
public class SwiftClass{
public func createPersonClass() -> ObjcPerson {
return ObjcPerson()
}
}
The code snippet above works just fine, within my framework atleast. But when I try to use my framework in another project, I get this error:
Missing required module 'PrivateObjc'
I've seen similar posts here in SO but most of them are using Carthage or Cocoapods, some about CommonCrypto. This question is about a custom framework created from scratch. Can anyone enlighten me how to accomplish this? Thanks!
I have a project with large amount of obj-c and swift code inside it.
I have an obj-c class which imports ProjectName-Swift.h to let that class access swift code.
Now I need to expose that obj-c class to the swift so I can access it from swift code.
The problem is that after import in bridging header name of obj-c class project stops building with error that it cannot find ProjectName-Swift.h which import is stated in obj-c class.
I cannot remove import of ProjectName-Swift.h from obj-c class because after that class breaks.
What can I do ?
OK, had one answer and then re-read the question. Make absolutely certain that your import of the swift header is the Module Name of the project (not necessarily the project name):
Apple Documentation: Swift and Objective-C in the Same Project
More specifically, go to the section marked "Importing Swift into Objective-C" and read it. And even more specifically:
Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to “Yes”.
Import the Swift code from that framework target into any Objective-C .m file within that framework target using this syntax and substituting the appropriate names:
#import <ProductName/ProductModuleName-Swift.h>
I've been wrestling with this specific issue for a while now. Basically I've added a framework BigNerdRanch CoreDataStack using Carthage.
But I've been having issues of using the said framework on my Objective-C codes. My project is an Objective-C project in which I recently added Swift codes and Swift frameworks.
So far I've been able to use Swift frameworks in my Swift codes with no problems. But once I started using the said Swift frameworks on my old Obj-C, specifically AppDelegate.m, I keep getting compiler error no matter what.
From what I understand I have to import it like this:
#import <CoreDataStack/CoreDataStack-Swift.h>
In which I did on AppDelegate.m.
I also have the following setup on my project:
Project Target Setup:
Build Settings for Swift Compilation:
I've been seeing some answers in StackOverflow to add public to the classes. But I'm unable to do so since the imported Frameworks are self-contained and only contains Headers for me to see in the XCode project.
Thanks for the help guys.
You added a framework written in swift, if they don't enable support for Objective C, you can't use it in your objective c code.
However, you can create a class in swift, which will be used as a mediator between the framework and your ObjC classes.
#objc class coreDataStackConvertor: NSObject {
func createStack(arg: Type) {
// Code which uses the real CoreDataStack
}
}
From you objective c classes, you can use the methods in the convertor class.
I have a framework (let's call it MyKit) written in Objective-C that I'm extending with some Swift classes. I'm trying to get my head around it using this documentation: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_77
As far as I understand, I'm not supposed to have a bridging header class, and instead put all my includes in the umbrella header file (that I understand to be ).
So I write in MyKit.h:
#import <MyKit/ModelObjectA.h>
#import <MyKit/ModelObjectB.h>
#import <MyKit/ControllerObjectC.h>
I don't list ControllerObjectD.swift, even though it goes into here as well? Or should I include
#import <MyKit/ControllerObjectD-Swift.h>
?
ControllerObjectD uses ModelObjectA and ModelObjectB. Now that I don't have a bridge headerfile, I get compile errors in it because it cannot find these objects.
The documentation says "Swift will see every header you expose publicly in your umbrella header." and this is true when I import the framework into other projects, but the framework project cannot compile because it doesn't see it. I have turned on the "Define Modules" build setting.
Is there something I've misunderstood about the umbrella header, perhaps? Where can I say "hi project, this is the umbrella header file"? The framework compiles if I set the umbrella header file as bridging header, but that sounds like I've come back to the beginning this way?
Cheers
Nik
I believe your problem may be down to the Access Modifiers in your Swift class, however I've written a short guide and sample project to help you:
Sample project can be found here
There are two parts to having a mixed language framework:
Importing Objective-C into Swift
Importing Swift into Objective-C
1. Importing Objective-C into Swift
This is, for example, if you have an Objective-C class named Player that you want to add to a swift class called Game.
According to the documentation, you need to do these two steps to import the Player object into the Game object.
Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes.
In your umbrella header file, import every Objective-C header you want to expose to Swift.
#import <Framework/Player.h>
Make sure your Player header file in Objective-C is marked for public target membership in the framework:
Following these steps, it's possible to import the Player Objective-C class into the Game Swift Class:
import UIKit
public class Game: NSObject {
public let player: Player
public init(player: Player) {
self.player = player
super.init();
}
}
2. Importing Swift into Objective-C
For importing the Swift Game class into the Player object, we can follow a similar procedure.
As before; Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes.
Import the Swift code from that framework target into any Objective-C .m file within that framework target using this syntax and substituting the appropriate names:
#import <ProductName/ProductModuleName-Swift.h>
In my case, this works as:
#import <SwiftObjC/SwiftObjC-Swift.h>
and I assume, for you:
#import <MyKit/MyKit-Swift.h>
Therefore, make sure that all the properties, methods and classes you want to access are defined as public in your swift file or else they won't be visible to Objective-C .
I have uploaded my sample project showing how this all works on GitHub https://github.com/elliott-minns/SwiftObjCTestFramework
I hope this helps you with your problem.
I think you need to do bridging-header to access your obj-c code in swift.
More in the link
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
so it is a file with your imports from obj-c transfering to swift. names are very important so care about that. hope it helps
p.s. also you need to add this header path to your project settings
From the Documentation at Apple: A Swift class must be a descendant of an Objective-C class to be accessible and usable in Objective-C. For more information about what you can access from Objective-C and how the Swift interface is imported, see Swift Type Compatibility.
So your public swift class needs to extend NSObject for example.
I have a Swift Project, and I'm using bridging-header to call some Obj-c Classes and it works great. and I can easily use Obj-C classes alongside Swift.
But now, I have problem calling one of my Swift classes from Obj-C.
I mean I want to call fun x from y class, which is a swift class, from another obj-c class in my project.
I've used 'y-Swift.h' and some other methods, but non of them worked. :(
From the documentation, you have to use something like this ProductName/ProductModuleName-Swift.h. You can get your product name from the project settings in XCode.
Another way to import is as followed:
#import "ProductModuleName-Swift.h"