无法下标“[UInt32]”类型的值
Cannot subscript a value of type '[UInt32]'
我有一个随机生成的数字数组,然后我尝试在 Firebase 中查询一个问题,该问题等于所选数字数组中索引 [0] 处的值。目前的问题是我收到错误 Cannot subscript a value of type '[UInt32]'
。 P.S。我在 swift 方面不是很有经验,所以非常感谢精确的代码解决方案!我还附上了我的 firebase 结构...
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
}
}
这是 Swift 编译器掩盖另一个编译器背后真正错误的典型情况。 .queryEqualToValue(..)
method 需要一个类型为 AnyObject
的参数;它只能包含引用 (class) 类型,而 UInt32
是值类型。
另一个混淆的主题可能是我们通常习惯于 AnyObject
类型可以 看似 持有 Int
类型,当事实上,这样的赋值隐式地将 Swift 本机 Int
值类型转换为基础 __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] */
因此,您可以通过以下方式解决此问题:
让 selectedNumbers
成为数组 0f Int
,而不是 UInt32
(并相应地修改代码中受影响的部分)。
在对 .queryEqualToValue(..)
的调用中执行从 UInt32
到 Int
的类型转换。例如,在上面的示例中:
a = Int(bar[0])
请注意,对于 32 位系统(例如 iPhone 5),可由 UInt32
类型表示的数字上限的一半不能由 Int
类型表示,因为在 32 位系统上,Int
对应于 Int32
。
INT32_MAX // 2147483647
UINT32_MAX // 4294967295
然而,快速浏览一下您的代码,这似乎不是问题,因为 selectedNumbers
的元素将不包含 [=15= 可表示的较大数字](并且,对于 64 位系统:好的,因为 Int
对应于 Int64
)。但是,如果选择此选项,为了良好的实践,您应该在转换之前断言 UInt32
值可以由旨在 运行 您的应用程序的系统上的 Int
类型表示。
我有一个随机生成的数字数组,然后我尝试在 Firebase 中查询一个问题,该问题等于所选数字数组中索引 [0] 处的值。目前的问题是我收到错误 Cannot subscript a value of type '[UInt32]'
。 P.S。我在 swift 方面不是很有经验,所以非常感谢精确的代码解决方案!我还附上了我的 firebase 结构...
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
}
}
这是 Swift 编译器掩盖另一个编译器背后真正错误的典型情况。 .queryEqualToValue(..)
method 需要一个类型为 AnyObject
的参数;它只能包含引用 (class) 类型,而 UInt32
是值类型。
另一个混淆的主题可能是我们通常习惯于 AnyObject
类型可以 看似 持有 Int
类型,当事实上,这样的赋值隐式地将 Swift 本机 Int
值类型转换为基础 __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] */
因此,您可以通过以下方式解决此问题:
让
selectedNumbers
成为数组 0fInt
,而不是UInt32
(并相应地修改代码中受影响的部分)。在对
.queryEqualToValue(..)
的调用中执行从UInt32
到Int
的类型转换。例如,在上面的示例中:a = Int(bar[0])
请注意,对于 32 位系统(例如 iPhone 5),可由
UInt32
类型表示的数字上限的一半不能由Int
类型表示,因为在 32 位系统上,Int
对应于Int32
。INT32_MAX // 2147483647 UINT32_MAX // 4294967295
然而,快速浏览一下您的代码,这似乎不是问题,因为
selectedNumbers
的元素将不包含 [=15= 可表示的较大数字](并且,对于 64 位系统:好的,因为Int
对应于Int64
)。但是,如果选择此选项,为了良好的实践,您应该在转换之前断言UInt32
值可以由旨在 运行 您的应用程序的系统上的Int
类型表示。