如何将这个Objective-c代码片段写入Swift?

时间:2015-03-08 21:44:12

标签: ios objective-c swift uikit audiotoolbox

这是从GBA4iOS控制器视图中逐字逐句改变振动长度的几行代码:

void AudioServicesStopSystemSound(int);
void AudioServicesPlaySystemSoundWithVibration(int, id, NSDictionary *);

@implementation vibeObject;

- (void)vibrate
{
    AudioServicesStopSystemSound(kSystemSoundID_Vibrate);

    int64_t vibrationLength = 30;

    NSArray *pattern = @[@NO, @0, @YES, @(vibrationLength)];

    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    dictionary[@"VibePattern"] = pattern;
    dictionary[@"Intensity"] = @1;

    AudioServicesPlaySystemSoundWithVibration(kSystemSoundID_Vibrate, nil, dictionary);
}

@end

我在这里的尝试只是拒绝在所有情况下工作:

func AudioServicesStopSystemSound(Int);
func AudioServicesPlaySystemSoundWithVibration(Int , NilLiteralConvertible, NSDictionary)();

func vibrate(){

    AudioServicesStopSystemSound(kSystemSoundID_Vibrate);

    let vibrationLength = 30;

    let pattern: NSArray = [false, 0, true, vibrationLength];

    var dictionary:NSMutableDictionary;
    dictionary["VibePattern"] = pattern;
    dictionary["Intensity"] = 1;

    AudioServicesPlaySystemSoundWithVibration(kSystemSoundID_Vibrate, nil, dictionary);
}

当取消注释时,它会崩溃SourceKit,但只会在视图控制器中崩溃。当我把它放在自己的文件中时:

vibeObject.vibrate(<# RIGHT HERE IT TELLS ME "EXPECTED ',' SEPARATOR vibeObject#>);

我已尽力而为,如果你能提供帮助,我将非常感激

2 个答案:

答案 0 :(得分:0)

在&#34; AudioServicesPlaySystemSoundWithVibration&#34;之后删除一组括号。声明。

自:

func AudioServicesPlaySystemSoundWithVibration(Int , NilLiteralConvertible, NSDictionary)();

要:

func AudioServicesPlaySystemSoundWithVibration(Int , NilLiteralConvertible, NSDictionary);

答案 1 :(得分:0)

首先,您不需要函数声明。 (事实上​​,我并不确定它是否有效。)

这一行:

 var dictionary:NSMutableDictionary;

不创建可变字典,需要写:

var dictionary = NSMutableDictionary()