1

Hello I have been looking at different questions regarding this and none of the solution seem to work for me. I have an object which I am trying to use in objective C, however I keep getting the 'No visible interface for object' when trying to initialize the object. I have properly imported my module into the objective C file and added the @objc annotation am i missing something? thank you in advance!

Code:

Swift-

import UIKIT

@objc class SaveFile: NSOBject {
 var name: String?
 var location: String?

//@objc(initId:andSize:) does not work
init(id: String, size: String) {
    super.init()
   //other code
}

}

Objective C -

    import "testProject-Swift.h"

// functions and initializers

-(void) saveFile {
    SaveFile *fileToSave = [[SaveFile alloc] initWithId:self.currentId size:self.size] //No visible @interface for 'SaveFile' declares the selector 'initWithId:Size:'

}
paul590
  • 1,385
  • 1
  • 22
  • 43
  • 1
    `NSOBject` reveals that this is *not* an exact copy of your real code. – Martin R Apr 06 '18 at 17:29
  • 1
    You need `@objc` for your properties, too. – Rob Apr 06 '18 at 17:30
  • @MartinR you are correct its not, I have changed the values and names of my project to include in this sample, however, the layout and values are all the same, does this have anything to do with the issue at hand? – paul590 Apr 06 '18 at 17:31
  • @Rob thank you, when I included this information in the init it still did not appear in my objective C file – paul590 Apr 06 '18 at 17:31
  • @paul590: You posted code that does not compile, that puts an unnecessary burden on those people who try to reproduce the issue. – Martin R Apr 06 '18 at 17:32
  • 2
    You need `@objc` qualifier there, too, e.g. `@objc init(id: String, size: String) { ... }`. – Rob Apr 06 '18 at 17:36
  • @MartinR you are correct, however, I was looking to see if anybody had ideas and have a discussion about the solutions since im unable to post my entire code, no need to post unnecessary comments. – paul590 Apr 06 '18 at 17:36
  • @Rob you are the man that did it! Thank you for your help regarding this issue – paul590 Apr 06 '18 at 17:38
  • @Rob I appreciate it Rob! I will include more information next time! thank you again! – paul590 Apr 06 '18 at 17:50

1 Answers1

2

Try this:

@objcMembers
class SaveFile: NSObject {//<-NSObject, not NSOBject
    var name: String?
    var location: String?

    init(id: String, size: String) {
        super.init()
        //other code
    }

}

When you write a class which is used in Objective-C code, it's an easy way to annotate with @objcMembers.

Or else, you can annotate all properties and methods including initializers with @objc:

class SaveFile: NSObject {
    @objc var name: String?
    @objc var location: String?

    //@objc(initWithId:size:) do work
    @objc init(id: String, size: String) {
        super.init()
        //other code
    }
}
OOPer
  • 47,149
  • 6
  • 107
  • 142