0

I'm getting an error stating: Property 'clientChatLoad' not found on object of type 'DataManager' when using the following implementation:

AppDelegate.m

 #import "IOS_Project_Name-Swift.h"

@class DataManager;

...

    DataManager.clientChatLoad(0){ data, error in
        guard data != nil else {
            print(error)
            return
        }

        guard let data = data else { return }
        let json = JSON(data: data)

        if let result = json["success"].bool{
            if (result){

                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.5 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) { () -> Void in
                    NSNotificationCenter.defaultCenter().postNotificationName("refreshChatDetails", object: nil, userInfo:nil)
                }
            }

        }

DataManager.swift

    ...

   @objc class func clientChatLoad(_ chatId: Int, completionHandler: @escaping (Data?, NSError?) -> ()) -> URLSessionTask {
        // AJ TO-DO
        var defaults = UserDefaults.standard
        let URL = Foundation.URL(string: chatUrl)!
        var request = URLRequest(url: URL, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 3.0)
        request.httpMethod = "POST"

    ...

From what I understand adding @class DataManager to my Objective C class as well as @objc before class func clientChatLoad should expose the Swift code to my Objective C class... but I still receive an error. What might I have overlooked?

  • 2
    Your code makes no sense. Your `AppDelegate.m` code should be in Objective-C, but it is in Swift. – matt Mar 09 '17 at 14:32
  • Does your Objective-C _.m_ file `#import` the generated _-Swift.h_ bridging header? – matt Mar 09 '17 at 14:33
  • Yes. (I updated the code snippet to reflect that.) – Droidthusiast Mar 09 '17 at 14:47
  • Still makes no sense, though, since your `DataManager.clientChatLoad` call is in Swift, not Objective-C. And anyway, an Objective-C _.m_ file _never_ needs to contain a `@class` declaration. Please make an effort to _show your real code_. – matt Mar 09 '17 at 14:49
  • How might I go about doing so in objective C? Looking at other code examples it appears I'd need to do something along the lines of: DataManager *dataMan = [DataManager new]; [dataMan clientChatLoad]; however the result is the error: Receiver 'DataManager' for class message is a forward declaration – Droidthusiast Mar 09 '17 at 14:54
  • I don't know what you mean by "doing so". You still have not shown us what your code _actually is_ that is generating the error! I mean, you cannot _really_ be trying to put Swift code into an Objective-C _AppDelegate.m_ file — can you??? – matt Mar 09 '17 at 15:11
  • And I already told you, you need to delete that `@class` line. – matt Mar 09 '17 at 15:21
  • I just posted an Objective C code snippet in my last comment and the error it produced... (I replaced my Swift call to clientChatLoad with that Objective C implementation [apologies if this is coming from Noobville... I'm usually an Android/Java developer and I've been tasked with making some changes to this project recently... just trying to work my way through it]). – Droidthusiast Mar 09 '17 at 15:22
  • And I told you what to do about the error. But comments are no place to discuss code. You should fix your question so that it is about the Objective-C code. – matt Mar 09 '17 at 15:25

2 Answers2

2

At first I thought your code was just some sort of misprint, but over the course of discussion in the comments I have come to realize that it is real and that you are attempting to put Swift code into an Objective-C file (namely AppDelegate.m). You cannot do that! An Objective-C file must be written entirely in Objective-C!

I would question whether you really even need a hybrid app (an app written in two languages). Your life will be much simpler if you pick just one language and write the whole app in that language. But if you are going to have a hybrid app, you need to learn Objective-C and write the Objective-C in Objective-C.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thank you for the clarification - I'll take two steps back, rethink the approach and come back with a better implementation. Much appreciated. - A complete iOS noob. – Droidthusiast Mar 09 '17 at 15:30
1

make sure your DataManager class inherits from NSObject. You should be good.

Priyatham51
  • 1,864
  • 1
  • 16
  • 24
  • Apologies (I'm very new to both languages) would that be something along the lines of: class func clientChatLoad(_ chatId: NSObject, Int, completionHandler: @escaping (Data?, NSError?) -> ()) -> URLSessionTask { – Droidthusiast Mar 09 '17 at 14:50