我知道在非引用类型数据上使用weak
时会出现错误。但是,我在一个由我自己创建的类中使用它。只是想知道有人有同样的问题。
internal static func registeruUser(email email: String, username: String, password: String, completion: (user: TSUser?, error: NSError?) -> Void) {
let baseURL = NSLocalizedString("sign_up", comment: "")
let headers = ["Accept": "application/json",
"Content-Type": "application/json"]
let parameters = ["email": email, "name": username, "password": password]
Alamofire.request(.POST, baseURL, headers: headers, parameters: parameters, encoding: .JSON).responseJSON {
[weak self] response in // compiler error
if let weakSelf = self {
switch response.result {
case .Success:
if let value = response.result.value {
let json = JSON(value)
if let jsonUser = json["user"].dictionaryObject,
let user = Mapper<TSUser>().map(jsonUser) as TSUser? {
currentUser = user
weakSelf.storeUserToCache() // compiler error
completion(user: user, error: nil)
}
else {
completion(user: nil, error: NSError(domain: "ResponseError:Something wrong with data from [TSUser]registeruUser", code: -1, userInfo: nil))
}
}
case .Failure(let error):
completion(user: nil, error: error)
}
}
}
}
这是我的班级声明:
@objc class TSUser: NSObject, Mappable, NSCoding
我打算致电storeUserToCache()
,但又出现了另一个错误:Missing argument for parameter #1 in call
以下是func
:
private func storeUserToCache() {
if let currentUser = TSUser.currentUser {
NSKeyedArchiver.archiveRootObject(currentUser, toFile: self.getUserPath())
}
}
任何人都有任何想法通过Swift编译器?
答案 0 :(得分:0)
static
,所以self
没有任何意义。低级错误。问题解决了。