【问题标题】:Swift AnyObject is not convertible to String/IntSwift AnyObject 不能转换为 String/Int
【发布时间】:2014-10-16 10:18:36
【问题描述】:

我想将 JSON 解析为对象,但我不知道如何将 AnyObject 转换为 String 或 Int,因为我得到了:

0x106bf1d07:  leaq   0x33130(%rip), %rax       ; "Swift dynamic cast failure"

使用举例时:

self.id = reminderJSON["id"] as Int

我有 ResponseParser 类并且在它里面(responseReminders 是一个 AnyObjects 数组,来自 AFNetworking responseObject):

for reminder in responseReminders {
    let newReminder = Reminder(reminderJSON: reminder)
        ...
}

然后在 Reminder 类中我像这样初始化它(提醒为 AnyObject,但是是 Dictionary(String, AnyObject)):

var id: Int
var receiver: String

init(reminderJSON: AnyObject) {
    self.id = reminderJSON["id"] as Int
    self.receiver = reminderJSON["send_reminder_to"] as String
}

println(reminderJSON["id"]) 结果为:可选(3065522)

在这种情况下,如何将 AnyObject 向下转换为 String 或 Int?

//编辑

经过一些尝试,我提出了这个解决方案:

if let id: AnyObject = reminderJSON["id"] { 
    self.id = Int(id as NSNumber) 
} 

对于 Int 和

if let tempReceiver: AnyObject = reminderJSON["send_reminder_to"] { 
    self.id = "\(tempReceiver)" 
} 

对于字符串

【问题讨论】:

  • 您是否尝试过强制转换为可选项? reminderJSON["id"] as Int?
  • 您应该将您的解决方案添加到您的问题中作为答案,而不是编辑您的问题。我们允许/鼓励您在 StackOverflow 上回答自己的问题。
  • 我对字符串使用了相同的解决方案,比类型转换和测试要容易得多!
  • 谢谢,但自 Swift 1.2 起,as 就是 as!

标签: swift xcode6-beta6


【解决方案1】:

reminderJSON["id"] 给你一个AnyObject?,所以你不能把它转换成Int 你必须先解开它。

self.id = reminderJSON["id"]! as Int

如果您确定 id 将出现在 JSON 中。

if id: AnyObject = reminderJSON["id"] {
    self.id = id as Int
}

否则

【讨论】:

  • 我已经尝试了这两种方法 - 转换仍然失败,但最后这有效: if let id: AnyObject =reminderJSON["id"] { self.id = Int(id as NSNumber ) } 对于 Int 和 if let tempReceiver: AnyObject =reminderJSON["send_reminder_to"] { self.id = "(tempReceiver)" } 对于字符串
  • Dictionary 之后添加! as String 对我有用。但是你能解释为什么它的解决方案吗?我不明白为什么它有效。为什么as String 不起作用?
【解决方案2】:

在 Swift 中,StringInt 不是对象。这就是您收到错误消息的原因。您需要转换为对象 NSStringNSNumber。一旦你有了这些,它们就可以分配给StringInt类型的变量。

我推荐以下语法:

if let id = reminderJSON["id"] as? NSNumber {
    // If we get here, we know "id" exists in the dictionary, and we know that we
    // got the type right. 
    self.id = id 
}

if let receiver = reminderJSON["send_reminder_to"] as? NSString {
    // If we get here, we know "send_reminder_to" exists in the dictionary, and we
    // know we got the type right.
    self.receiver = receiver
}

【讨论】:

  • 如果不是对象,那是什么? String有一个名为“hasPrefix”的方法,如何把这个方法放在没有类的地方?
  • 字符串是一个结构。在 Swift 中,结构可以有方法。不同之处在于它们的传递方式:结构通过值传递(复制),类通过引用传递(指针)。更令人困惑的是,大多数时候 Strings 和 NSStrings 是自动桥接的,因此您通常不必考虑它。您可以将 Swift String 传递给采用 NSString 的可可方法,它就可以工作。这个转换实例是出现差异的情况之一。
  • 在我的情况下,传入的 JSON 字典具有从服务器返回的整数值作为字符串,那么? NSString",然后使用 .integerValue 转换为整数。
  • 怎么样:if let stringValue = dict["id"] as? NSString as String? { … }?
  • 您过去可以将 NSString 分配给 String,但现在您必须强制转换它。我相信现在只需as? String 就足够了。
【解决方案3】:

现在你只需要import Foundation。 Swift 会将值 type(String,int) 转换为对象 types(NSString,NSNumber)。由于 AnyObject 可以与所有对象一起使用,现在编译器不会抱怨。

【讨论】:

  • 它不适用于mytextfield.text = NSUserDefaults.standardUserDefaults().valueForKey( "mykey" )
【解决方案4】:

这实际上很简单,可以在一行中提取、转换和解包值:if let s = d["2"] as? String,如:

var d:[String:AnyObject] = [String:AnyObject]()
d["s"] = NSString(string: "string")

if let s = d["s"] as? String {
    println("Converted NSString to native Swift type")
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 2014-10-10
    • 2017-04-07
    相关资源
    最近更新 更多