ios - import swift class in objective-c, <myModule>-Swift.h file not found -
i have ios project written objective-c. created swift class in project, bridging header file accessing objective-c in swift generated successfully, , works fine.
my problem other way around. want import swift class in objective-c code.
in xcode, target -> build settings--> swift compiler section, see objective-c generated interface header name
field value mymodule-swift.h
, when import header in objective-c class:
#import "mymodule-swift.h"
i compiler error:
mymodule-swift.h file not found
and in project, cannot find file either. how can solve problem?
my xcode version 6.4
go to
build settings->objective-c generated interface header name
and set value yourmodule-swift.h (this set, filename need import on .m file#import "yourmodule-swift.h"
go build settings , search "defines module", set both values yes
create class in swift prefix of
@objc
example@objc class myswiftclass{...}
go window->projects , press delete button on "derived data"
build project again
should work now.
why add @objc?
a swift class or protocol must marked @objc attribute accessible , usable in objective-c.
this @objc
prefix, tells compiler generate swift class header file. add "mymodule-swift.h" file, example:
@objc class mycustomviewcontroller : uiviewcontroller {..}
will generate code:
swift_class("_ttc10mycustomviewcontroller4viewcontroller") @interface viewcontroller : uiviewcontroller - (void)viewdidload; - (void)viewdidappear:(bool)animated; - (void)didreceivememorywarning; - (swift_nullability(nonnull) instancetype)initwithnibname:(nsstring * __nullable)nibnameornil bundle:(nsbundle * __nullable)nibbundleornil objc_designated_initializer; - (swift_nullability(nonnull) instancetype)initwithcoder:(nscoder * __nonnull)adecoder objc_designated_initializer; @end
allowing objective-c know class.
Comments
Post a Comment