gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 05:50:37 26 4
gpt4 key购买 nike

我有一个随机生成的数字数组,然后我尝试在 Firebase 中查询一个问题,该问题等于所选数字数组中索引 [0] 处的值。目前的问题是我收到错误 Cannot subscript a value of type '[UInt32]'。附言我在 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 类型的参数;它只能包含引用(类)类型,而 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 成为数组 0f Int,而不是 UInt32(并相应地修改代码中受影响的部分)。

  2. 在对 .queryEqualToValue(..) 的调用中执行从 UInt32Int 的类型转换。例如,在上面的示例中:

    a = Int(bar[0])

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

    INT32_MAX   // 2147483647
    UINT32_MAX // 4294967295

    但是,快速浏览一下您的代码,这似乎不是这里的问题,因为 selectedNumbers 的元素将不包含 可表示的较大数字UInt32(对于 64 位系统:OK,因为 Int 对应于 Int64)。但是,如果选择此选项,为了良好实践,您应该在转换之前断言 UInt32 值可以由旨在运行您的系统的 Int 类型表示申请。

关于arrays - 无法下标类型 '[UInt32]' 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35668775/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com