• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Swift--错误总结

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

一、可选类型为nil,对可选类型强制解包crash
 
var response:String?
let dict = response!  // 强制解包导致 crash
fatal error: unexpectedly found nil while unwrapping an Optional value


// 可以使用下面的解包方式
if let dict = response {
    print("----->\(dict)”)
}
 
二、Swift3.0把闭包的入参参数名去掉了 
 
Function types cannot have argument labels; use '_' before 'respose'
 
 
三、Could not cast value of type 'UIView' (0x1b57b3ed8) to ‘XXX’
 
XXX 是自己定义的类名,出现这种问题  先检查代码,xib和代码的类是否一样,如果id什么都是一样的,那么,直接将xib删掉,重新添加就行了!
 
也有可能是自己代码问题,像下面这种将AddItemsTopView 改为UIView就行了 
Could not cast value of type 'UIView' (0x1b57b3ed8) to 'ROMWESWIFT.AddItemsTopView' (0x100030ac8).
 
 
 
四、Argument of '#selector' refers to instance method 'btnClick(sender:)' that is not exposed to Objective-C
 
btn.addTarget(self, action:#selector(btnClick(sender:)), for: .touchUpInside)   报错 Argument of '#selector' refers to instance method 'btnClick(sender:)' that is not exposed to Objective-C
func btnClick(sender:UIButton?) {  
}
Selectors are a feature of Objective-C and can only be used with methods that are exposed to the dynamic Obj-C runtime. You can't have a selector to a pure Swift method.
 
If your class inherits from NSObject then its public methods are exposed to Obj-C automatically. Since your class does not inherit from NSObject you have to use the @objc attribute to indicate that you want this method exposed to Obj-C so that it may be called with an Obj-C selector.
 
#selector() is the new syntax in Swift 2.2. It allows the compiler to check that the selector you're trying to use actually exists. The old syntax is deprecated and will be removed in Swift 3.0.
 
加上 @objc 就好了
@objc func btnClick(sender:UIButton?) {
}

 

五、nw_endpoint_flow_service_writes [2.1 192.168.10.129:8888 ready socket-flow (satisfied)] Write request has 4294967295 frame count, 0 byte count
 
使用 Alamofire 的GET请求,然后参数放到parars里面会导致这个问题,把参数直接放到url里就行了

 

 
 
 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[Swift]LeetCode529.扫雷游戏|Minesweeper发布时间:2022-07-13
下一篇:
swift-基于TCL的自动化测试框架发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap