无法下标类型'[UInt32的]'的值 [英] Cannot subscript a value of type '[UInt32]'
问题描述
我已经随机生成的数字数组,然后我试图查询火力地堡的一个问题,就是等于在选定号码的数组中的索引[0]的值。目前的问题是,我得到一个错误无法下标类型的值'[UInt32的]
。附:我不是在的极端迅速经历了这么详细的code解决方案将是非常一个preciated!我还附上我的火力结构...
进口的UIKit
进口火力地堡类QuestionViewController:UIViewController的{
让REF =火力地堡(URL:https://123test123.firebaseio.com/questions)覆盖FUNC viewDidLoad中(){
super.viewDidLoad() //空数组来保存选择的号码
变种selectedNumbers:[UInt32的] = [] //接受号范围
让randomNumberRange = 1 ... 10 //多少个数字需要?
让randomNumbersToChoose = 10 //崩溃,如果要求更多的数字比范围内的可用
断言(randomNumberRange.count> = randomNumbersToChoose,必须有足够的数量可供选择!) //直到所有足够的编号已被选定重复这个循环
而selectedNumbers.count< randomNumbersToChoose { //在允许的范围内选择一个随机数
让selectedNumber = arc4random_uniform(UInt32的(randomNumberRange.endIndex - randomNumberRange.startIndex))+ UInt32的(randomNumberRange.startIndex) //如果不是已经选定的阵中,添加
如果(selectedNumbers.indexOf(selectedNumber)==无){
selectedNumbers.append(selectedNumber)
}
} //打印结果
打印(selectedNumbers)
打印(selectedNumbers)
让selectedNumberIndex:UInt32的= 2
ref.queryOrderedByChild(值)。queryEqualToValue(selectedNumbers [0])
.observeEventType(.ChildAdded,withBlock:{
快照
//做一些与问题
打印(snapshot.key)
打印(snapshot.value.valueForKey(问题))
})
}@IBAction FUNC真pressed(发件人:AnyObject){
}@IBAction FUNC虚假pressed(发件人:AnyObject){
}}
JSON数据:
{
问题1 : {
答案:Nohghpe
问题:你知道雨燕,
值:1
},
question10:{
答案:A fdsbit
问题:你kndfggow火力
值:10
},
问题2:{
答案:A bfhit
问题:Dodhfg你知道火力地堡
值:2
},
问题3:{
答案:A bsdit
问题:你知不知道firebsgdfase
值:3
},
问题4:{
答案:A vcxbit
问题:你知道yosgfdu火力
值:4
},
question5:{
答案:A bivcxt
问题:你kfghnow火力
值:5
},
question6:{
答案:A bxcvit
问题:你知不知道fnhirebase
值:6
},
question7:{
答案:A bivxct
问题:你sgdfknow火力
值:7
},
question8:{
答案:A bivcxt
问题:你knsfdow火力
值:8
},
question9:{
答案:A bdsfit
问题:你kdfgnow ffsdirebase
值:9
}
}
这是斯威夫特编译器遮挡后面另外一个真正的错误的典型情况。该 .queryEqualToValue(..)
方法期望类型的参数 AnyObject
;可容纳仅供参考(类)的类型,而 UInt32的
是值类型。
困惑的另一个问题可能是我们一般用这样的事实: AnyObject
类型可以,的看似的,持有诠释
类型,而事实上,这种分配隐式转换雨燕本地内部
价值型基金会 __ NSCFNumber
引用类型。这隐式转换,但不适用于 UInt32的
键入
VAR一:AnyObject?
让富:[INT] = [1,2,3]
让栏:[UInt32的] = [1,2,3]/ * OK:INT - > [隐] - GT; __NSCFNumber * /
A = foo的[0]
打印(一!.dynamicType)// __NSCFNumber/* 不好 */
A =酒吧[0]
/ *错误:无法下标类型的值'[UInt32的]'一栏= [0] * /
因此,可以通过解决此问题:
-
放生
selectedNumbers
是0F内部
一个数组,而不是UInt32的
(和修改$ C $的受影响部分C.因此)。 -
在您的通话执行从
UInt32的
类型转换为内部
到.queryEqualToValue(..)
。例如,在上面的示例:A = INT(栏[0])
只是要小心,对于32位系统(如iPhone 5),数字的上限一半
UInt32的
重新presentable类型不能再$ P通过内部
键入$ psented,因为在32位系统中,内部
对应的Int32
。INT32_MAX // 2147483647
UINT32_MAX // 4294967295从您的code一瞥但是,它似乎没有,就好像这应该是这里的一个问题,因为元素
selectedNumbers
不会通过UInt32的
包含重presentable较大的数字(和,对于64位系统:OK,如内部
对应于的Int64
)。如果选择此选项,你应该,但是,对于好的做法,主张转换的UInt32的
值可以重新通过psented $ P $之前的诠释
键入旨在运行应用程序的系统。
I have an array of numbers that have been generated randomly and I am then trying to query Firebase for a question that is equal to the value at index [0] in the array of chosen numbers. The problem at the moment is that I get an error Cannot subscript a value of type '[UInt32]'
. P.S. I am not extremly experienced in swift so exact code solutions would be very apreciated! I have also attached my firebase structure...
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 data:
{
"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
}
}
This is a typical situation of the Swift compiler obscuring the real error behind another one. The .queryEqualToValue(..)
method expects an argument of type AnyObject
; which can hold only reference (class) types, whereas UInt32
is a value type.
Another subject of confusion could be that we're generally used to the fact that AnyObject
types can, seemingly, hold Int
types, when in fact such assignments implicitly converts the Swift native Int
value type to the Foundation __NSCFNumber
reference type. This implicit conversion is, however, not available for UInt32
type.
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] */
Hence, you could solve this issue by:
Letting
selectedNumbers
be an array 0fInt
, rather thanUInt32
(and modify the affected parts of your code accordingly).Perform a type conversion from
UInt32
toInt
in your call to.queryEqualToValue(..)
. E.g., in the example above:a = Int(bar[0])
Just take care that for 32-bit systems (e.g. iPhone 5), half the upper range of numbers representable by
UInt32
type cannot be represented byInt
type, since on 32-bit system,Int
corresponds toInt32
.INT32_MAX // 2147483647 UINT32_MAX // 4294967295
From a quick glance at your code, however, it doesn't seem as if this should be an issue here, as the elements of
selectedNumbers
will not contain the larger numbers representable byUInt32
(and, for 64-bit systems: OK, asInt
corresponds toInt64
). If choosing this option, you should, however, for good practice, assert prior to conversion that theUInt32
value can be represented by theInt
type on the systems intended to run your application.
这篇关于无法下标类型'[UInt32的]'的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!