【问题标题】:Trouble with scope and arrays SWIFT范围和数组 SWIFT 出现问题
【发布时间】:2015-03-09 17:42:07
【问题描述】:

您好,我正在使用 Parse 开发一个应用程序,当用户创建一个帐户时,会有一个 UIPickerView 具有不同的字符串,用户必须从中进行选择。我试图将他们的选择保存在一个字符串中。 我已经可以使用它了,但这会保存一个数字而不是字符串。

let myRank = picker.selectedRowInComponent(0)

当我尝试时

let myRank = pickerArray[row]

我收到一条错误消息,提示“使用未解析的标识符“行”。 我猜这是因为范围,但我不知道如何解决它!

这是我的代码:

let pickerArray: NSArray = ["- Välj din kompetens -", "Assistent", "Instruktör", "Biträdande Kurschef", "Kurschef"]

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerArray.count
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    return "\(pickerArray[row])"
}

func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
    // selected value in Uipickerview in Swift
    let rank = pickerArray[row]

}

@IBAction func registerButtonPressed(sender: UIButton) {

    let myRank = pickerArray[row]

    var user = PFUser()
    user.username = email
    user.password = password
    user.email = email
    user[PF_USER_RANK] = myRank
    user[PF_USER_EMAILCOPY] = email
    user[PF_USER_FULLNAME] = name
    user.signUpInBackgroundWithBlock { (succeeded: Bool, error: NSError!) -> Void in
        if error == nil {
            PushNotication.parsePushUserAssign()
            ProgressHUD.showSuccess("Succeeded.")
            self.dismissViewControllerAnimated(true, completion: nil)
        } else {
            if let userInfo = error.userInfo {
                ProgressHUD.showError(userInfo["error"] as String)
            }
        }
    }
}

【问题讨论】:

    标签: ios xcode swift uipickerview uipickerviewcontroller


    【解决方案1】:

    除非您将 row 存储为实例变量,否则您将无法访问它。

    不过,我认为这是没有必要的情况。你已经成功了一半。您已经了解了如何从选择器中检索选定的行索引,并且您已经知道如何根据索引从 pickerArray 中选择特定字符串。只需将两者结合起来:

    let myRank = pickerArray[picker.selectedRowInComponent(0)]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 1970-01-01
      • 2016-11-10
      • 2017-01-05
      • 1970-01-01
      • 2013-05-18
      • 1970-01-01
      相关资源
      最近更新 更多