Concatening Strings

I have

textView.text="The current working directory is " + cwd()

Why is this flagged as error? cwd is a func->String

Is there a Swift Beginners' forum topic?

Based on your other thread, this is not entirely true. Your function returns an optional String I think, and you cannot concatenate a String with an optional String like this.

You could do something like

textView.text = "The current working directory is " + (cwd() ?? "nil")

or

textView.text = "The current working directory is \(cwd() ?? "nil")"

Both these variants will provide a default value for the output of cwd() and hence this will always produce a String, and not just the optional String?. Then the concatenation works.

No there isn't. If you have any questions about the Swift language, feel free to ask under "Using Swift" and don't worry if they are "beginners questions". Every single person started out as a beginner.
If you are just starting out with Swift, I highly recommend you read through the official https://docs.swift.org/swift-book/. This explains all the basics and potentially helps you get up and running faster.

If you have questions about Apple's frameworks (e.g. UIKit, etc…) then you will get answers faster on https://stackoverflow.com, as this forum is about the programming language itself.

2 Likes
Terms of Service

Privacy Policy

Cookie Policy