转换为swift(id)initWithPreset:(int [])预设

时间:2016-02-12 04:30:41

标签: iphone swift

你可以转换 - (id)initWithPreset:(int [])预设; 在斯威夫特

int *type = nil;
    if (_selectedIndex == 0) {
        type = PRESET_PHONE;
    } 

extern int PRESET_FM[];
extern int PRESET_CD[];
extern int PRESET_STUDIO[];
extern int PRESET_VOICE[];
extern int PRESET_PHONE[];
extern int PRESET_TAPE[];
extern int PRESET_HIFI[];

在目标c

  MP3Converter *mp3Converter = [[MP3Converter alloc] initWithPreset:type];

但我在Swift中使用

var mp3Converter : MP3Converter!
            mp3Converter = MP3Converter(preset:PRESET_VOICE )

我们如何给出预设:PRESET_VOICE?。它给出错误Unsafe Immutable Pointer ..

1 个答案:

答案 0 :(得分:0)

问题似乎是你定义常量数组的方式。

您需要将它们定义为整数指针而不是数组。检查:

//const int  PRESET_FM[] = {1, 2, 4, 5};  //<< This won't get exported to swift
const int * PRESET_FM = {1, 2, 4, 5}; //Swift can see this fine

在你的swift代码中,你需要将这个C常量正确地转换为UnsafeMutablePointer,因为Swift会看到C指针。

var mp3Converter : mp3Converter = MP3Converter(preset:UnsafeMutablePointer<Int32>( PRESET_FM)(PRESET_VOICE))

还要确保目标C接口使用int *而不是int []:

- (id)initWithPreset:(int *)preset  

HTH。