ios - 转到详细 View Controller

标签 ios swift xcode firebase

我有一个带有 Collection View 的 View Controller ,当我单击 Collection View 的单元格时,我想转入一个 Controller ,该 Controller 包含我单击的单元格的用户拥有的所有帖子。

var posts = NSMutableArray()

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "UsersProfile" {
   let vc = segue.destination as! UsersProfileViewController
   let postsForUser = posts.filter {
       guard let post = $0 as? [String : Any] else { 
          return false 
       }  
       return post["username"] as? String == selectedUserName 
   }
   print("postForUser = \(postsForUser)")
   vc.posts = postsForUser as! NSMutableArray
  }
}

当我使用 xcode 8 时,它工作得非常好,当我更新到 xcode 9 时,当我单击单元格时它崩溃了,我不确定如何识别 xcode 9 中的问题。

最佳答案

post 字典包含带有“username”键的值,它是一个 String 类型,因此过滤器将返回 Dictionary<String, Any> 的数组。或 [[String: Any]] 不是 NSMutableArray。还有 NSMutableArray是 Objective-C 集合类型,在 Swift 中没有使用。所以 ProfileViewController 中的帖子应该是 [[String: Any]] 类型

let postsForUser: [[String: Any]] = posts.filter {
   guard let post = $0 as? [String : Any] else { 
      return false 
   }  
   return post["username"] as? String == selectedUserName 
}
print("postForUser = \(postsForUser)")
vc.posts = postsForUser //vc.posts should be [[String: Any]]

关于ios - 转到详细 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46701241/

相关文章:

ios - 计数发射方法

ios - 数据源更新后,Rx TableView 不会重新加载

swift - 从字符串中获取直到字符串索引末尾的子字符串

ios - 如何在单击时为单元格添加边框

ios - FetchedResults 崩溃 : Dispatch queue: com. apple.main-thread (0)

iphone - 多 View 应用程序在模拟器中显示空白屏幕

ios - 获取 UIViewController View 的正确边界

iphone - 如何将以前存档的应用程序从 xcode 管理器安装到我的 iphone

ios - iOS7 和 iOS8 返回的 UIScreen 边界不同

ios - 一维条码扫描器 IOS (Xcode)