Search code examples
iosswiftuikitstring-concatenationstring-interpolation

String interpolation or concatenation does not seem to work (swift)


I have this little bit of code in a table view data source function func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell:

cell.textLabel?.text = "\(playerSorted[indexRow.path].username): \(playerSorted[indexRow.path].kills)"

in which .username is a string and .kills is an int. For some reason, only the username manages to show up in the label. I've tried casting kills as a string, I've tried using concatenation:

cell.textLabel?.text = playerSorted[indexRow.path].username + ": " + String(playerSorted[indexRow.path].kills

but nothing shows up after .username, not even the colon. I've tried putting stuff before .username, and that manages to show up for some reason, like so:

cell.textLabel?.text = "Player \(playerSorted[indexRow.path].username): \(playerSorted[indexRow.path].kills)"

or

cell.textLabel?.text = "Player " + playerSorted[indexRow.path].username + ": " + String(playerSorted[indexRow.path].kills

In these cases, "Player " and .username show up in the label on screen. Any advice to help with this issue would be great, I've been stuck on it for a while. Thanks for your time.

Full collectionViewCell class:

import UIKit

class CollectionViewCellRankings: UICollectionViewCell {
    

    
    @IBOutlet weak var tableViewInGame: UITableView!
    
    
    var teamsSorted = [Team]()
    var playersInYourTeam = [Player]()
    var playersSorted = [Player]()
    
    var cellNumber = 0
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
}

extension CollectionViewCellRankings: UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch cellNumber {
        case 0:
            return playersSorted.count
        case 1:
            return teamsSorted.count
        case 2:
            print("creating second cell")
            return playersInYourTeam.count
        default:
            return 0
            
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "RankingCellTV")!
        cell.backgroundColor = UIColor.systemGray2
        
        switch cellNumber {
        case 0://FFA

            print("Kills:")
            print(playersSorted[indexPath.row].kills)

//Line that is causing the problems
            cell.textLabel?.text = "\(playersSorted[indexPath.row].username): \(playersSorted[indexPath.row].kills)"

        case 1://Team rankings
            if Game.teamSetting > 0 {
//This line of interpolation actually works! (no idea why)
                cell.textLabel?.text = "Team \(String(teamsSorted[indexPath.row].team!)): \(String(teamsSorted[indexPath.row].score!))"
                switch teamsSorted[indexPath.row].team {
                case 1:
                    cell.backgroundColor = UIColor.red
                case 2:
                    cell.backgroundColor = UIColor.blue
                case 3:
                    cell.backgroundColor = UIColor.green
                case 4:
                    cell.backgroundColor = UIColor.purple
                case 5:
                    cell.backgroundColor = UIColor.orange
                case 6:
                    cell.backgroundColor = UIColor.cyan
                case 7:
                    cell.backgroundColor = UIColor.yellow
                case 8:
                    cell.backgroundColor = UIColor.magenta
                default:
                    cell.backgroundColor = UIColor.systemBlue
                }
            }
        case 2://Players in team
            print("printing team player")
            cell.textLabel?.text = "\(playersInYourTeam[indexPath.row].username) | K: \(playersInYourTeam[indexPath.row].kills) D: \(playersInYourTeam[indexPath.row].deaths)"
        default:
            break
            
        }
        
        return cell
    }
        
    
    
    
}

UIViewController class collection view delegate and data source methods:

extension InGameVC: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if Game.teamSetting > 0 {
            return 2
        } else {
            return 1
        }
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = inGameCV.dequeueReusableCell(withReuseIdentifier: "RankingCell", for: indexPath) as! CollectionViewCellRankings
        cell.backgroundColor = UIColor.systemGray2
        
        if indexPath.row == 0 {
            cell.tableViewInGame.backgroundColor = UIColor.systemGray2
            if Game.teamSetting > 0 {
                print(indexPath.row)
                cell.cellNumber = indexPath.row + 1
                cell.playersInYourTeam = playersInYourTeam
                cell.teamsSorted = teamsSorted
            } else {
                cell.cellNumber = 0
                cell.playersSorted = playersSorted
            }
            
        }
        cell.tableViewInGame.delegate = cell
        cell.tableViewInGame.dataSource = cell
       // cell.tableViewInGame.separatorStyle = UITableViewCell.SeparatorStyle.none
        cell.tableViewInGame.reloadData()
        return cell
    }
    
    

}

Player and Team are both structs.


Solution

  • So, check precisely playerSorted[indexRow.path].username. It may end with a line feed. Then you don't see the kills (nor the semi colon by the way).