如何将这个 Objective-C 代码片段写入 Swift?
Posted
技术标签:
【中文标题】如何将这个 Objective-C 代码片段写入 Swift?【英文标题】:How do I write this objective-c code snippet into 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#>);
我已经尽力了,如果你能提供帮助将不胜感激
【问题讨论】:
【参考方案1】:删除“AudioServicesPlaySystemSoundWithVibration”声明后的括号集。
发件人:
func AudioServicesPlaySystemSoundWithVibration(Int , NilLiteralConvertible, NSDictionary)();
收件人:
func AudioServicesPlaySystemSoundWithVibration(Int , NilLiteralConvertible, NSDictionary);
【讨论】:
【参考方案2】:首先,您不需要函数声明。 (事实上,我不确定这是否有效。)
这一行:
var dictionary:NSMutableDictionary;
不创建可变字典,需要写:
var dictionary = NSMutableDictionary()
【讨论】:
以上是关于如何将这个 Objective-C 代码片段写入 Swift?的主要内容,如果未能解决你的问题,请参考以下文章
从 Android Studio 中的片段将数据写入 Firebase
将 NSData Objective-C 代码转换为 Swift 时遇到问题