I have the following code. When it runs, it triggers the error : Cannot subscript a value of type '[String : Int]' with an index of type 'String?'
Can someone please help
self.books = self.books.filter { $0.completed[MUser.sharedInstance.getUserId()] ?? 0 > 0 }
completed is declared as
var completed = [String : Int]()
The error indicates, that MUser.sharedInstance.getUserId()
returns an optional.
You need to unwrap this:
self.books = self.books.filter {
guard let userID = MUser.sharedInstance.getUserId() else { return false }
return $0.completed[userID] ?? 0 > 0
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.