- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个随机生成的数字数组,然后我尝试在 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] */
因此,您可以通过以下方式解决此问题:
让 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
的元素将不包含 可表示的较大数字UInt32
(对于 64 位系统:OK,因为 Int
对应于 Int64
)。但是,如果选择此选项,为了良好实践,您应该在转换之前断言 UInt32
值可以由旨在运行您的系统的 Int
类型表示申请。
关于arrays - 无法下标类型 '[UInt32]' 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35668775/
我有以下代码 unsigned int headerbytes = 0U; headerbytes = (unsigned int*)strtoull(packet_space->header
我有这段无法编译的代码: public struct MyStruct { private fixed uint myUints[32]; public uint[] MyUints
在 Go 中,从函数返回哪个更有效:返回 uint 还是返回 *uint? 该函数在 cpu 密集型库的内部 for 循环中调用。 最佳答案 一般来说,只要效率是个问题,您就应该运行基准测试。 让我们
int 加上 unsigned int 返回一个 unsigned int。应该这样吗? 考虑这段代码: #include #include #include class test {
我正在尝试从可通过 URL 访问的内容中初始化一个字符串: actualresponse.response = String(contentsOfURL: url, usedEncoding: NSU
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 提供事实和引用来回答它. 1年前关闭。 Improve this
我从函数 Swift 得到类型为 UnsafeMutablePointer 的结果 我可以把它转换到UInt吗? ? 最佳答案 只需使用memory 属性来访问底层数据。 let ptr: Unsaf
我深入了解了 List并发现了以下代码: public T this[int index] { get { // Following trick can red
我在 this page on bit twiddling 的帮助下编写了这个函数: uint16_t *decode(uint64_t instr) { // decode instr (thi
我正在从微 Controller 读取两个寄存器。一个具有 4 位 MSB(前 4 位有一些其他内容),另一个具有 8 位 LSB。我想将其转换为一个 12 位 uint(准确地说是 16 位)。到目
要演示的示例代码: public int FindComplement(int num) { //uint mask = ~0; //<-- error CS0031 //
$ rustc --test mapAsMapKey.rs mapAsMapKey.rs:18:43: 18:52 error: mismatched types: expected `fn@(&&@
一般问题:我有一个很大的二维点空间,里面稀疏地分布着点。把它想象成一 block 撒满黑点的白色大 Canvas 。我必须多次迭代和搜索这些点。 Canvas (点空间)可能很大,接近极限int 的值
假设我们只是调用一个普通数字,数字会启动什么。 uint256 plainNumber 我明白它是零。但是我要问的是,有没有办法检测该数字是由编译器还是用户变量设置的。例如... uint256 pl
我试图在 leetcode.com ( https://leetcode.com/problems/number-of-1-bits/ ) 上解决一个简单的问题,我遇到了一个奇怪的行为,这可能是我缺乏
uint number = 0x418 in bits : 0000010000011000 uint number1 = 0x8041 in bits: 1000000001000001 uint
我如何在 C# 中生成具有某个最大值的伪随机 uint? (不需要最低限度。)似乎有很多问题要求完全随机,但没有上限。 澄清:此上限可能大于 int.MaxValue,因此仅强制转换 Random.N
我已经用私有(private)数据成员围绕 ulong 编写了一个简单的包装器。我希望能够将包装器转换为 ulong 以检索数据。我希望强制转换为 uint 并丢失数据是非法的,因此我没有编写对 ui
哪些是“Uint”变量?就是有“Uint8”、“Uint16”等…… 但是它们是什么? 现在我有一些时间使用 C++,但我从来不需要使用这些变量并引起我的好奇。 提前致谢。 最佳答案 uint 不是标
我有一个 native 方法,它需要一个指针来写出一个双字(uint)。 现在我需要从 (Int) 指针中获取实际的 uint 值,但是 Marshal 类只有方便的方法来读取(有符号)整数。 如何从
我是一名优秀的程序员,十分优秀!