从Optional中检索值

时间:2016-06-09 23:58:58

标签: swift dictionary

我正在使用xcode,快速申请。

我有这个可选值,我使用:

打印到控制台
    print(myData)

这是可选的

Optional({
    "avatar_content_type" = "image/png";
    "avatar_file_name" = "add_a_photo_three-b04cdb4bcfc59b603590d88567e359115b3bbc63acfde789bd6f2caa3a1410ce.png";
    "avatar_file_size" = 17312;
    "avatar_updated_at" = "2016-05-24T22:36:17.000Z";
    "cover_image_file_name" = "<null>";
    "cover_image_url" = "/images/_staging/og-image-alt.png";
    "created_at" = "2016-05-24T22:36:17.000Z";
    creator = "<null>";
    description = "";
    id = 246;
    "mycount" = 0;
    name = "Spring/Summer";
    size = 0;
    "updated_at" = "2016-05-24T22:36:26.000Z";
    user =     {
        "avatar_content_type" = "image/png";
        "avatar_file_name" = "upload.png";
        "avatar_file_size" = 5534390;
        "avatar_updated_at" = "2015-12-26T22:07:02.000Z";
        bio = "Chief";
        "created_at" = "2015-11-09T02:20:42.000Z";
        "date_of_birth" = "1982-07-29";
        email = "person@site.com";
        "first_name" = Joseph;
        gender = male;
        id = 3;
        "is_private" = 0;
        "last_name" = Romulus;
        "next_stand_number" = "<null>";
        "profile_image_medium_url" = "/images/users/original/3.png/?1451167622";
        "profile_image_thumb_url" = "/images/users/thumb/3.png/?1451167622";
        provider = "<null>";
        uid = "<null>";
        "updated_at" = "2016-06-09T18:38:47.000Z";
        "user_name" = Joe;
        "user_token" = "############";
    };
    "user_id" = 3;
})

我正在尝试这样做

let created_at_raw = data["created_at"] as! String

但它会出现此错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

我习惯使用字典,但这不是一个,我对如何从这里获取值感到困惑:"created_at" = "2016-05-24T22:36:17.000Z";

编辑1

更多代码:

    let created_at_raw = data["created_at"] as! String
    let formatter : NSDateFormatter = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
    formatter.timeZone = NSTimeZone(name: "UTC")
    self.created_at = formatter.dateFromString(created_at_raw)!

编辑2

class MYUserStuff: MYObject {
  private var created_at : NSDate

  init(data: Dictionary<String, AnyObject>) {

...


    if let created_at_raw = data["created_at"] as? String {
        let formatter : NSDateFormatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
        formatter.timeZone = NSTimeZone(name: "UTC")
        self.created_at = formatter.dateFromString(created_at_raw)!
    }
...

}

给出错误:

.swift:18:5: Property 'self.created_at' not initialized at implicitly generated super.init call

1 个答案:

答案 0 :(得分:1)

您收到此错误,因为必须在初始化时定义非可选值。您需要使变量created_at成为可选类型,并在数据类型

之后带有问号
class MYUserStuff: MYObject {
    private var created_at : NSDate?

    init(data: Dictionary<String, AnyObject>) {

    ...


    if let created_at_raw = data["created_at"] as? String {
        let formatter : NSDateFormatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
        formatter.timeZone = NSTimeZone(name: "UTC")
        self.created_at = formatter.dateFromString(created_at_raw)!
    }

   ...
}

我建议您阅读本文以熟悉期权和非期权_ A Beginner’s Guide to Optionals in Swift