无法修改函数体内的函数参数

Unable to modify function parameters within function body

我在 swift 项目中有一个方法定义:

class func fireGetRequest(urlString: String!, username: String?, password: String?, completionBlock:(NSDictionary)->Void) {
    //check if user passed nil userName
    if username == nil || password == nil {
        // retrieve userName, password from keychain
        // here we have OR check since we have single value from pair it is of no use and can be considered as corrupted data
        // so it is better to retrieve stable data from keychain for further processing
        let (dataDict, error) = Locksmith.loadDataForUserAccount(kKeychainUserAccountName)

        // no error found :)
        // use data retrieved
        if error == nil {
            username = dataDict[kKeychainUserNameKey]
            password = dataDict[kKeychainUserPwdKey]
        }
    }

    // do something with username, password
}

这里我正在做以下事情:

  1. 检查用户名、密码是否为零
  2. 如果其中任何一个为零,则尝试从字典中设置相应的值

问题是 - 我无法解决以下错误:

objective-c 中的类似方法非常有效:

+ (void)fireGetRequestWithUrlString:(NSString *)urlString userName:(NSString *)username userPwd:(NSString *)password{
    NSDictionary *dataDict = @{kKeychainUserNameKey: @"Some user name", kKeychainUserPwdKey: @"Some password"};

    if (!username || !password) {
        username = dataDict[kKeychainUserNameKey];
        password = dataDict[kKeychainUserPwdKey];
    }

    // do something with username, password
}

有什么想法吗?

更新 1:

按照答案中的建议,我将函数参数 - 用户名和密码声明为 var,但随后我开始收到这些编译错误:

为了解决这个问题,我进行了类型转换,但又出现了另一个错误:

强制向下转型,出现以下错误:

还是一头雾水:(

终于解决了:)

if let dataDictionary = dataDict {
                    username = dataDictionary[kKeychainUserNameKey] as! String?
                    password = dataDictionary[kKeychainUserPwdKey] as! String?
                }

您应该将用户名和密码声明为var。默认为 let.

class func fireGetRequest(urlString: String!, var username: String?, var password: String?, completionBlock:(NSDictionary)->Void) {
    // ...
}

另请参阅:The Swift Programming Language: Functions

Constant and Variable Parameters

Function parameters are constants by default. Trying to change the value of a function parameter from within the body of that function results in a compile-time error. This means that you can’t change the value of a parameter by mistake.

However, sometimes it is useful for a function to have a variable copy of a parameter’s value to work with. You can avoid defining a new variable yourself within the function by specifying one or more parameters as variable parameters instead. Variable parameters are available as variables rather than as constants, and give a new modifiable copy of the parameter’s value for your function to work with.

函数参数默认为常量。

将可变参数显式定义为 var

Swift 2

class func fireGetRequest(urlString: String!, var username: String?, var password: String?, completionBlock:(NSDictionary)->Void) 

Swift 3

class func fireGetRequest(urlString: String!, username: String?, password: String?, completionBlock:(NSDictionary)->Void){
   var username = username
   var password = password
   //Other code goes here
}