How to make Objective-c class using std:vector mad

2019-07-10 03:25发布

问题:

If I attempt to include an objective-C class that utilizes std:vector in my project's Swift bridging header, in my class I get the error:

#import <vector>                 Error! 'vector' file not found

The bridging file in question is in my custom Framework. If I don't include the objective-c header in my bridging header, all compiles and works fine, but of course I can't access the class from Swift classes.

How can I use this objective-c class in my Swift classes?

回答1:

Swift only supports bridging to Objective-C. You need to move any CPP code / declarations to the .mm file, such as:

Foo.h

#import <Foundation/Foundation.h>

@interface Foo : NSObject

- (void)bar;

@end

Foo.mm

#import "Foo.h"
#import <vector>

@interface Foo() {
    std::vector<int> _array;
}

@end

@implementation Foo

- (void)bar {
    NSLog(@"in bar");
}

@end

One solution, if you have to consume the C++ classes in other C++ / Objective-C++ code, to create a separate header file for Swift's bridging header, and expose what you need:

Foo.h

#import <Foundation/Foundation.h>
#import <vector>

@interface Foo : NSObject {
    std::vector<int>* _bar;
}

@property (atomic, readonly) std::vector<int>* bar;
@property (readonly) size_t size;

- (void)pushInt:(int)val;
- (int)popInt;

@end

Foo+Swift.h

Include this in your bridging header

#import <Foundation/Foundation.h>
#import <stdint.h>

@interface Foo : NSObject

@property (readonly) size_t size;

- (void)pushInt:(int)val;
- (int)popInt;

@end

Foo.mm

#import "Foo.h"

@implementation Foo

@synthesize bar;

- (instancetype)init {
    if (self = [super init]) {
        _bar = new std::vector<int>();
    }

    return self;
}

- (void)dealloc {
    delete _bar;
}

- (void)pushInt:(int)val {
    _bar->push_back(val);
}

- (int)popInt {
    if (_bar->size() == 0) {
        return -1;
    }

    auto front = _bar->back();
    _bar->pop_back();
    return front;
}

- (size_t)size {
    return _bar->size();
}

@end

main.swift

#import Foundation

let f = Foo()
f.pushInt(5);
f.pushInt(10);

print("size = \(f.size)")
print("\(f.popInt())")
print("\(f.popInt())")
print("size = \(f.size)")