I am new to iOS. I am currently studying iOS using Objective-C and Swift.
To append a string in Objective-C I am using following code:
NSString *str
SWIFT 2.x
let extendedURLString = urlString.stringByAppendingString("&requireslogin=true")
SWIFT 3.0
From Documentation: "You can append a Character value to a String variable with the String type’s append() method:" so we cannot use append for Strings.
urlString += "&requireslogin=true"
"+" Operator works in both versions
let extendedURLString = urlString+"&requireslogin=true"
Strings concatenate in Swift language.
let string1 = "one"
let string2 = "two"
var concate = " (string1) (string2)"
playgroud output is "one two"
> Swift2.x:
String("hello ").stringByAppendingString("world") // hello world
Add this extension somewhere:
extension String {
mutating func addString(str: String) {
self = self + str
}
}
Then you can call it like:
var str1 = "hi"
var str2 = " my name is"
str1.addString(str2)
println(str1) //hi my name is
A lot of good Swift extensions like this are in my repo here, check them out: https://github.com/goktugyil/EZSwiftExtensions
According to Swift 4 Documentation, String values can be added together (or concatenated) with the addition operator (+) to create a new String value:
let string1 = "hello"
let string2 = " there"
var welcome = string1 + string2
// welcome now equals "hello there"
You can also append a String value to an existing String variable with the addition assignment operator (+=):
var instruction = "look over"
instruction += string2
// instruction now equals "look over there"
You can append a Character value to a String variable with the String type’s append() method:
let exclamationMark: Character = "!"
welcome.append(exclamationMark)
// welcome now equals "hello there!"
You can simply append string like:
var worldArg = "world is good"
worldArg += " to live";