如何在swift调整c++代码呢?

swift通过工程的桥接文件,调用oc的代码,间接调用c++代码!!!

1.创建C++ Person类文件:Person.hppPerson.cpp

Person.hpp内容如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

//
//  Person.hpp
//  SwiftCallC
//
//  Created by ksnowlv on 2019/3/28.
//  Copyright © 2019 ksnowlv. All rights reserved.
//

#ifndef Person_hpp
#define Person_hpp

#include <stdio.h>

class Person {
public:
    Person();
    ~Person();
    
    void setName(const char* pName);
    const char* name();
    
    void setAge(const int age);
    const int age() const;
    
private:
    char *m_pName;
    int m_age;
};


#endif /* Person_hpp */

Person.cpp内容如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

//
//  Person.cpp
//  SwiftCallC
//
//  Created by ksnowlv on 2019/3/28.
//  Copyright © 2019 ksnowlv. All rights reserved.
//

#include "Person.hpp"
#include <string.h>


Person::Person() {
    m_pName = nullptr;
    m_age = 0;
}

Person::~Person() {
    
    if (m_pName) {
        delete [] m_pName;
        m_pName = nullptr;
    }
}

void Person::setAge(const int age) {
    m_age = age;
}

const int Person::age()const {
    return m_age;
}

void Person::setName(const char *pName) {
    if (m_pName) {
        delete [] m_pName;
        m_pName = nullptr;
    }
    
    if (pName) {
        const size_t len = strlen(pName) + 1;
        m_pName = new char[len];
        memset(m_pName, 0, len);
        strcpy(m_pName, pName);
    }
}

const char* Person::name() {
    return m_pName;
}

2.创建oc类文件:PersonExt.hPersonExt.mm

PersonExt.h内容如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//
//  PersonExt.h
//  SwiftCallC
//
//  Created by ksnowlv on 2019/4/1.
//  Copyright © 2019 ksnowlv. All rights reserved.
//

#import <Foundation/Foundation.h>


NS_ASSUME_NONNULL_BEGIN

@interface PersonExt : NSObject 


@property(nonatomic, strong) NSString *name;
@property(nonatomic, assign) NSInteger age;


@end

NS_ASSUME_NONNULL_END

PersonExt.mm内容如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

//
//  PersonExt.m
//  SwiftCallC
//
//  Created by ksnowlv on 2019/4/1.
//  Copyright © 2019 ksnowlv. All rights reserved.
//

#import "PersonExt.h"
#include "Person.hpp"

@interface PersonExt () {
    Person *_person;
}


@end

@implementation PersonExt

- (id)init {
    self = [super init];
    
    if (self) {
        _person = new Person();
    }
    
    return self;
}

- (void)dealloc {
    
    if (_person) {
        delete _person;
        _person = nil;
    }
}

- (void)setName:(NSString *)name {
    if (_person) {
        _person->setName([name UTF8String]);
    }
}

- (NSString *)name {
    if (_person) {
        const char *name = _person->name();
        
        if (name) {
            return  [NSString stringWithUTF8String:name];
        }
    }
    
    return nil;
}

- (void)setAge:(NSInteger)age {
    
    if (_person) {
        _person->setAge((const int)age);
    }
}

- (NSInteger)age {
    if (_person) {
        return  _person->age();
    }
    
    return 0;
}

@end

3.在桥接文件中,加入PersonExt.h引用:#include "PersonExt.h"

4.swift中调用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10

    let personExt = PersonExt()
    var age = personExt.age
    print("age =",age)
    personExt.age = 20
    age = personExt.age
    print("age =",age)
        
    personExt.name = "ksnowlv"
    print("name = ",personExt.name) 

结果显而易见:

1
2
3
age = 0
age = 20
name =  ksnowlv