我是ObjectiveC和Swift的初學者(但必須爲Cordova應用程序開發iOS共享擴展)。無法將目標C代碼片段轉換爲Swift
我想在我的份額擴展
NSURL *destinationURL = [NSURL URLWithString:@"myapp://"];
// Get "UIApplication" class name through ASCII Character codes.
NSString *className = [[NSString alloc] initWithData:[NSData dataWithBytes:(unsigned char []){0x55, 0x49, 0x41, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E} length:13] encoding:NSASCIIStringEncoding];
if (NSClassFromString(className)) {
id object = [NSClassFromString(className) performSelector:@selector(sharedApplication)];
[object performSelector:@selector(openURL:) withObject:destinationURL];
}
實現這個code snippet現在我有以下的,但我真的不知道如何將「performSelector」的一部分,因爲它似乎它不是翻譯在Swift中。
let bytesArray : [UInt8] = [0x55, 0x49, 0x41, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E]
let classNameNs = NSString.init(data: NSData(bytes: bytesArray, length: bytesArray.count), encoding: NSASCIIStringEncoding) ?? ""
let className = classNameNs as String
NSClassFromString(className).map { clazz in
let result = clazz.performSelector(Selector("sharedApplication"))
}
有人可以幫我完成這部分嗎?謝謝
爲什麼不使用[此解決方案(http://stackoverflow.com/a/28037297/1226963)? – rmaddy
@rmaddy爲什麼它會更好? –
我建議它,因爲它已經有工作的Swift代碼。 – rmaddy