Swift调用C、C++、Object

1.Swift调用C语言
a,首先在项目中添加 CFile 文件命名为CHello,同时产生桥梁文件。

b,创建之后的项目结构

b,在CHello.h文件中编写接口方法,在CHello.c编写实现方法,最后在SwiftCallC-Bridging-Header.h文件中引入CHello.h

CHello.h文件

#ifndef __SwiftCallC__CHello__
#define __SwiftCallC__CHello__

#include <stdio.h>

void sayHello();

#endif /* defined(__SwiftCallC__CHello__) */

CHello.c文件

#include "CHello.h"

void sayHello(){
    printf("hello\n");
}

SwiftCallC-Bridging-Header.h文件

#import "CHello.h"

2.Swift调用OC语言
a,同理先在项目下添加object-c文件命名为OCHello。这里因为已经创建过文件桥梁,这里就不需要了。

b,在OCHello.h文件中编写接口方法,在OCHello.c编写实现方法,最后在SwiftCallC-Bridging-Header.h文件中引入OCHello.h头文件

OCHello.h文件

#import <Foundation/Foundation.h>

@interface OCHello : NSObject
//方法没有小括号,真不适应- -!
-(void) sayHello;
@end

OCHello.m文件

#import "OCHello.h"

@implementation OCHello
-(void)sayHello{
    NSLog(@"hello");
}
@end

SwiftCallC-Bridging-Header.h文件

#import "CHello.h"
#import "OCHello.h"

c,最后在Swift代码中调用Object-C即可以了~

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        var oc = OCHello()
        oc.sayHello()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

3.Swift调用C++语言
调用c++代码只要将原来的OCHello.m文件改为OCHello.mm,然后就可以在该文件中引用iostream头文件....^^瞬间有c++的feel了!
最后稍微修改下实现的方法就可以了。

#import "OCHello.h"
#include "iostream"

using namespace std;

@implementation OCHello
-(void)sayHello{
    cout<<"Hello"<<endl;
}

@end
 

ios开发——实用技术篇Swift&Swift调用C、C++、Object的更多相关文章

  1. ios开发——实用技术篇&Pist转模型详细介绍

    Pist转模型详细介绍 关于Plist转模型在iOS开发中是非常常见的,每开一一个项目或者实现一个功能都要用到它,所以今天就给大家讲讲Plist怎么转成模型数据, 前提:必须有一个Plist文件或者通 ...

  2. ios开发——实用技术篇&网络音频播放

    网络音频播放 在日常的iOS开发中,我们通常会遇到媒体播放的问题,XCode中已经为我们提供了功能非常强大的AVFoundation框架和 MediaPlayer框架.其中AVFoundation框架 ...

  3. ios开发——实用技术篇Swift篇&地址薄、短信、邮件

    //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnimated(tru ...

  4. ios开发——实用技术篇Swift篇&播放MP3

    播放MP3 // MARK: - 播放MP3 /*----- mp3 ------*/ //定时器- func updateTime() { //获取音频播放器播放的进度,单位秒 var cuTime ...

  5. ios开发——实用技术篇Swift篇&拍照

    拍照 // MARK: - 拍照 func fromPhotograph() { if UIImagePickerController.isSourceTypeAvailable(.Camera) { ...

  6. ios开发——实用技术篇Swift篇&照片选择

    照片选择 // MARK: - 选择照片 /*----- 选择照片 ------*/ @IBAction func addImageButtonClick() { let actionSheet = ...

  7. ios开发——实用技术篇Swift篇&系统声音

    系统声音 // MARK: - 系统声音 /*----- 系统声音 ------*/ @IBAction func systemSound() { //建立的SystemSoundID对象 var s ...

  8. ios开发——实用技术篇Swift篇&视频

    视频 // MARK: - 播放视频 /*----- 播放视频 ------*/ func moviePlayerPreloadFinish(notification:NSNotification) ...

  9. ios开发——实用技术篇Swift篇&录音

    录音 // MARK: - 录音 /*----- 录音 ------*/ var recorder:AVAudioRecorder? //录音器 var player:AVAudioPlayer? / ...

随机推荐

  1. bzoj1030

    AC自动机和DP. f[i][j] 表示在匹配到第i位置,处于ac自动机的j节点.决策第(i+1)个字母,计算出转移到第j2节点. f[i+1][j2] += f[i][j]; #include< ...

  2. [LOJ 1027] Dangerous Maze

    A - A Dangerous Maze Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  3. SGU 438 The Glorious Karlutka River =) ★(动态+分层网络流)

    [题意]有一条东西向流淌的河,宽为W,河中有N块石头,每块石头的坐标(Xi, Yi)和最大承受人数Ci已知.现在有M个游客在河的南岸,他们想穿越这条河流,但是每个人每次最远只能跳D米,每跳一次耗时1秒 ...

  4. python auto send email

    /*************************************************************************** * python auto send emai ...

  5. 让Codeigniter控制器支持多级目录

    <?php if (!defined('BASEPATH')) { exit ('No direct script access allowed');} class MY_Router exte ...

  6. Spring Autowire自动装配介绍

    在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象.但是对于一个大型的系统,这个操作将会耗费我们大量的资源,我们不得不花费大量的时间和精力用于创建和维护系统中的< ...

  7. Java [leetcode 27]Remove Element

    题目描述: Given an array and a value, remove all instances of that value in place and return the new len ...

  8. c#保存datagridview中的数据时报错 “动态SQL生成失败。找不到关键信息”

    ilovejinglei 原文 C#中保存datagridview中的数据时报错"动态SQL生成失败.找不到关键信息" 问题描述     相关代码 using System; us ...

  9. android中GridView关于间距的属性值介绍

    android:columnWidth  设置列的宽度.关联的方法为:setColumnWidth(int)  stretchMode属性值的作用是设置GridView中的条目以什么缩放模式去填充空间 ...

  10. UVA 10462 Is There A Second Way Left? 次小生成树

    模板题 #include <iostream> #include <algorithm> #include <cstdio> #include <cstdli ...