Swift -- 错误总结

1、可选类型为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)”)
}
 
2、Swift3.0把闭包的入参参数名去掉了 
 
Function types cannot have argument labels; use '_' before 'respose'
 
 
3、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).
 
 
 
4、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?) {
}

 

5、 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里就好了
相关文章
相关标签/搜索