错误 - 条件绑定的初始化程序必须具有可选类型,而不是'字符串' - 我的情况

时间:2017-02-21 12:03:37

标签: swift

import UIKit

class ViewController: UIViewController , UICollectionViewDelegate ,UICollectionViewDataSource, UITextFieldDelegate {

    struct mess {
        let user : String
        let textmessage : String
        var cellId : String
        let view = UIView()
    }

    var message = [mess]()


    @IBOutlet weak var messageCollection: UICollectionView!
    @IBOutlet weak var textFieldZone: UITextField!
    @IBAction func user1Action(_ sender: Any) {
        addMessage(user: "", cell: "customCell")
    }
    func addMessage( user : String , cell : String){
        let text = textFieldZone.text
        let mymessage = mess(user: user, textmessage: text!, cellId: cell)
        message.append(mymessage)
        self.messageCollection.reloadData()
        self.textFieldZone.text = " "
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return message.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let currentMessage = message[indexPath.item]

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: currentMessage.cellId, for: indexPath) as! SimpleCell1
        cell.text.text = currentMessage.textmessage
        return cell

    }

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        if let messageText = message?[indexPath.item].textmessage {
            let size = CGSize(width: 250, height: 1000)
            let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
            let estimatedFrame = NSString(string: messageText).boundingRect(with: size, options: options, attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 18)], context: nil)

            return CGSize(width: view.frame.width, height: estimatedFrame.height + 20)
        }

        return CGSize(width: view.frame.width, height: 100)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(8, 0, 0, 0)
    }
}

错误符合"如果"。

出了什么问题?

我删除了?,但它没有帮助。

错误是"条件绑定的初始化程序必须具有可选类型,而不是'字符串'"在func collectionView:layou:sizeForItemAt 方法中的if语句行上

1 个答案:

答案 0 :(得分:0)

基于您在消息上添加的代码,错误是由于message不是可选的,消息[indexPath.item]不是可选的以及消息[indexPath.item] ] .textmessage不是可选的。

if let语句需要在equals符号的右侧有一个可选项。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let messageText = message[indexPath.item].textmessage
    let size = CGSize(width: 250, height: 1000)
    let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
    let estimatedFrame = NSString(string: messageText).boundingRect(with: size, options: options, attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 18)], context: nil)

    return CGSize(width: view.frame.width, height: estimatedFrame.height + 20)
}

如果您不确定该邮件是否包含索引,请检查message.count

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    if message.count > indexPath.item {
        let messageText = message[indexPath.item].textmessage
        let size = CGSize(width: 250, height: 1000)
        let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
        let estimatedFrame = NSString(string: messageText).boundingRect(with: size, options: options, attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 18)], context: nil)

        return CGSize(width: view.frame.width, height: estimatedFrame.height + 20)
    }
    return CGSize(width: view.frame.width, height: 100)
}