override func collectionView(collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
let msg : Mesages = fetchedResultsControler.objectAtIndexPath(indexPath) as! Mesages
switch (msg.fromID!) {
case(friend!.id!) : let messageData = JSQMessage(senderId: friend!.id!, displayName: friend!.id!, text: msg.text)
return messageData
default :
let messageData = JSQMessage(senderId: userdefaults.objectForKey("FBid") as! String, displayName: userdefaults.objectForKey("FBid") as! String, text: msg.text)
return messageData
}
}
我需要安全地解开msg.fromID!
和friend!.id!
var friend: Friend?
extension Friend {
@NSManaged var id: String?
}
extension Mesages {
@NSManaged var fromID: String?
@NSManaged var text: String?
}
答案 0 :(得分:1)
if (friend?.id) != nil {
switch (msg.fromID) {
case(friend!.id!)? :
_ = friend!.id
default :
_ = "FBid"
}
}
对于切换部分,您可以按case value?
- >匹配如果msg.fromID不是可选的,则解析为true。
对于friend.id,我想你可能需要在交换机外检查它。也许其他人可以为friend.id提供更好的解决方案。