0

I have been trying to solve this problem. So I thought of creating a new simple new framework on swift 4.1.

My code is this:

public class TestStupidity: NSObject {
    public static var shared = TestStupidity()
    public func hello() {
        print("hello")
    }
}

However even this small piece of code is not working out for me. The public class and it variables dont show up in the header files.

I have uploaded this project on GitHub at link.

Can anyone help me figure what is it that I am doing wrong here?

Amogh Shettigar
  • 275
  • 2
  • 3
  • 18

2 Answers2

0
  1. Use open keyword: An open class is accessible and subclassable outside of the defining module. An open class member is accessible and overridable outside of the defining module. Also add @objcMembers

    @objcMembers open class ARCompactDeviceInfo: NSObject {}

  2. Go to Target -> Build Phase -> Headers add Umbrella Header file(the file with same name of project) file in Public as shown in picture. Like in my project I've CryptoAPI.h and rest of files hiding Example of Swift 4 Framework. enter image description here

  3. Go to Target -> General -> Embedded Binaries drag your framework. enter image description here

EDITED: @Amogh Shettigar: I've downloaded your project and Integrated with Sample App after adding above details. Here is URL for both Framework and Integrated App

Aleem
  • 3,173
  • 5
  • 33
  • 71
  • I tried the changes mentioned by you. But the class is still not visible after I import it in my swift app. – Amogh Shettigar Apr 19 '18 at 12:52
  • Public is enough to use the class. – Aaron Bratcher Apr 19 '18 at 13:57
  • @AmoghShettigar Check my updated Answer. 3rd Point will help you. If work kindly accept and upvote – Aleem Apr 19 '18 at 14:00
  • @Aleem adding the file to public headers will mean that the entire file along with the code will be added to the `.framework`. I would like to keep my implementation hidden – Amogh Shettigar Apr 20 '18 at 06:06
  • @AmoghShettigar Yes this is true, if you're concerned about privacy then only add umbrella header file in Public and rest of file add into project. I also updated my answer accordingly. – Aleem Apr 20 '18 at 06:18
  • @Aleem it still isn't showing up in my header file. I have uploaded the code [here](https://github.com/amoghvs22/TestStupidity/tree/master/Tester). Could you please have a look? – Amogh Shettigar Apr 20 '18 at 07:27
  • Ok let me check – Aleem Apr 20 '18 at 11:43
  • Check my answer, Under Edited section I practically Implement your problem. Now I hope this will resolve your issue. – Aleem Apr 20 '18 at 11:57
  • @Aleem I highly appreciate your help, but I am trying to develop a app in Swift4, importing in Objc app works, but if I build the same framework and add to a swift 4 app then it does not work. – Amogh Shettigar Apr 20 '18 at 12:31
  • @AmoghShettigar, If you're trying to create a framework binary that is to be distributed, look here for some great instructions: https://eladnava.com/publish-a-universal-binary-ios-framework-in-swift-using-cocoapods/ Also see my comment in the other answer about the header file – Aaron Bratcher Apr 20 '18 at 12:49
0

Follow these steps and see that it works just fine:

Create new project of type Cocoa Touch Framework call it Tester

Add new Swift file called TestStupidity.swift

Add the code from your question to the file:

public class TestStupidity: NSObject {
    public static var shared = TestStupidity()
    public func hello() {
        print("hello")
    }
}

Add new target to project of type Single View App and call it TesterApp

Select the TestApp target and in the General tab, click the + sign for Embedded Binaries. Select Tester framework

In the ViewController.swift file, set code to this:

import UIKit
import Tester

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let testClass = TestStupidity()
        testClass.hello()
    }
}

Run TestApp and see "hello" in the output.

Aaron Bratcher
  • 6,051
  • 2
  • 39
  • 70
  • This code worked for me thanks. But is there a way where I can keep separate workspace for both my framework and app, as I have it working in swift 3 and would like to keep it that way? – Amogh Shettigar Apr 20 '18 at 06:15
  • I have mentioned my main issue in another question [here](https://stackoverflow.com/questions/49914538/unable-to-see-public-protocol-from-framework-after-converting-to-swift-4-1) – Amogh Shettigar Apr 20 '18 at 06:16
  • @AmoghShettigar, you keep mentioning a header file. Following my steps above, can you direct me to the header you're talking about? Perhaps the problem isn't what you think. – Aaron Bratcher Apr 20 '18 at 12:44
  • @AmoghShettigar, If you want to keep it working in Swift 3 too, that's a completely different situation. For now, The framework and parent app need to be compiled with the same version of Swift. See discussion here: https://stackoverflow.com/questions/49922008/swift-framework-problems-with-xcode-compatibility/49922881#49922881 – Aaron Bratcher Apr 20 '18 at 12:54