简而言之,在使用Xcode 9 Beta时,我遇到了以下警告:
不推荐在Swift 4模式下使用Swift 3 @objc推理.请解决已弃用的@objc推理警告,使用"使用已弃用的Swift 3 @objc推理"日志记录测试代码,并禁用Swift 3 @objc推理.**
经过一番研究,我仍然不知道如何解决这个问题.我将非常感谢有关如何解决此问题的任何提示以及对正在发生的事情的解释.
我的目标是更好地理解我的代码发生了什么.
在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
})
Run Code Online (Sandbox Code Playgroud)
但这似乎不再在Swift 3(或4)中编译.在Swift 3中使用新的Dispatch API编写它的首选方法是什么?
我有以下用Swift 3编写的简单代码:
let str = "Hello, playground"
let index = str.index(of: ",")!
let newStr = str.substring(to: index)
Run Code Online (Sandbox Code Playgroud)
从Xcode 9 beta 5,我得到以下警告:
'
substring(to:)'已被弃用:请使用String带有'partial range from'运算符的切片下标.
如何在Swift 4中使用具有部分范围的切片下标?
我正在尝试将我的项目源代码从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标记,我假设没有必要.
如何重写选择器来处理弃用?
相关问题:
此代码在ios10中正常工作.我得到我的标签和图像按钮,这是用户照片配置文件,圆形圆形..好的.但是当运行xcode 9 ios11模拟器时,我将它拉出来.按钮框必须是32x32,当检查SIM并获取视图并告诉xcode描述视图时,我得到170x32或类似的输出.
继承我的代码.
let labelbutton = UIButton( type: .system)
labelbutton.addTarget(self, action:#selector(self.toLogin(_:)), for: .touchUpInside)
labelbutton.setTitleColor(UIColor.white, for: .normal)
labelbutton.contentHorizontalAlignment = .right
labelbutton.titleLabel?.font = UIFont.systemFont(ofSize: 18.00)
let button = UIButton(type: .custom)
button.addTarget(self, action:#selector(self.toLogin(_:)), for: .touchUpInside)
button.frame = CGRect(x: 0, y: 0, width: 32, height: 32)
button.setTitleColor(UIColor.white, for: .normal)
button.setTitleColor(UIColor.white, for: .highlighted)
var buttomItem : UIBarButtonItem = UIBarButtonItem()
buttomItem.customView = button
buttomItem.target = self
buttomItem.action = "ToLogin"
var labelItem : UIBarButtonItem = UIBarButtonItem()
labelItem.customView = labelbutton
labelItem.target = self
labelItem.action = "ToLogin"
if …Run Code Online (Sandbox Code Playgroud) 我有一个实现Swift 4的结构Codable.是否有一种简单的内置方法将该结构编码为字典?
let struct = Foo(a: 1, b: 2)
let dict = something(struct)
// now dict is ["a": 1, "b": 2]
Run Code Online (Sandbox Code Playgroud) Swift 4添加了新Codable协议.当我使用JSONDecoder它似乎要求我的Codable类的所有非可选属性在JSON中有键或它会引发错误.
使我的类的每个属性都是可选的似乎是一个不必要的麻烦,因为我真正想要的是使用json中的值或默认值.(我不希望这个属性为零.)
有没有办法做到这一点?
class MyCodable: Codable {
var name: String = "Default Appleseed"
}
func load(input: String) {
do {
if let data = input.data(using: .utf8) {
let result = try JSONDecoder().decode(MyCodable.self, from: data)
print("name: \(result.name)")
}
} catch {
print("error: \(error)")
// `Error message: "Key not found when expecting non-optional type
// String for coding key \"name\""`
}
}
let goodInput = "{\"name\": \"Jonny Appleseed\" }"
let badInput = "{}"
load(input: goodInput) …Run Code Online (Sandbox Code Playgroud) 下午好,
在更新到最新版本的Xcode(版本10.0)之后,项目无法构建,因为它发现了一些关于"Command CompileSwift失败并返回非零退出代码"错误的错误.
我该如何解决这个错误? 它们出现在我在项目中使用的大多数Pod(我使用CocoaPods)中.
我已经尝试将pod版本和pod更新到可用的最新版本,但问题仍然存在.
我在网上搜索了很多,关于这个问题的信息非常少.
问候.
在使用Swift4和Codable协议时,我遇到了以下问题 - 看起来没有办法允许JSONDecoder跳过数组中的元素.例如,我有以下JSON:
[
{
"name": "Banana",
"points": 200,
"description": "A banana grown in Ecuador."
},
{
"name": "Orange"
}
]
Run Code Online (Sandbox Code Playgroud)
还有一个Codable结构:
struct GroceryProduct: Codable {
var name: String
var points: Int
var description: String?
}
Run Code Online (Sandbox Code Playgroud)
解码这个json时
let decoder = JSONDecoder()
let products = try decoder.decode([GroceryProduct].self, from: json)
Run Code Online (Sandbox Code Playgroud)
结果products是空的.这是可以预料到的,因为JSON中的第二个对象没有"points"键,而struct中points不是可选的GroceryProduct.
问题是如何允许JSONDecoder"跳过"无效对象?