相关疑难解决方法(0)

接收到的通知的方法处理程序崩溃了应用程序.:(

我正在处理外部附件框架,这是我注册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)

如果有人也遇到了问题......请分享

nsnotifications nsnotificationcenter ios swift ios8

5
推荐指数
1
解决办法
282
查看次数

Swift 2.3中#selector update的优势是什么?

只是将方法名称写为字符串,我看不出问题.我只是好奇为什么这更好?

swift

5
推荐指数
3
解决办法
3060
查看次数

Swift - scheduledTimerWithTimeInterval - NSInvocation

我想在将来安排一个函数调用.我正在使用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没有我可以找到的内置方式?

timer swift

5
推荐指数
1
解决办法
3810
查看次数

#selector() 和 Selector() ios Swift 之间有什么不同

这两者之间有什么不同?什么是Selector,什么是#selector

在此处输入图片说明 在此处输入图片说明

ios swift

4
推荐指数
1
解决办法
1638
查看次数

Swift Selector caseInsensitiveCompare

怎么样的守则

@selector(caseInsensitiveCompare:)
Run Code Online (Sandbox Code Playgroud)

在斯威夫特?我尝试对一个字典数组进行排序

 NSSortDescriptor(key: "name", ascending: true, selector: Selector(???))
Run Code Online (Sandbox Code Playgroud)

谢谢

arrays sorting selector swift

2
推荐指数
1
解决办法
2274
查看次数

在Swift中遇到NSTimer问题

我试过这个,但什么也没发生.选择器有问题吗?

    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)

nstimer ios swift

1
推荐指数
1
解决办法
4484
查看次数

无法识别UIImageWriteToSavedPhotosAlbum中的Swift选择器,为什么?

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)

selector ios swift

1
推荐指数
1
解决办法
1509
查看次数

ios无法将类型'()'的值转换为预期的参数类型'String'swift 3

在此输入图像描述

我已将我的代码更新为Swift 3,现在得到上面的错误.我认为这种方式selector有问题.有人请帮帮我,有什么不对.

selector ios swift3

0
推荐指数
2
解决办法
7267
查看次数