[英]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.