简而言之,在使用Xcode 9 Beta时,我遇到了以下警告:
不推荐在Swift 4模式下使用Swift 3 @objc推理.请解决已弃用的@objc推理警告,使用"使用已弃用的Swift 3 @objc推理"日志记录测试代码,并禁用Swift 3 @objc推理.**
经过一番研究,我仍然不知道如何解决这个问题.我将非常感谢有关如何解决此问题的任何提示以及对正在发生的事情的解释.
我的目标是更好地理解我的代码发生了什么.
我正在尝试将我的项目源代码从Swift 3转换为Swift 4.一个警告Xcode给我的是我的选择器.
例如,我使用常规选择器将按钮添加到按钮,如下所示:
button.addTarget(self, action: #selector(self.myAction), for: .touchUpInside)
Run Code Online (Sandbox Code Playgroud)
这是它显示的警告:
'#selector'的参数是指'ViewController'中的实例方法'myAction()',它取决于Swift 4中不推荐使用的'@objc'属性推断
添加'@objc'以将此实例方法公开给Objective-C
现在,点击Fix错误消息对我的函数执行此操作:
// before
func myAction() { /* ... */ }
// after
@objc func myAction() { /* ... */ }
Run Code Online (Sandbox Code Playgroud)
我真的不想重命名我的所有函数来包含@objc标记,我假设没有必要.
如何重写选择器来处理弃用?
相关问题:
func getAllPropertyName(_ aClass : AnyClass) -> [String] {
var count = UInt32()
let properties = class_copyPropertyList(aClass, &count)
var propertyNames = [String]()
let intCount = Int(count)
for i in 0 ..< intCount {
let property : objc_property_t = properties![i]!
guard let propertyName = NSString(utf8String: property_getName(property)) as? String else {
debugPrint("Couldn't unwrap property name for \(property)")
break
}
propertyNames.append(propertyName)
}
free(properties)
return propertyNames
Run Code Online (Sandbox Code Playgroud)
这段代码一直工作到 Swift 3.2。但是我使用的是 Swift 4,它给了我一个空的 Array[String]。
我NSFetchedResultsController在我的应用程序中使用带有自定义sectionNameKeyPath(名为“ formattedDate”)的a将表视图中的多个元素组合在一起。
该formattedDate属性是在我的 NSManagedObject 的扩展中定义的一个属性(我使用 Xcode 自动代码进行类定义,这就是我在扩展中添加这个属性的原因)。此属性不在 Core Data 模型中(不是瞬态属性)。
extension ActivityMO {
var formattedDate: String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM yyyy"
return dateFormatter.string(from: endDate as Date!)
}
}
Run Code Online (Sandbox Code Playgroud)
我的应用程序在 iOS10 上运行良好。
在 iOS11 上,我收到以下错误并且应用程序崩溃:
2017-08-10 11:52:12.735277+0200 *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[< ActivityMO 0x60000048f820> valueForUndefinedKey:]: the entity ActivityMO is not key value coding-compliant for the key "formattedDate".'
我究竟做错了什么?iOS11 有什么变化吗?
谢谢,
阿克塞尔
我创建了一个类名用户,如下所示
public class Users: NSObject {
var name: String?
var email: String?
}
Run Code Online (Sandbox Code Playgroud)
和其他类创建用户类的数组
var regUser = [Users]()
Run Code Online (Sandbox Code Playgroud)
和
func fetchUser(){
Database.database().reference().child("users").observe(.childAdded, with: {(DataSnapshot) in
if let dictionary = DataSnapshot.value as? [String: AnyObject]{
let user = Users()
user.setValuesForKeys(dictionary)
self.users.append(user)
DispatchQueue.main.async {
self.tableView.reloadData()
}
print(user.email)
}
}, withCancel: nil)
}
Run Code Online (Sandbox Code Playgroud)
发生以下错误:
"Users 0x60400028fdc0> setValue:forUndefinedKey:]:此类不是密钥值的密钥值编码."