无法下标“String”类型的值,其索引类型为“String”

时间:2016-02-08 06:02:40

标签: ios arrays string swift

我想在tableview单元格上显示一些存储在postFromFriends数组中的名称,但是当我编写下面显示的代码时,它给出了一个错误:“不能下载类型为'String'的值的索引类型'String'“关于名称常量的声明。如果有人可以提供帮助。谢谢

var postsFromFriends = [String]()

这是我追加名字的数组。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! FriendsTaskTableViewCell

    if let name = postsFromFriends[indexPath.row]["name"] as? String {
        cell.name?.text = name
    }
    return cell
}

2 个答案:

答案 0 :(得分:1)

您已将postsFromFriends声明为String的数组,但听起来您想要一个字典数组:

var postsFromFriends = [[String:String]]()

答案 1 :(得分:0)

假设您的错误消息String是一个postsFromFriends[indexPath.row]的数组,那么String会返回["name"],然后您尝试下标postsFromFriends <% @surf_schools.each_with_index do |surf_school, i| %> <div class="average-surf_school_review-rating-<%= i+1 %>" data-score="<%= surf_school.surf_school_reviews.average(:rating).round(2) %>"></div> <% end %> <%= hidden_field_tag 'surf_schools', @surf_schools.count %> <script> if($('#surf_schools').val() > 0){ for (i = 1; i <= $('#surf_schools').val(); i++) { $('.average-surf_school_review-rating-'+i).raty({ readOnly: true, path: '/assets/', score: function() { return $(this).attr('data-score') } }); }; } </script> 。您是否打算将var d = new Date(1454911465467) \\ output : Mon Feb 08 2016 11:34:25 GMT+0530 (IST)数组存储为Dictionaries,或者某个具有可下标名称字段的自定义对象类型?