无法使用索引类型为“String”的类型'[String:AnyObject]'下标值

编程入门 行业动态 更新时间:2024-10-26 23:39:34
本文介绍了无法使用索引类型为“String”的类型'[String:AnyObject]'下标值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试从JSON内容(在我的data.swift文件中)获取一些数据并将其分配给comments。任何人都知道这里出了什么问题以及如何解决它?看起来像是一个我遇到问题的语法问题。

I'm trying to get some data from a JSON content(in my data.swift file) and assign it to "comments". Anyone know whats going wrong here and how I can fix it? Seems like a syntax issue that I'm having trouble with.

我得到的错误:

The error I am getting:

import UIKit class CommentsTableViewController: UITableViewController { var story = [String:AnyObject]() var comments = [String:AnyObject]() override func viewDidLoad() { super.viewDidLoad() comments = story["comments"] tableView.estimatedRowHeight = 140 tableView.rowHeight = UITableViewAutomaticDimension }

它不喜欢 comments = story [注释] part。

推荐答案

您的代码中有错误,但错误消息是您由于Swift编译器错误,我看到的是不正确和误导。实际的错误消息应为: AnyObject不能转换为[String:AnyObject] 。

There is an error in your code, but the error message you're seeing is incorrect and misleading due to a Swift compiler bug. The actual error message should read: AnyObject is not convertible to [String:AnyObject].

self.story [comments] 返回 AnyObject 。要将该值分配给 selfments ,您必须首先将 AnyObject 强制转换为字典类型 [字符串:AnyObject] 。

self.story["comments"] returns an AnyObject. To assign that value to selfments you must first typecast AnyObject to the Dictionary type [String:AnyObject].

例如:

selfments = self.story["comments"] as! [String:AnyObject]

更多推荐

无法使用索引类型为“String”的类型'[String:AnyObject]'下标值

本文发布于:2023-11-26 16:40:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634409.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   下标   索引   String   AnyObject

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!