问题描述
我一直试图让这个工作多年没有成功,如果这对你们来说似乎很容易,请道歉.
Hi there I have been trying to get this working for ages with no success so apologies if this seems easy for you guys.
我想使用 3 轮 UIPickerView - 第一个轮的 didSelectRow 值将用于更改其余两个轮的数组,但它们将与转换应用程序相同.
I want to use a 3 wheel UIPickerView - The 1st wheel didSelectRow value will be used to change the array for the remaining two wheels but they will be the same as it is a conversion app.
当填充轮子说 Anyobject 与 String 不同时,这似乎给我一个错误.我看不出有什么问题,但我知道这是我错过的基本内容.
It seems to throw me an error up when populating the wheels saying that Anyobject is not identical to String. I cannot see anything wrong but I know it is something basic I have missed.
任何指针将不胜感激.
莫蒂.
// ViewController.swift
// picker test
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate {
@IBOutlet weak var picker1Label: UILabel!
@IBOutlet weak var picker2Label: UILabel!
@IBOutlet weak var bigPicker: UIPickerView!
var wheelContents = []
var length = ["metres","feet","yards","inches","mm","cm","miles"]
var volume = ["m3","US Gall","Imp Gall","Barrels", "cubic FT","litres"]
var conType = ["length","volume"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
wheelContents = [conType,length,length]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//For main selection of type of conversion
// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(bigPicker: UIPickerView) -> Int{
return wheelContents.count
}
// returns the # of rows in each component..
func pickerView(bigPicker: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return wheelContents[component].count
}
func pickerView(bigPicker: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{
return wheelContents[component][row]
}
}
推荐答案
由于你声明了这样的wheelContents wheelContents = []
,没有指定它的元素类型,编译器自动推断它是数组AnyObjects 又名 [AnyObject]
.
Since you declared wheelContents like this wheelContents = []
, without specifying its elements type, the compiler automatically infers that it is array of AnyObjects aka [AnyObject]
.
这就是为什么当您返回 wheelContents[component].count
时它会生成错误:此时编译器需要一个 String!
但您正在提供AnyObject
.
That's the reason why when you are returning wheelContents[component].count
it generates an error: at that moment the compiler is expecting a String!
but you are providing an AnyObject
.
这是一个非常容易解决的问题,您应该在声明它时指定数组的内容.(它是一个字符串数组,又名 [[String]]
)
It's a really easy fix, you should just specify what the content of the array it is going to be when you declare it. (it's an array of arrays of strings aka [[String]]
)
这篇关于快速使用带有多个组件的 UIPickerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!