简体   繁体   English

Alamofire:“弱”不能应用于非类类型错误

[英]Alamofire: 'weak' cannot be applied to non-class type error

I understand that this will throw error when use weak on non-reference type data. 我知道在对非引用类型的数据使用weak时会抛出错误。 But, I use it on a class that is created by myself. 但是,我在自己创建的类上使用它。 Just wondering anyone having the same issue. 只是想知道有人遇到同样的问题。

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)
            }
        }
    }
}

This is my class declaration: 这是我的课堂宣言:

@objc class TSUser: NSObject, Mappable, NSCoding

I am intended to call storeUserToCache() but got another error: Missing argument for parameter #1 in call 我打算调用storeUserToCache()但出现另一个错误: Missing argument for parameter #1 in call

Here is the func : 这是func

private func storeUserToCache() {
    if let currentUser = TSUser.currentUser {
        NSKeyedArchiver.archiveRootObject(currentUser, toFile: self.getUserPath())
    }
}

Anyone has any ideas to go through the Swift compiler? 任何人都有任何想法可以通过Swift编译器吗?

Paulw11 is correct. Paulw11是正确的。 Because I am using static , that's why self does not make any sense. 因为我使用的是static ,这就是为什么self没有任何意义的原因。 Low level mistake. 低级错误。 Problem solved. 问题解决了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “弱”不能应用于非类类型“ &lt;&lt;错误类型&gt;&gt;” - 'weak' cannot be applied to non-class type '<< error type>>' xcode 7类型参数不能应用于非参数化类 - xcode 7 Type arguments cannot be applied to non-parameterized class swift:弱不能应用于插座 - swift: weak cannot be applied to an outlet 如何修复Alamofire 5错误:“无法专用于非通用类型&#39;DataResponseSerializer&#39;”? - How to fix Alamofire 5 Error: “Cannot specialize non-generic type 'DataResponseSerializer'”? 要求非等级成员 - Request for member which is of non-class Alamofire 4:无法调用非函数类型“ HTTPURLResponse?”的值 - Alamofire 4: Cannot call value of non-function type 'HTTPURLResponse?' Alamofire:错误:无法将类型为“结果&lt;_,_&gt;”的值分配为类型为“结果” - Alamofire: ERROR: Cannot assign value of type 'Result<_,_>' to type 'Result' 在针对 iOS 8.4 SDK 使用 Crashlytics 3.3.4 构建时,类型参数不能应用于非参数化类“NSDictionary” - Type arguments cannot be applied to non-parameterized class 'NSDictionary' when using Crashlytics 3.3.4 building against iOS 8.4 SDK Alamofire上下文类型的关闭错误 - Alamofire Contextual type for closure Error Alamofire 的 Codables 错误“无法将 '[String: Any]' 类型的值转换为预期的参数类型 'Data'” - Codables error with Alamofire “Cannot convert value of type '[String : Any]' to expected argument type 'Data'”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM