无法下标'[UInt32]'类型的值

时间:2016-02-27 10:48:43

标签: arrays swift firebase arc4random

我有一个随机生成的数字数组,然后我尝试在Firebase中查询一个问题,该问题等于所选数字数组中索引[0]的值。目前的问题是我收到错误Cannot subscript a value of type '[UInt32]'。附:我在swift方面并不是极其有经验,因此精确的代码解决方案会非常苛刻!我还附上了我的火力架结构......

import UIKit
import Firebase

class QuestionViewController: UIViewController {


let ref = Firebase(url: "https://123test123.firebaseio.com/questions")

override func viewDidLoad() {
    super.viewDidLoad()

    // An empty array to hold the selected numbers
    var selectedNumbers: [UInt32] = []

    // A range of acceptable numbers
    let randomNumberRange = 1...10

    // How many numbers are needed?
    let randomNumbersToChoose = 10

    // Crash if asking for more numbers than are available in the range
    assert(randomNumberRange.count >= randomNumbersToChoose, "Must have enough numbers to choose from!")

    // Repeat this loop until all enough numbers have been selected
    while selectedNumbers.count < randomNumbersToChoose {

        // Pick a random number within the allowed range
        let selectedNumber = arc4random_uniform(UInt32(randomNumberRange.endIndex - randomNumberRange.startIndex)) + UInt32(randomNumberRange.startIndex)

        // If it's not already in the selected array, add it
        if (selectedNumbers.indexOf(selectedNumber) == nil) {
            selectedNumbers.append(selectedNumber)
        }
    }

    // Print the result
    print(selectedNumbers)


        print(selectedNumbers)
        let selectedNumberIndex: UInt32 = 2
        ref.queryOrderedByChild("value").queryEqualToValue(selectedNumbers[0])
            .observeEventType(.ChildAdded, withBlock: {
                snapshot in
                //Do something with the question
                print(snapshot.key)
                print(snapshot.value.valueForKey("question"))
            })
    }

@IBAction func truepressed(sender: AnyObject) {
}

@IBAction func falsePressed(sender: AnyObject) {
}

}

JSON数据:

{
  "question1" : {
    "answer" : "Nohghpe",
    "question" : "Do you know swift",
    "value" : 1
  },
  "question10" : {
    "answer" : "A fdsbit",
    "question" : "Do you kndfggow firebase",
    "value" : 10
  },
  "question2" : {
    "answer" : "A bfhit",
    "question" : "Dodhfg you know firebase",
    "value" : 2
  },
  "question3" : {
    "answer" : "A bsdit",
    "question" : "Do you know firebsgdfase",
    "value" : 3
  },
  "question4" : {
    "answer" : "A vcxbit",
    "question" : "Do yosgfdu know firebase",
    "value" : 4
  },
  "question5" : {
    "answer" : "A bivcxt",
    "question" : "Do you kfghnow firebase",
    "value" : 5
  },
  "question6" : {
    "answer" : "A bxcvit",
    "question" : "Do you know fnhirebase",
    "value" : 6
  },
  "question7" : {
    "answer" : "A bivxct",
    "question" : "Do you sgdfknow firebase",
    "value" : 7
  },
  "question8" : {
    "answer" : "A bivcxt",
    "question" : "Do you knsfdow firebase",
    "value" : 8
  },
  "question9" : {
    "answer" : "A bdsfit",
    "question" : "Do you kdfgnow ffsdirebase",
    "value" : 9
  }
}

1 个答案:

答案 0 :(得分:7)

这是Swift编译器模糊另一个背后的真实错误的典型情况。 .queryEqualToValue(..) method期望参数类型为AnyObject;它只能包含引用(类)类型,而UInt32是值类型。

另一个混乱的主题可能是我们通常习惯AnyObject类型可以,看似,持有Int类型的事实,实际上这样赋值隐式将Swift本机Int值类型转换为Foundation __NSCFNumber引用类型。但是,此隐式转换不适用于UInt32类型。

var a : AnyObject?
let foo : [Int] = [1, 2, 3]
let bar : [UInt32] = [1, 2, 3]

/* OK: Int -> [implicitly] -> __NSCFNumber */
a = foo[0]
print(a!.dynamicType) // __NSCFNumber

/* Not OK */
a = bar[0]
/* error: cannot subscript a value of type '[UInt32]' a = bar[0] */

因此,您可以通过以下方式解决此问题:

  1. selectedNumbers成为Int的数组,而不是UInt32(并相应地修改代码中受影响的部分)。

  2. 在您致电UInt32的通话中,执行从Int.queryEqualToValue(..)的类型转换。例如,在上面的例子中:

    a = Int(bar[0])
    

    请注意,对于32位系统(例如iPhone 5),由UInt32类型表示的数字的上半部分的一半不能用Int类型表示,因为在32位系统上, Int对应Int32

    INT32_MAX   // 2147483647
    UINT32_MAX  // 4294967295
    

    但是,从快速浏览一下代码来看,这似乎不应该是一个问题,因为selectedNumbers的元素不会包含{{1}所代表的较大数字(对于64位系统:好的,UInt32对应Int)。但是,如果选择此选项,您应该在转换之前断言Int64值可以由用于运行应用程序的系统上的UInt32类型表示。