Creation and invocation of the framework of swift and OC mixing first create a project with a name of "Mymixed" and select Ios-framework&library-cocoa Touch The framework then creates a Swiftview.swift file inside, a OBJC ocview file and Myocview file three files are inherited UIView first in Swiftview call Ocview
1 Import UIKit2 3 4 5 classSwiftview:uiview {6 7 8 9 Init (frame:cgrect) {Ten One super.init (frame:frame) A -Self.backgroundcolor =Uicolor.redcolor () - the - -var Ocview = Ocview (Frame:cgrectmake (0,0, -, -)) - + Self.addsubview (Ocview) - + } A at - - } - -
And then call Swiftview in Myocview.
1 @implementationMyocview2 3-(Instancetype) initWithFrame: (CGRect) Frame4 5 {6 7Self =[Super Initwithframe:frame];8 9 if(self) {Ten OneSelf.backgroundcolor =[Uicolor Graycolor]; A -Swiftview *sview = [[Swiftview alloc] Initwithframe:cgrectmake (0,0, -, -)]; - the [self addsubview:sview]; - - } - + returnSelf ; - + A at}
This time the compilation will be wrong, because you can not find the path to each other, the project mymixed build settings-swift compiler-objective-only bridging header to join the header file path mymixed/ OCView.h Let Swiftview find OCView.h
And then join in the OCVIEW.M.
The project name plus a name generated at compile time, so you can use the Swift file
#import "Mymixed/mymixed-swift.h"
Can be compiled, this time the hybrid framework is finished.
Re-build a project called Mixpro here we use swift language
Then add files to "... "Import the framework's project files (you can also import the compiled framework directly), then compile the framework project to see if it compiles, and then click Link Binary with libraries in the build phases of the Mixpro project. Add Mymixed.framework, this time the framework is added to finish, compile to see if it is passed
Then add the code to the Viewcontroller.
1 Import UIKit2 3 Import mymixed4 5 6 7 classViewcontroller:uiviewcontroller {8 9 Ten One Overridefunc viewdidload () { A - super.viewdidload () - the //additional setup after loading the view, typically from a nib. - - - +var Swiftview = Swiftview (Frame:cgrect (x: -Y: -, Width: -, Height: -)) - + Self.view.addSubview (Swiftview) A at - -var Myocview = Myocview (Frame:cgrectmake ( -, $, $, $)) - - Self.view.addSubview (Myocview) - in } - to + - Overridefunc didreceivememorywarning () { the * super.didreceivememorywarning () $ Panax Notoginseng //Dispose of any resources the can be recreated. - the } + A the +}
This is going to go wrong because we can't find myocview because we chose Swift when we created the Mymixed project, so only the. Swift file can be seen outside of the MyMixed.h file that we click on Mymixed project to see
In this header, you should import all the public headers of your framework using statements like #import
It is not difficult to understand that if you want other files of this framework to be seen by external engineering, we need to #import this header file, but this must be PublicHeader.h so we click Headers in the build phases of mymixed project, You can see that there is only MyMixed.h in public, we want to use MyOCView.h externally, we need to drag it from project to public, then recompile, error-Erase
Attach a photo
iOS development--a framework of practical articles &swift and object-c