Creation and invocation of a mixed framework for swift and OC Integrated Advanced Tutorials

Source: Internet
Author: User
Tags uikit

First create a project with a name of "Mymixed" and select Ios-framework&library-cocoa Touch framework

Then create a swiftview.swift file inside, a OBJC ocview file and Myocview file three files are inherited UIView first in Swiftview call Ocview

Import UIKit

Class Swiftview: UIView {

init (frame: cgrect) {

Super. Init (frame:frame)

Self . backgroundcolor = uicolor.redcolor ()

var ocview = Ocview (frame:CGRectMake (0,0,.))

Self . Addsubview (ocview)

}

}

And then call Swiftview in Myocview.

@implementation Myocview

-(instancetype) initWithFrame: (cgrect) frame

{

Self = [super Initwithframe:frame];

if (self) {

Self. BackgroundColor = [Uicolor graycolor];

Swiftview *sview = [[swiftview alloc] initwithframe:CGRectMake (0, 0, + )];

[self addsubview:sview];

}

return self;

}

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.

Import UIKit

Import mymixed

Class Viewcontroller: uiviewcontroller {

override func viewdidload () {

Super. viewdidload ()

Additional setup after loading the view, typically from a nib.

var swiftview = Swiftview (frame:cgrect (x:y: +, Width: +, Height:))

Self . view. Addsubview (swiftview)

var myocview = Myocview (frame:CGRectMake ($, $))

Self . view. Addsubview (myocview)

}

override func didreceivememorywarning () {

Super. Didreceivememorywarning()

Dispose of any resources the can be recreated.

}

}

This time it's going to go wrong because I can't find it.Myocview, because we chose Swift when we created the Mymixed project, so only the. Swift file can be seen externallyWe click on the MyMixed.h file of the Mymixed project to see

In this header, you should import all the public headers of your framework using statements like #import

It's not hard 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 on the build of Mymixed project Phases in the headers, you can see the public only MyMixed.h, we want to use the outside of the MyOCView.h is to drag it from project to public, and then recompile, error elimination compiled by

Creation and invocation of a mixed framework for swift and OC Integrated Advanced Tutorials

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.