试图解决错误“对成员'下标的模糊引用'”

时间:2016-10-31 19:08:33

标签: xcode8 swift2.3

首先我创建一些列表:

let day0 = [0] as NSArray
let day1 = [0, 8, 2, 3, 4] as NSArray
let day2 = [0, 1, 2, 3, 4] as NSArray
let day3 = [0, 1, 2, 3, 4] as NSArray
let day4 = [0, 1, 2, 3, 4] as NSArray

let month0 = [0] as NSArray
let january = [day0, day1, day2, day3, day4] as NSArray
let february = [day0, day1, day2, day3, day4] as NSArray
let march = [day0, day1, day2, day3, day4] as NSArray
let april = [day0, day1, day2, day3, day4] as NSArray
let may = [day0, day1, day2, day3, day4] as NSArray
let june = [day0, day1, day2, day3, day4] as NSArray
let july = [day0, day1, day2, day3, day4] as NSArray
let august = [day0, day1, day2, day3, day4] as NSArray
let september = [day0, day1, day2, day3, day4] as NSArray
let october = [day0, day1, day2, day3, day4] as NSArray
let november = [day0, day1, day2, day3, day4] as NSArray
let december = [day0, day1, day2, day3, day4] as NSArray

let calendar = [month0, january, february, march, april, may, june, july, august, september, october, december] as NSArray

然后我尝试返回一个元素:

@IBAction func SetVariablesButton(sender: UIButton)
{
    var result = calendar[1][1][1] as String
    BedTimeLabel.text = String(result)
}

然后我收到错误。我尝试了各种变化,例如

let day0 = [0] as! NSArray

let day0 = [0] as Array<T>

但那些没有用。 我原本根本没有施展变量,但在研究了常见问题之后,常见的解决方案就是抛出变量,以便编译器能够理解正在发生的事情。 我正在使用Xcode 8和Swift 2.3

1 个答案:

答案 0 :(得分:0)

首先将虚拟month0声明为[[0]],以获得与其他人相同的类型。

然后移除所有演员。

let day0 = [0]
let day1 = [0, 8, 2, 3, 4]
let day2 = [0, 1, 2, 3, 4]
let day3 = [0, 1, 2, 3, 4]
let day4 = [0, 1, 2, 3, 4]

let month0 = [[0]]
let january = [day0, day1, day2, day3, day4]
let february = [day0, day1, day2, day3, day4]
let march = [day0, day1, day2, day3, day4]
let april = [day0, day1, day2, day3, day4]
let may = [day0, day1, day2, day3, day4]
let june = [day0, day1, day2, day3, day4]
let july = [day0, day1, day2, day3, day4]
let august = [day0, day1, day2, day3, day4]
let september = [day0, day1, day2, day3, day4]
let october = [day0, day1, day2, day3, day4]
let november = [day0, day1, day2, day3, day4]
let december = [day0, day1, day2, day3, day4]

let calendar = [month0, january, february, march, april, may, june, july, august, september, october, december]

编译器可以推断出calendar[[[Int]]]

现在你可以写

var result = String(calendar[1][1][1])