简体   繁体   English

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

[英]How do I write this objective-c code snippet into Swift?

This is a couple lines of code taken pretty much verbatim from the GBA4iOS controller view that changes the length of a vibration: 这是几行代码,几乎完全从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

My attempt here just refuses to work under all circumstances: 我在这里的尝试只是在所有情况下都不起作用:

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);
}

When left uncommented, it crashes SourceKit, but only in view controller. 如果不加注释,它会使SourceKit崩溃,但只会在视图控制器中崩溃。 When I put it in its own file: 当我将其放在自己的文件中时:

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

I've tried everything I could, if you could help it'd be greatly appreciated 我已经尽力了,如果可以的话,不胜感激

Remove the set of parenthesis after your "AudioServicesPlaySystemSoundWithVibration" declaration. 在“ AudioServicesPlaySystemSoundWithVibration”声明之后,删除括号集。

From: 从:

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

To: 至:

func AudioServicesPlaySystemSoundWithVibration(Int , NilLiteralConvertible, NSDictionary);

First, you don't need the function declarations. 首先,您不需要函数声明。 (In fact I'm not sure that works at all.) (实际上,我不确定这是否可行。)

This line: 这行:

 var dictionary:NSMutableDictionary;

Does not create a mutable dictionary, you need to write: 不创建可变字典,您需要编写:

var dictionary = NSMutableDictionary()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将Objective-C中的此代码片段转换为Swift - How to translate this code snippet in Objective-C to Swift 如何将AppDelegate Swift代码放入Objective-C AppDelegate文件并使其工作? - How do I put AppDelegate Swift code into Objective-C AppDelegate file and make it work? 如何从Swift单元测试中访问测试目标中存在的Objective-C代码? - How do I access Objective-C code that exists in the test target, from a Swift unit test? 需要将快速的代码片段转换为目标C - Need to convert a swift snippet of code into Objective C 如何用Swift语法编写这个Objective-C块? - How to write this objective-c block in swift syntax? 像objective-C一样,如何在swift中编写“valueForKey”? - Like objective-C , how to write “valueForKey” in swift? 如何使用 swift Viewcontroller 通过编写代码打开 objective-c Viewcontrollerv 页面? - How can I use swift Viewcontroller through write coding to open the objective-c Viewcontrollerv page? Objective-C桥接问题。 如何在Swift项目中使用Obj-C框架? - Objective-C bridging issue. How do I use an Obj-C framework in a Swift project? 我在iOS上有一个Objective-C应用程序,如何构建SwiftWatch扩展程序? - I have an Objective-C application on iOS, how do I build a Swift Watch extension? 如何创建一个链接到Objective-C代码的Swift框架? - How to create a Swift framework which links to Objective-C code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM