0

I am new in iOS. I have a lib that I use in my App both in Swift and Objc files (I have connected my lib correctly including the *-Swift.h file).

So, my lib has such a structure

Factory

import Foundation

@objc public class Factory : NSObject{
    @objc public func create() -> ModelProtocol
    {
        return FirstModel()
    }
}

Model protocol

import Foundation

@objc public protocol ModelProtocol
{
    func foo()
}

First Model

import Foundation

public class FirstModel : NSObject, ModelProtocol
{
    public func foo() {
        //Do some things here
    }
}

when I try to use this factory on the Swift side everything is working properly

import Foundation
import Lib

class TestClass {
    
    public init(){}
    
    func boo()
    {
        let factory = Factory()
        let _: ModelProtocol = factory.create()
    }
}

but when I try to do the same on the Objc side I got an error

Use of undeclared identifier 'ModelProtocol'
#import <Foundation/Foundation.h>
#import "Lib-Swift.h"


@implementation ViewController : NSObject

- (void)foo {
    Factory *factory = [Factory new];
    ModelProtocol *protocol = [factory create];
}

@end

also I put both these projects (lib & app) on my drive https://drive.google.com/drive/folders/1-DMYMhTqTqpgHcp31DNVby7US46OXUYM?usp=sharing

How to fix it?

1 Answer 1

1

Actually your only problem is incorrect syntax of protocols in Objective-C. Correctly, a type conforming to protocol would be written this way:

id<ModelProtocol> protocol = [factory create];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.