1

Am getting error while calling swift function in objective c file. I have created "-Swift.h" file.

I have created ".swift" file. My code here is

import Foundation
@objc class className : NSObject {
@objc func someFunc(sender: AnyObject) {
//Some code
}
}

I have created "-Swift.h" header file. My code here is

#ifndef ProjectName_Swift_h
#define ProjectName_Swift_h
#endif /* ProjectName_Swift_h */
@class className;
@interface className : NSObject
-(void)someFunc;
@end

In my objective C ".m" file am calling the swift function.

#import "XXX-Swift.h"

- (void)viewDidLoad
{
    [super viewDidLoad];
    className *obj = [[className alloc]init];
    [obj someFunc];
}

Now am getting error in "XXX-Swift.h" file.

ERROR: Cannot find interface declaration for 'NSObject', super class for 'className'.

Pallavi Konda
  • 1,640
  • 1
  • 12
  • 18

3 Answers3

3

delete the file you created ending in -Swift.h, that file is auto-generated by Xcode.

you can view the Xcode generated version (after a successful build) if you command click #import "XXX-Swift.h"

if you go into your build settings and search for SWIFT_OBJC_INTERFACE_HEADER_NAME you can see the file name.

Casey
  • 6,531
  • 24
  • 43
0

you have to created Bridging header file and import you swift file there.

Use this link, it will help you

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

vikash1307
  • 272
  • 2
  • 12
  • Check this link, you get you solution http://stackoverflow.com/questions/24102104/how-to-import-swift-code-to-objective-c – vikash1307 Feb 24 '16 at 05:04
  • For accessing swift code in objective c i think no need to create bridging header.h. I just created -swift.h even though i tried am getting the same error – Pallavi Konda Feb 24 '16 at 05:10
  • Check out apple documentation , that i given a link above, it will solve your problem. – vikash1307 Feb 24 '16 at 05:12
-1

I found difference between manually creating -Bridging-header file and automatically Xcode generated -Bridging-header file. I have deleted the -Swift.h file and manually created -Bridging-header file. Added -Bridging-header(Xcode generated one) file. It automatically created -Swift.h file after successful build of project.

Pallavi Konda
  • 1,640
  • 1
  • 12
  • 18