美文网首页
Swift跨语言调用以及取舍(笔记)

Swift跨语言调用以及取舍(笔记)

作者: 952625a28d0d | 来源:发表于2016-11-12 20:04 被阅读565次

    引用语法和值语法

    DF945236-A37A-41AF-9CAA-B70C42969AB9.png
    • Class为引用语法,而Struct为值语法
    import UIKit
    
    class Refrence{
        var s:String = "hello"
    }
    
    struct Value{
        var s:String = "hello"
    }
    
    let a = Refrence()
    
    let b = a
    b.s = "world"
    print("Class \(a.s)")
    
    let c = Value()
    
    var d = c
    d.s = "world"
    print("Class \(c.s)")```
    
    
    ![Paste_Image.png](https://img.haomeiwen.com/i189984/2d21a6511b444c2e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    ###跨语言调用
    - 首先当我们在Swift项目中创建OC、C、C++文件的时候,系统自动提示我们创建一个Bridge文件,用来引入其他类型文件的头文件
    
    ![Paste_Image.png](https://img.haomeiwen.com/i189984/87f3990211c614cc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    - OC 与 Swift
    
    - 先写一个简单的OC文件
    
    

    import <Foundation/Foundation.h>

    @interface ObjDemo : NSObject

    • (NSString *)getTitle;
    • (void)setTitle:(NSString *)title;

    @end```

    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c81b13}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; min-height: 21.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c42275}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000}span.s1 {font-variant-ligatures: no-common-ligatures; color: #822d0f}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s4 {font-variant-ligatures: no-common-ligatures; color: #c42275}
    
    #import "ObjDemo.h"
    #import "ViewController.swift"
    
    @implementation ObjDemo{
        NSString *_title;
    }
    
    - (NSString *)getTitle{
        return _title;
    }
    
    - (void)setTitle:(NSString *)title{
        _title = title;
    }
    
    @end```
    
    - 在Bridge文件中加入OC头文件
    
    ![Paste_Image.png](https://img.haomeiwen.com/i189984/0666afb64012bff1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    - 在Swift文件中引用OC文件
    
    ```swift
    let c = ObjDemo()
            c.setTitle("title")
            let t = c.getTitle() as String
            print(t)```
    
    ###Swift And C
    
    - 先初始化一个C文件
    
    ```c
    #include <stdio.h>
    
    int getAnswer(){
        return 42;
    }
    
    // 在外界传递一个指针进来,然后在C函数中对这个指针进行修改
    void generateAnswer(int * answer){
        *answer = 42;
    }```
    
    - 引入C文件的方法名到Bridge文件中
    
     ```C
    int getAnswer();
    void generateAnswer(int * answer);```
    
    - Swift文件中调用
    
    

    // Swift中对C的调用
    let a = getAnswer()
    print(a)

        // Swift中数据通过指针传递给C
        var pi:Int32 = 43
        generateAnswer(&pi)
        print(pi)```
    

    Swift And C++

    • 这里建议先使用OC文件将C++内容进行封装,然后再加入Bridge文件中进行使用

    • 先初始化C++文件

    • .cpp文件中

    #include "Document.hpp"
    
    void Document::setTitle(const std::string &title){
        title_ = title;
    }
    
    std::string Document::getTitle(){
        return title_;
    }```
    
    - .hpp文件中
    
    

    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #822d0f}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; min-height: 21.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c42275}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #c81b13}span.s3 {font-variant-ligatures: no-common-ligatures; color: #c42275}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s6 {font-variant-ligatures: no-common-ligatures; color: #822d0f}

    ifndef Document_hpp

    define Document_hpp

    include <string>

    class Document {
    public:
    void setTitle(const std::string & title);
    std::string getTitle();
    private:
    std::string title_;
    };

    endif /* Document_hpp */```

    • 创建OC文件对其进行封装,这里要注意把.m文件改成.mm就可以正常使用C++的语法了

    .mm

    #import "DocumentWapper.h"
    #include "Document.hpp"
    
    @interface DocumentWapper(){
        Document doc;
    }
    
    @end
    
    @implementation DocumentWapper
    
    - (NSString *)getTitle{
        return [NSString stringWithUTF8String:doc.getTitle().c_str()];
    }
    
    - (void)setTitle:(NSString *)title{
        auto t = std::string([title cStringUsingEncoding:NSUTF8StringEncoding]);
        doc.setTitle(t);
    }
    
    @end```
    
    .h暴露头文件
    
    

    import <Foundation/Foundation.h>

    @interface DocumentWapper : NSObject

    • (NSString *)getTitle;

    • (void)setTitle:(NSString *)title;

    @end```

    • 在Swift文件中调用
    // Swift和C++交互 最好先用OC对C和C++进行封装
            let wa = DocumentWapper()
            wa.setTitle("C++_Title")
            let waT = wa.getTitle() as String
            print(waT)```
    
    ###ARC回收机制
    
    ![4B7273A1-C5C8-4C76-91BD-A88DD437E99B.png](https://img.haomeiwen.com/i189984/f689c344ba871a11.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    ![AB50155C-2F6B-4E7A-BF47-196C6044F4F8.png](https://img.haomeiwen.com/i189984/9b6af764eb68c53f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

    相关文章

      网友评论

          本文标题:Swift跨语言调用以及取舍(笔记)

          本文链接:https://www.haomeiwen.com/subject/bykqpttx.html