简体   繁体   中英

Unexpected non-void return value in void function / swift

extension VoiceController: UITableViewDataSource, UITableViewDelegate {
    public func tableView(_ chatHistory: UITableView, numberOfRowsInSection section: Int) -> Int {
        return userMessagesData.count
    }
    public func tableView(_ chatHistory: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        func userMessage() -> UITableViewCell {
            let userCell = chatHistory.dequeueReusableCell(withIdentifier: "userMessage")! as! UITableViewCell
            userCell.textLabel!.textColor = UIColor(red:0.00, green:0.33, blue:0.62, alpha:1.0)
            userCell.textLabel!.numberOfLines = 0
            userCell.textLabel!.lineBreakMode = .byWordWrapping
            userCell.textLabel!.text = userMessagesData[indexPath.row]
            userCell.textLabel!.textAlignment = .right
            return userCell;
        }
        func botMessage() -> UITableViewCell {
            let botCell = chatHistory.dequeueReusableCell(withIdentifier: "botMessage")! as! UITableViewCell
            botCell.textLabel!.textColor = UIColor(red:1.00, green:0.56, blue:0.25, alpha:1.0)
            botCell.textLabel!.numberOfLines = 0
            botCell.textLabel!.lineBreakMode = .byWordWrapping
            botCell.textLabel!.text = botMessagesData[indexPath.row]
            botCell.textLabel!.textAlignment = .left
            return botCell;
        }
    }
}

That is my code. I need to do something for this code to works. If I'll delete one of functions, that will work I can not understand, how to change my code to work. Please, help

You need

public func tableView(_ chatHistory: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let item = arr[indexPath.row]

    if item.isUser {

        let userCell = chatHistory.dequeueReusableCell(withIdentifier: "userMessage")! as UITableViewCell
        userCell.textLabel!.textColor = UIColor(red:0.00, green:0.33, blue:0.62, alpha:1.0)
        userCell.textLabel!.numberOfLines = 0
        userCell.textLabel!.lineBreakMode = .byWordWrapping
        userCell.textLabel!.text = item.messsage
        userCell.textLabel!.textAlignment = .right
        return userCell
    }
    else {

        let botCell = chatHistory.dequeueReusableCell(withIdentifier: "botMessage")! as UITableViewCell
        botCell.textLabel!.textColor = UIColor(red:0.00, green:0.33, blue:0.62, alpha:1.0)
        botCell.textLabel!.numberOfLines = 0
        botCell.textLabel!.lineBreakMode = .byWordWrapping
        botCell.textLabel!.text = item.message
        botCell.textLabel!.textAlignment = .left
        return botCell
    }
}

struct Item { 
  let isUser:Bool
  let message:String 
}

Where arr

var arr = [Item]()

You need to return the same returning type that you declared in your function,

func botMessage() -> Void //returns void 

while you're returning a UITableViewCell . replace Void with UITableViewCell .

Also i have noticed you have sub function that returns Int remove that function is unnecessary.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM