简体   繁体   English

即时修复类型“ NSFastEnumerationIterator.Element”(又名“ Any”)没有下标成员

[英]Hot to Fix Type 'NSFastEnumerationIterator.Element' (aka 'Any') has no subscript members

Trying to get post's 'title','content', categorie's 'title' & author name out of this JSON. 尝试从此JSON中获取帖子的“标题”,“内容”,类别的“标题”和作者姓名。 getting Error Type 'NSFastEnumerationIterator.Element' (aka 'Any') has no subscript members . 得到错误Type 'NSFastEnumerationIterator.Element' (aka 'Any') has no subscript members printing post in console works fine but getting error while trying to get title of post. 在控制台中打印帖子可以正常工作,但是在尝试获取帖子标题时出错。 Please help. 请帮忙。 JSON & Swith Code is JSON和Swith代码为

JSON JSON格式

{  
   "status":"ok",
   "count":1,
   "count_total":44,
   "pages":44,
   "posts":[  
      {  
         "id":87,
         "url":"http://www.website.com/blogs/my first blog/",
         "title":"My First Blog",
         "content":"blog content",
         "date":"2015-04-06 22:42:12",
         "modified":"2015-12-26 00:45:09",
         "categories":[  
            {  
               "id":45,
               "title":"Trip",
               "description":"",
               "post_count":21
            }
         ],
         "author":{  
            "id":1,
            "name":"admin",
            "url":"",
            "description":"hello"
         }
      }
   ]
}

Swift Code SWIFT代码

            if let blogContent = data {

                do {

                    let jsonResult = try JSONSerialization.jsonObject(with: blogContent, options: JSONSerialization.ReadingOptions.mutableContainers)

                    if let items = jsonResult as? [NSString:Any] {

                        //print(items)

                        let item = items["posts"] as! NSArray

                        for post in item {

                            print(post)

                            print(post["title"])

                        }

                    }
                } catch {

                    print("JSON processing failed.")
                }

            }

Got it working. 得到它的工作。 Here is the working code. 这是工作代码。 Hope it can help someone with same problem. 希望它可以帮助遇到同样问题的人。 Thanks :) 谢谢 :)

if let blogContent = data {

                do {

                    let jsonResult = try JSONSerialization.jsonObject(with: blogContent, options: JSONSerialization.ReadingOptions.mutableContainers)

                    if let items = jsonResult as? [String: AnyObject] {

                        if let item = items["posts"] as? NSArray {

                            for posts in item {

                                if let post = posts as? [String: AnyObject] {

                                    print(post["title"]!)

                                    let categories = post["categories"] as! NSArray

                                    for category in categories {

                                        if let categ = category as? [String: AnyObject] {

                                            print(categ["title"]!)

                                        }
                                    }

                                    let author = post["author"] as! [String: AnyObject]

                                    print(author["name"]!)

                                }

                            }

                        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 键入any没有下标成员swift 3 - Type any has no subscript members swift 3 类型'[String:Any]'在Swift中没有下标成员 - Type '[String:Any]' has no subscript members in Swift 错误:类型'((键:字符串,值:任何)''没有下标成员带有json - Error: Type '(key: String, value: Any)' has no subscript members with json 将语法转换为swift 3时,类型'Any'没有下标成员吗? - When convert syntax to swift 3, Type 'Any' has no subscript members? 错误:类型“ Any”没有下标成员带有Swift 4的AlamoFire - Error: Type 'Any' has no subscript members AlamoFire with Swift 4 Alamofire 4.0出现“类型'任何'没有下标成员”错误 - “Type 'Any' has no subscript members” error on Alamofire 4.0 “输入‘有吗?’ 尝试从嵌套的 JSON 字典初始化对象时,没有下标成员”错误 - "Type 'Any?' has no subscript members " error when trying to initialize objects from a nested JSON dictionary JSON解析错误-类型'RecentTvListData'没有下标成员 - JSON Parsing error - Type 'RecentTvListData' has no subscript members 获取本地 JSON 数据:该元素隐式具有类型“any” - Get local JSON data: The element has a type "any" implicitly 无法用索引类型“ String”下标“ [[[String:Any]]””的值 - Cannot subscript a value of type '[[String : Any]]' with an index of type 'String'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM