- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
编辑:不重复:
该解决方案给出了'Could not find an overload for 'subscript' that accepts the supplied arguments'
错误。所以,不,这不是重复的。
这是函数声明。
func auth(user: String, pass: String, completion: (returned: Bool, error: Bool, response: Dictionary<String, NSObject>?) -> ()){
response
可以是 nil
现在我试图访问在另一个文件中传回的值并收到错误:
if let labelString = response["error_description"] as! String?{
self.labelPrompt.text = labelString
}
错误:无法为“字典”类型的值下标?具有“字符串”类型的索引
最佳答案
它是链接问题的副本:您需要在使用下标之前打开字典。
有很多方法(“if let”等),链接的答案通过添加 ?
给出了使用“可选绑定(bind)”的解决方案。在保存字典的变量和下标之间。
Playground 上的例子:
var response: Dictionary<String, NSObject>? = nil
// NOTICE THE "?" BETWEEN THE VARIABLE AND THE SUBSCRIPT
if let labelString = response?["error_description"] as? String {
println(labelString) // not executed because value for key is nil
}
response = ["test":"yep"]
if let labelString = response?["test"] as? String {
println(labelString) // "yep"
}
另一种解包字典的方法:
if let responseOK = response, let test = responseOK["test"] as? String {
println(test) // "yep"
}
关于Swift:无法下标类型为 'Dictionary<String, NSObject>?' 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32015990/
SWIFT代码 print("1", NSObject() == NSObject()) print("2", ObjectIdentifier(NSObject()) == ObjectIdenti
保留在 NSObject 协议(protocol)中声明。 因此NSObject类和NSProxy类实现了它。 然而 NSProxy 和 NSObject 类都有一个分配。 为什么 alloc 没有在
我有一个已在App Store中发布的应用程序。大约有1%-2%的用户报告该应用程序崩溃了。这不是完全意外的行为,因此我要求提供崩溃日志。这是最后一个异常回溯(实际显示问题的部分): Last E
我有一个自定义 NSObject 类,可以调用 People,还有一个来自 CloudMade RMMarker.h 的名为 RMMarker 的类。 RMMarker 类有一个名为 data 的属性
这是崩溃报告,不知道为什么。我使用 AFHTTPRequestOperation setCompletionBlockWithSuccess:failure: 调用电话。在完成 block 中,我得到
应用程序有时会因 [NSObject(NSObject) doesNotRecognizeSelector:] 而崩溃,选择器为 [UIImageView setImage:]。 我通过设置异常断点捕
崩溃报告: 0 CoreFoundation!__exceptionPreprocess + 0x7c 1 libobjc.A.dylib!objc_exception_throw
我正在将我的整个项目从 Objective-C 转换为 Swift...在转换时我遇到了这个问题:“无法将‘Facility’类型的值转换为预期的参数类型“Facility!” 错误出现在我提到的最后
我在将数组[String]保存到核心数据时遇到问题。在核心数据中,我设置了字段“countires”的类型 - 可转换。 我通过以下方式添加数据: filters!.countries = selec
假设您有一个返回 NSDictionary 的方法。要构建字典,您可能需要创建一个 NSMutableDictionary。返回字典的不可变副本而不是仅返回可变字典是否有任何必要或优势? 例如 - (
在一个小型 RTS 项目中,我有一个按钮列表,每个按钮都分配有一个构建对象。有些建筑物非常通用,但其他建筑物则非常特殊,因此我将某些建筑物作为子类。 当我创建一个按钮列表时,每个按钮都有一个 PEHo
这个问题在这里已经有了答案: Objective-C: Property / instance variable in category (6 个答案) Objective-C: Instance
我实际上想知道如何从 NSObject 或任何其他类访问 encodeWithCoder、init 等方法而不继承 NSObject 类。 因为我读到如果我们从 NSObject 类继承,那么它在 S
我正致力于从 Objective-C 教程转换此委托(delegate)协议(protocol)函数,但在尝试通过使用下标返回基于键的值来访问字典中的值时遇到错误。我不太确定这里的错误是什么意思。任何
在我的 Swift 应用程序中,我有一个类: open class CustomCluster : NSObject { open var coordinate = CLLocationCoo
我有一个字典/数组,看起来像这样: var myArray: [[String:NSObject]] = [] let newItem = [ [
这很奇怪,我可以使用 Xcode 5 (5A1413) 在模拟器、iPhone 4S 和 iPhone 5 中运行我的应用程序而没有任何问题,但是当我将应用程序提交到 iTunes 商店时被拒绝并且此
我使用 NSKeyedArchiver.archivedDataWithRootObject(obj) 将对象转换为 NSData。 archivedDataWithRootObject(obj) 方
背景。 请考虑以下步骤: 1) 在 Xcode 中创建一个新的“单 View 应用程序”。 2)创建类NSObject+Extension.h和.m文件: // .h @interface NSObj
在 ARC 环境中遇到一些小问题。创建一个将 View 添加到父 View 的 NSObject - 它基本上是一个可以处理一些文本并显示它的“弹出类”。 在 View Controller 中它被实
我是一名优秀的程序员,十分优秀!