【问题标题】:Slow compiling swift source file - Xcode 8 swift 3慢速编译 swift 源文件 - Xcode 8 swift 3
【发布时间】:2017-01-26 08:11:06
【问题描述】:

我刚刚将我的项目更新为Xcode 8Swift 3。 我的问题是我的应用程序编译特定类的速度非常慢,如下:

var dict: Dictionary<String, AnyObject> {
        return [
            "book_key": book_key as AnyObject,
            "book_title": book_title as AnyObject,
            "book_author": book_author as AnyObject,
            "book_price": book_price as AnyObject,
            "book_publisher" : book_publisher as AnyObject,
            "page_count": page_count as AnyObject,
            "book_description": book_description as AnyObject,
            "book_urlImage" : book_urlImage as AnyObject,
            "book_urlImage2": book_urlImage2 as AnyObject,
            "user_key": user_key as AnyObject,
            "user_name": user_name as AnyObject,
            "user_tag_login" : user_tag_login as AnyObject,
            "onGoingNegotiations" : onGoingNegotiations as AnyObject,
            "other_user_key": other_user_key as AnyObject,
            "other_tag_login": other_tag_login as AnyObject,
            "book_condition": book_condition as AnyObject,
            "timestamp": timestamp! as AnyObject
        ]
    }

如何解决我的问题?感谢您的帮助。

【问题讨论】:

  • 将字典转换为类(对象)可能会更容易,因为您拥有所有这些属性(book_key、book_title...等)
  • 使用ObjectMapper:只是一个建议
  • 谢谢@PeterLee,这样更好。使用 Xcode 7 比现在更快

标签: ios swift xcode dictionary swift3


【解决方案1】:

这个方法太慢了,所以我找到了解决方案。 timestamp NSNumber 将被转换为String,因此该方法再次完美运行。

 var dict: Dictionary<String, String> {
        return [
            "book_key": book_key,
            "book_title": book_title,
            "book_author": book_author,
            "book_price": book_price,
            "book_publisher" : book_publisher,
            "page_count": page_count,
            "book_description": book_description,
            "book_urlImage" : book_urlImage,
            "book_urlImage2": book_urlImage2,
            "user_key": user_key,
            "user_name": user_name,
            "user_tag_login" : user_tag_login,
            "onGoingNegotiations" : onGoingNegotiations,
            "other_user_key": other_user_key,
            "other_tag_login": other_tag_login,
            "book_condition": book_condition,
            "timestamp": timestamp
        ]
}

感谢大家的帮助

【讨论】:

    【解决方案2】:

    Swift 3 像这样:

    如果它有多种类型

    var dict: [String: Any] = [
                    "book_key":    true,
                    "book_title":  "Yannick",
                    "book_author": "Test",
                    "book_price":  123
                ]
    

    或者如果你返回很多这样的字符串

    var dict: [String: String] = [
                    "book_key":       "true",
                    "book_author":    "Yannick",
                    "book_price":     "123",
                    "book_publisher": "Yannick",
                    "page_count":     "123"
                ]
    

    【讨论】:

      猜你喜欢
      • 2017-02-14
      • 1970-01-01
      • 1970-01-01
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      • 2017-01-15
      • 2016-03-15
      • 1970-01-01
      相关资源
      最近更新 更多