我试图添加一个UITapGesture到一个UIButton,所以它会触发一个function,当点击。 我正在使用Swift 3并出现一些错误: 终止应用程序由于未捕获的exception'NSInvalidArgumentException',原因:' – [SwiftRunner.ViewController tapBlurButton]:无法识别的select发送到实例0x149e07610' 这大致是我所拥有的: // Swift 3 import UIKit class ViewController { @IBOutlet weak var qsBlurButton: UIButton! override func viewDidLoad() { super.viewDidLoad() let tapGesture = UITapGestureRecognizer(target: self, action: Selector(("tapBlurButton"))) qsBlurButton.addGestureRecognizer(tapGesture) } func tapBlurButton(sender: UITapGestureRecognizer) { print("Please Help!") } }
显然这是现在可以与ios10: optional func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) 这个答案基本上说,需要做的工具: 当应用程序打开并在前台显示股票iOS通知横幅? 我只是不太了解如何把它放在一起。 我不知道这是多么重要,但我不能保持可选的function和Xcode要我切换到私人。 我试图展示徽章和文档提供 static var badge: UNNotificationPresentationOptions { get } 有一点在这里丢失 然后我假设,如果我想排除某些视图控制器获得这些徽章,我不使用导航控制器,我发现这个代码将工作? :var window:UIWindow? if let viewControllers = window?.rootViewController?.childViewControllers { for viewController in viewControllers { if viewController.isKindOfClass(MyViewControllerClass) { print("Found it!!!") } } }
我在我的swift ios应用程序中实现socket.io 。 目前在几个面板上我正在监听服务器,并等待传入的消息。 我通过调用每个面板中的getChatMessage函数: func getChatMessage(){ SocketIOManager.sharedInstance.getChatMessage { (messageInfo) -> Void in dispatch_async(dispatch_get_main_queue(), { () -> Void in //do sth depending on which panel user is }) } } 但是我注意到这是一个错误的方法,我需要改变它 – 现在我想要开始只听一次收到的消息,当有消息传来时,把这个消息传给任何监听它的面板。 所以我想通过NSNotificationCenter传入消息。 到目前为止,我能够传递发生的事情的信息,但不传递数据本身。 我是这样做的: NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.showSpinningWheel(_:)), name: showSpinner, object: nil) 那么我有一个函数叫做: func showSpinningWheel(notification: NSNotification) { } 任何时候我想打电话给我,我正在做: NSNotificationCenter.defaultCenter().postNotificationName(hideSpinner, object: self) 那么如何传递对象messageInfo并将其包含在被调用的函数中呢?
在我的项目中,在转换为swift 3之后,在我的ViewController类之前出现了一个新的函数: fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool { switch (lhs, rhs) { case let (l?, r?): return l < r case (nil, _?): return true default: return false } } 这个function是做什么的? 为什么我需要它?
好的,所以我在Xcode 8中发现了新的Swifty Dispatch API 。我使用DispatchQueue.main.async获得了乐趣,并且我一直在Xcode的Dispatch模块中查找所有新的API。 但我也使用dispatch_once来确保像单身人士创build和一次性设置不会执行多次(即使在multithreading环境中)… dispatch_once无处可查找新的调度模块? static var token: dispatch_once_t = 0 func whatDoYouHear() { print("All of this has happened before, and all of it will happen again.") dispatch_once(&token) { print("Except this part.") } }
标准库中的ObjectiveC.swift文件在第228行附近包含以下几行代码: extension NSObject : Equatable, Hashable { /// … open var hashValue: Int { return hash } } 在这种情况下, open var意味着什么,或者一般的open关键字是什么?
在Swift 2中,我能够使用dispatch_after来延迟使用大中央调度的操作: var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC))) dispatch_after(dispatchTime, dispatch_get_main_queue(), { // your function here }) 但这似乎不再在Swift 3(或4)中编译。 什么是在Swift 3中使用新的Dispatch API编写的首选方法?
我想连接多个string在迅速3: var a:String? = "a" var b:String? = "b" var c:String? = "c" var d:String? = a! + b! + c! 编译时出现以下错误: error: cannot convert value of type 'String' to specified type 'String?' var d:String? = a! + b! + c! ~~~~~~~~^~~~ 这曾经工作在迅速2.我不知道为什么它不工作了。
从Swift 2.2转换到3.0之后,我的Array扩展名不再编译,因为它包含对全局标准库函数min<T>(T,T) extra argument in call min<T>(T,T)并extra argument in call显示编译器错误的extra argument in call 。 这里有一个简单的方法来重现错误: extension Array { func smallestInt(first: Int, second: Int) -> Int { return min(first, second) // compiler error: "Extra argument in call" } } 当将相同的函数添加到Dictionary的扩展中时,我得到相同的错误,而完全相同的代码在其他types的扩展中编译得很好(例如String或AudioBuffer ): 看看Array和Dictionary的文档,我发现Sequence中有实例方法public func min() -> Element? 和public func min(by areInIncreasingOrder: (Element, Element) throws -> Bool) rethrows […]
如何将下面的函数转换为swift 3 ? 目前得到一个Binary operator '..<' cannot be applied to operands of type 'Int' and 'Self.IndexDistance'错误的Binary operator '..<' cannot be applied to operands of type 'Int' and 'Self.IndexDistance' 。 extension MutableCollection where Index == Int { /// Shuffle the elements of `self` in-place. mutating func shuffleInPlace() { // empty and single-element collections don't shuffle if […]