How to convert Int to String in Swift? All ways to convert a number to a string
Hello everyone! I just started learning Swift and encountered a simple but confusing task for me. I need to convert an integer (Int) to a string (String) to display it in a UILabel or combine it with other text.\n\nHere's an example of my code:\n\n```swift\nlet score = 42\nlet message = "Your score: " + score // Error!\n```\n\nXcode gives an error: "Binary operator '+' cannot be applied to operands of type 'String' and 'Int'". I understand that Swift is a strictly typed language, and you cannot directly add a string and a number. But how do I correctly convert Int to String?\n\nI tried writing `String(score)`, and it seems to work. But are there other ways? For example, what about formatting — if I need to add leading zeros or display a number with spaces between digits? Or how to display
No answers yet.
Log in to leave an answer