3

The c Lib API : mycapi.h

typedef struct{
    int  itype;
    double * dx;
    double * dy;
}MyObjInfo;

typedef MyObjInfo * MyObjHandle;

MyObjHandle MyObjInit(const char *pFile);

add myapi.h as module

my swift code :

import Foundation
import mycapiModule


var h:MyObjHandle = MyObjInit("/home/a")
var o:MyObjInfo = h.memory
////to do somethings
//...
h.destroy()
h = nil
print("\(o.itype)")

So my questions are:

  1. Why am I still able to use o after h.destroy()?
  2. Do I have to destroy h and o?
Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
Enping Wu
  • 31
  • 3
  • I think you need to utilize UnsafeMutablePointer : You can read a about working with C here: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html – Dan Beaulieu Dec 09 '15 at 05:04

1 Answers1

0

You can not access directly.

To access C / Cpp Library, In Swift programming language with XCode use below way.

add main.mm file with

#import <UIKit/UIKit.h>
#import "ProjectName-swift.h"
//#import "AppDelegate.h"

int main(int argc, char * argv[]) {

 @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

Comment - @UIApplicationMain in AppDelegate

Now by using bridge you can access C / Cpp library or directly from Swift programming language.

Hasya
  • 9,792
  • 4
  • 31
  • 46