gpt4 book ai didi

ios - 不符合协议(protocol) UIPickerViewDataSource

转载 作者:IT王子 更新时间:2023-10-29 05:25:25 25 4
gpt4 key购买 nike

我不知道我的代码有什么问题。我尝试按照教程进行操作,但发生了同样的错误。

错误:

Type 'FourthViewController' does not conform to protocol 'UIPickerViewDataSource'

这是我的代码:

import UIKit

let characters = ["Jaja Bink", "Luke", "Han Solo", "Princess Leia"];

let weapons = ["LightSaber", "Pistol", "Keris"];

class FourthViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

@IBOutlet weak var doublePicker: UIPickerView!

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

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

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


func pickerView(pickerView: UIPickerView,
titleForRow row: Int,
forComponent component: Int) -> String? {

if component == 0 {
return characters[row]
} else {
return weapons[row]
}

}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int? {
if component == 0 {
return characters.count
} else {
return weapons.count
}
}

}

最佳答案

替换 UIPickerViewDataSource 协议(protocol)方法的弃用版本,如果您使用的是 Swift 3 可能适合您。

已弃用的协议(protocol)方法

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

Swift 3 协议(protocol)的最新方法

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

注意:对于其他必需的协议(protocol)方法也是如此。即

Deprecated:

func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int

Recent Version:

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int

关于ios - 不符合协议(protocol) UIPickerViewDataSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39198893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com