数组 – 进入uipickerview swift 3的国家列表

前端之家收集整理的这篇文章主要介绍了数组 – 进入uipickerview swift 3的国家列表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我用Google搜索了如何查找国家/地区的列表并尝试实施到PickerView,但是我在这一点上陷入困​​境,我尝试做country.count它给了我一个错误说:

cannot convert return expression of type ‘int’ to return type ‘string’
any suggestions?

  1. import UIKit
  2.  
  3. class ViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate{
  4. @IBOutlet weak var pickerValue: UIPickerView!
  5.  
  6. let countries = NSLocale.isoCountryCodes.map { (code:String) -> String in
  7. let id = NSLocale.localeIdentifier(fromComponents: [NSLocale.Key.countryCode.rawValue: code])
  8. return NSLocale(localeIdentifier: "en_US").displayName(forKey: NSLocale.Key.identifier,value: id) ?? "Country not found for code: \(code)"
  9. }
  10.  
  11. func numberOfComponents(in pickerView: UIPickerView) -> Int {
  12. return 1
  13. }
  14.  
  15. func pickerView(_ pickerView: UIPickerView,titleForRow row: Int,forComponent component: Int) -> String? {
  16. return countries.count
  17. }
  18.  
  19. override func viewDidLoad() {
  20. super.viewDidLoad()
  21. // Do any additional setup after loading the view,typically from a nib.
  22. }
  23.  
  24. override func didReceiveMemoryWarning() {
  25. super.didReceiveMemoryWarning()
  26. // Dispose of any resources that can be recreated.
  27. }
  28. }
您需要在方法numberOfRowsInComponent中返回数组计数,并且在titleForRow中需要返回数组对象,显示它将在picker中显示.
  1. func pickerView(_ pickerView: UIPickerView,numberOfRowsInComponent component: Int) -> Int {
  2. return countries.count
  3. }
  4.  
  5. func pickerView(_ pickerView: UIPickerView,forComponent component: Int) -> String? {
  6. return countries[row]
  7. }

猜你在找的Swift相关文章