2016-11-25 3 views
1

私は国のリストを見つける方法をGoogleで検索してPickerViewに実装しようとしますが、私はそれは言って私にエラーを与える私はcountries.countをしようと、この時点で立ち往生しています:リスト3

'int'型の戻り値の型を 'string'型に変換できません 提案はありますか?あなたが配列の方法numberOfRowsInComponentに、あなたは配列オブジェクトを返す必要がtitleForRowでカウント返す必要が

import UIKit 

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{ 
    @IBOutlet weak var pickerValue: UIPickerView! 

    let countries = NSLocale.isoCountryCodes.map { (code:String) -> String in 
     let id = NSLocale.localeIdentifier(fromComponents: [NSLocale.Key.countryCode.rawValue: code]) 
     return NSLocale(localeIdentifier: "en_US").displayName(forKey: NSLocale.Key.identifier, value: id) ?? "Country not found for code: \(code)" 
    } 

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

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 
     return countries.count 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 

答えて

2

、それがピッカーに表示されることを示しています。

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

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 
    return countries[row] 
} 
関連する問題