如何将这个 objective-c 代码片段编写到 Swift 中



这是从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#>);

我已经尽力了,如果你能帮忙,将不胜感激

删除"AudioServicesPlaySystemSoundWithVibration"声明后面的括号集。

从:

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

自:

func AudioServicesPlaySystemSoundWithVibration(Int , NilLiteralConvertible, NSDictionary);

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

这一行:

 var dictionary:NSMutableDictionary;

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

var dictionary = NSMutableDictionary()

最新更新