【问题标题】:How do I write this objective-c code snippet into Swift?如何将这个 Objective-C 代码片段写入 Swift?
【发布时间】:2015-03-08 21:44:11
【问题描述】:

这是几行代码,几乎是从 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#>);

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

【问题讨论】:

    标签: ios objective-c swift uikit audiotoolbox


    【解决方案1】:

    删除“AudioServicesPlaySystemSoundWithVibration”声明后的括号集。

    发件人:

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

    收件人:

    func AudioServicesPlaySystemSoundWithVibration(Int , NilLiteralConvertible, NSDictionary);
    

    【讨论】:

      【解决方案2】:

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

      这一行:

       var dictionary:NSMutableDictionary;
      

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

      var dictionary = NSMutableDictionary()
      

      【讨论】:

        猜你喜欢
        • 2014-07-28
        • 1970-01-01
        • 2015-11-30
        • 2018-07-02
        • 1970-01-01
        相关资源
        最近更新 更多