我正在处理外部附件框架,这是我注册notofication的代码.
override func viewDidLoad() {
super.viewDidLoad()
EAAccessoryManager.sharedAccessoryManager().registerForLocalNotifications()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "accessoryDidConnectNotify", name: EAAccessoryDidConnectNotification, object: nil)
}
Run Code Online (Sandbox Code Playgroud)
这是我的方法处理功能......
func accessoryDidConnectNotify(notification: NSNotification){
let alert : UIAlertController = UIAlertController(title: "Alert", message: "MFi Accessory Connected", preferredStyle:UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
}))
self.presentViewController(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
而我的问题是,如果我没有在accessoryDidConnectNotify函数内给出任何参数,当我插入MFi配件时,应用程序可以正常运行警报视图.
(i.e) func accessoryDidConnectNotify(){ // works fine (with no arguments)
}
Run Code Online (Sandbox Code Playgroud)
但我需要在我的accessoryDidConnectNotify函数中使用NSNotification对象来获取附件的名称...但是如果我添加NSNotification对象,则插入MFi附件时应用程序崩溃...
(i.e) func accessoryDidConnectNotify(notification: NSNotification){
} // crashes app (with arguments)
Run Code Online (Sandbox Code Playgroud)
如果有人也遇到了问题......请分享
我想在将来安排一个函数调用.我正在使用Swift.
我想回调一个私有的方法并返回一个Promise(来自PromiseKit)
我见过的所有例子都使用了
NSTimer.scheduledTimerWithTimeInterval(ti: NSTimeInterval, target: AnyObject, selector: Selector, userInfo: AnyObject?, repeats: Bool)
Run Code Online (Sandbox Code Playgroud)
精细.我试过了
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "connect", userInfo: nil, repeats: false)
Run Code Online (Sandbox Code Playgroud)
那失败了No method declared with Objective-C selector 'connect'.
什么是Objective-C在这做什么?
无论如何,建议我@objc在我的方法前添加connect.精细.好吧,我不能,因为显然Method cannot be marked @objc because its result type cannot be represented in Objective-C
如果我想使用Objective-C我不会写Swift ...
还有一个scheduledTimerWithTimeInterval是
NSTimer.scheduledTimerWithTimeInterval(ti: NSTimeInterval, invocation: NSInvocation, repeats: Bool)
Run Code Online (Sandbox Code Playgroud)
但是从我所读到NSInvocation的不是斯威夫特的事情......
所以我最终创建了一个包装器,除了调用connect和返回VoidObjective C可以理解之外什么也没做.它有效,但感觉非常愚蠢.有更好的Swift方式吗?
额外奖励:为什么javascript可以这样做,setTimeout(this.connect, 1)而且Swift没有我可以找到的内置方式?
怎么样的守则
@selector(caseInsensitiveCompare:)
Run Code Online (Sandbox Code Playgroud)
在斯威夫特?我尝试对一个字典数组进行排序
NSSortDescriptor(key: "name", ascending: true, selector: Selector(???))
Run Code Online (Sandbox Code Playgroud)
谢谢
我试过这个,但什么也没发生.选择器有问题吗?
func timer() {
var timer = NSTimer(timeInterval: 2.0, target: self, selector:Selector("function"), userInfo: nil, repeats: false)
}
func function() {
println("it worked")
}
Run Code Online (Sandbox Code Playgroud) iOS API函数UIImageWriteToSavedPhotosAlbum将选择器作为一个参数:
func UIImageWriteToSavedPhotosAlbum(_ image: UIImage,
_ completionTarget: Any?,
_ completionSelector: Selector?,
_ contextInfo: UnsafeMutableRawPointer?)
Run Code Online (Sandbox Code Playgroud)
https://developer.apple.com/documentation/uikit/1619125-uiimagewritetosavedphotosalbum
但是,迅速地,当我调用此函数时,选择器永远不会被识别:
class Base {
func save_image(img:UIImage) {
UIImageWriteToSavedPhotosAlbum(img, self, Selector("image:didFinishSavingWithError:contextInfo:"), nil)
// I also tried this:
// UIImageWriteToSavedPhotosAlbum(img, self, #selector(image(_:didFinishSavingWithError:contextInfo:))
}
@objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
print("Photo Saved Successfully")
}
}
class Child:Base {
}
// This is how I call the save_image function:
let child = Child()
child.save_image()
Run Code Online (Sandbox Code Playgroud)
如您所见,我尝试根据签名和字符串构造选择器,但均无效。我总是在运行时收到此错误:
'XXX.Child' does not implement methodSignatureForSelector: -- …Run Code Online (Sandbox Code Playgroud)