可以在iOS 7和iOS 8上运行的警报

我得到的dyld:符号未find:_OBJC_CLASS _ $ _ UIAlertAction当我试图让这个怪物运行。

如何弱链接8.0的东西?

var device : UIDevice = UIDevice.currentDevice()!; var systemVersion = device.systemVersion; var iosVerion : Float = systemVersion.bridgeToObjectiveC().floatValue; if(iosVerion < 8.0) { let alert = UIAlertView() alert.title = "Noop" alert.message = "Nothing to verify" alert.addButtonWithTitle("Click") alert.show() } else { var alert : UIAlertController? = UIAlertController(title: "Noop", message: "Nothing to verify", preferredStyle: UIAlertControllerStyle.Alert) if alert { let actionStyle : UIAlertActionStyle? = UIAlertActionStyle.Default; var alertAction : UIAlertAction? = UIAlertAction(title: "Click", style: actionStyle!, handler: nil) if(alertAction) { alert!.addAction(alertAction) self.presentViewController(alert, animated: true, completion: nil) } } } return; 

解决:UIKit必须被标记为可选,而不是必需的。 现在简化版本是:

 var device : UIDevice = UIDevice.currentDevice()!; var systemVersion = device.systemVersion; var iosVerion : Float = systemVersion.bridgeToObjectiveC().floatValue; if(iosVerion < 8.0) { let alert = UIAlertView() alert.title = "Noop" alert.message = "Nothing to verify" alert.addButtonWithTitle("Click") alert.show() } else { var alert : UIAlertController = UIAlertController(title: "Noop", message: "Nothing to verify", preferredStyle: UIAlertControllerStyle.Alert) alert.addAction(UIAlertAction(title: "Click", style:.Default, handler: nil)) self.presentViewController(alert, animated: true, completion: nil) } 

  • 在项目构build阶段的“Link Binary With Libraries”部分显式添加UIKit。

  • 你可以像这样testingUIAlertController的存在性:

     if NSClassFromString("UIAlertController") != nil { // Use it } else { // Fall back } 
  • 我写了一个适用于iOS 7和iOS 8的包装器。你可以在这里find它 。 它需要一个视图控制器后跟一堆可选的参数和任意数量的button:

     showAlert(self, style: .ActionSheet, sourceView: cell, completion: { tableView.deselectRowAtIndexPath(indexPath, animated: true) }, (.Default, "Send clipboard", { if someCondition { // completion must be specified because of a Swift bug (rdar://18041904) showAlert(self, title: "Nothing to send", message: "The clipboard is empty.", completion: nil, (.Cancel, "OK", nil) ) } }), (.Cancel, "Cancel", nil) ) 

在Xcode 6 beta 5中,由于Swift隐式地链接了框架,因此在Link Phases – > Link Binary With Libraries中没有任何东西。 在这种情况下,您需要手动添加它并标记可选。

而不是明确检查系统版本,你可以检查UIAlertController的可用性

 if nil != NSClassFromString("UIAlertController") { //show alertcontroller ... } else { //show alertview ... } 

尝试下面的Objective C的代码。它适用于iOS 8和以下版本。

 if (IS_OS_8_OR_LATER) { UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]; [alertVC addAction:cancelAction]; [[[[[UIApplication sharedApplication] windows] objectAtIndex:0] rootViewController] presentViewController:alertVC animated:YES completion:^{ }]; } else{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; [alert show]; }