在Swift 3中,要注册通知,我可以执行以下方法:
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.n1(notification:)), name: Notification.Name("123"), object: nil)
func n1(notification: Notification){
print("123")
}
// #selector is more brief
NotificationCenter.default.addObserver(self, selector: #selector(n2), name: Notification.Name("456"), object: nil)
func n2(notification: Notification){
print("456")
}
Run Code Online (Sandbox Code Playgroud)
但是,在Xcode 9.0 beta 2(Swift 4.0)中,当我以这种方式注册通知时,对象方法应该具有前缀@objc,为什么呢?使用通知的最佳做法是什么?
Argument of '#selector' refers to instance method 'n1(notification:)' that is not exposed to Objective-C
//Add '@objc' to expose this instance method to Objective-C
@objc func n1(notification: Notification){
print("123")
}
@objc func n2(notification: Notification){
print("456")
}
Run Code Online (Sandbox Code Playgroud) 我无法从Objective-C ViewController 访问我的Swift Singleton类.
Xcode确实识别我的Swift类,它构建,所以我不认为这是一个桥接头问题.
这是我的Swift Singleton类:
@objc class MySingleton : NSObject {
static let shared = MySingleton()
private override init() {}
}
Run Code Online (Sandbox Code Playgroud)
然后在我的.m文件中导入此标头:
#import "myProject-Swift.h"
Run Code Online (Sandbox Code Playgroud)
以这种方式使用Singleton:
MySingleton *testSingleton = [MySingleton shared];
Run Code Online (Sandbox Code Playgroud)
或者这样:
[MySingleton shared];
Run Code Online (Sandbox Code Playgroud)
它确实识别MySingleton类类型,但我无法访问此类的任何函数或属性.
我错过了什么?所有类似的帖子都没有帮助.
编辑:我做了一个测试功能
func testPrint() {
print("Singleton worked")
}
Run Code Online (Sandbox Code Playgroud)
并在objective-c文件中以这种方式调用它:
[[MySingleton shared] testPrint];
Run Code Online (Sandbox Code Playgroud)
没有已知的选择器'testPrint'实例
以下代码在swift 3.x中完美运行,但在swift 4中运行不正常.
let selector = "managerDidDetectedStation:"
let observer = <UIViewController subclass object>
let station = <Station Object>
if observer.responds(to: Selector(selector) {
observer.perform(Selector(selector), with: station)
}
Run Code Online (Sandbox Code Playgroud)
observer.responds(to: Selector(selector)总是返回false.有人知道swift4中这个api有什么变化吗?
当更新项目代码到swift 4得到add.target方法的一些错误我怎么能修复这个错误?
//swift3
var chatLogController: ChatLogController? {
didSet {
sendButton.addTarget(chatLogController, action: #selector(ChatLogController.handleSend), for: .touchUpInside)
uploadImageView.addGestureRecognizer(UITapGestureRecognizer(target: chatLogController, action: #selector(ChatLogController.handleUploadTap)))
}
}
Run Code Online (Sandbox Code Playgroud) 我认为部分问题是因为Swift 4改变了诸如@objc工作之类的方式。
有很多教程,具有很多不同的值,而且我无法在使用哪个版本的版本之间做出选择,以弄清楚如何使其在该版本中工作。
let delegate = UIApplication.shared.delegate as! AppDelegate
delegate.addObserver(self, forKeyPath: #keyPath(AppDelegate.session), options: [], context: nil)
// Warning: Argument of #keyPath refers to non-'@objc' property 'session'
Run Code Online (Sandbox Code Playgroud)
添加@objc到var声明只是告诉我,无法在Objective-C中引用APISession。这似乎导致要求我将要使用此工具的每个类/变量公开给Obj-C的方法,而且似乎倒退了-据我了解,这是一个较新的功能,这很奇怪苹果不会让它在Swift中原生运行。对我来说,这表明我在某种程度上误解或误用了某些东西。
我有使用Swift 3.2和Objective-C编译与Xcode版本9.0(9A235)的项目.它在Swift 3.2中编译得很好.但是,当我切换到Swift 4.0时,在Swift中声明的方法在Objective-C中不再可见.错误是No visible @interface "UserAPI" declares the selector ...