35

This used to work in Xcode 6: Beta 5. Now I'm getting a compilation error in Beta 6.

for aCharacter: Character in aString {
    var str: String = ""
    var newStr: String = str.append(aCharacter) // ERROR
    ...
}

Error: Cannot invoke append with an argument of type Character

samatron
  • 447
  • 1
  • 4
  • 8
  • Yep. That was the original syntax I used and gave this error: cannot invoke '+' with an argument list of type '(Character, @lvalue String)' – samatron Aug 23 '14 at 02:52
  • `append` returns `Void` and only mutates the string. – Qbyte Aug 16 '15 at 16:50

9 Answers9

44

Update for the moving target that is Swift:

Swift no longer has a + operator that can take a String and an array of characters. (There is a string method appendContentsOf() that can be used for this purpose).

The best way of doing this now is Martin R’s answer in a comment below:

var newStr:String = str + String(aCharacter)

Original answer: This changed in Beta 6. Check the release notes.I'm still downloading it, but try using:

var newStr:String = str + [aCharacter]
Gary Makin
  • 3,109
  • 1
  • 19
  • 27
  • Thanks that worked. I guess I'm suppose to infer that an array of characters is a string? No mention of this specific syntax in release notes for Beta 6. – samatron Aug 23 '14 at 02:54
  • 1
    It could have been clearer but it does say "You can still + two Characters or a String with any other sequence of Character or an Array with any other sequence of the same element type." From that, I decided to make the character into an array with the [] syntax. – Gary Makin Aug 23 '14 at 03:00
  • 8
    `var newStr:String = str + String(aCharacter)` works as well. – Martin R Aug 23 '14 at 04:42
  • Above answer returns `'String' is not identical to 'Uint8'`. I guess the latest official version is different to the beta used by Gary. So the right answer seems to be that of Martin R. – qwerty_so Nov 24 '14 at 07:35
  • I'm not sure what you're doing to get `'String' is not identical to 'Uint8'` but I agree that Martin's answer will be correct in more circumstances. – Gary Makin Jan 20 '15 at 23:33
11

This also works

var newStr:String = str + String(aCharacter)
baskInEminence
  • 710
  • 11
  • 26
9

append append(c: Character) IS the right method but your code has two other problems.

The first is that to iterate over the characters of a string you must access the String.characters property.

The second is that the append method doesn't return anything so you should remove the newStr.

The code then looks like this:

for aCharacter : Character in aString.characters {
    var str:String = ""
    str.append(aCharacter)
    // ... do other stuff
}
Mike Vosseller
  • 4,097
  • 5
  • 26
  • 28
5

Another possible option is

var s: String = ""
var c: Character = "c"
s += "\(c)"
hennes
  • 9,147
  • 4
  • 43
  • 63
2

According to Swift 4 Documentation , You can append a Character value to a String variable with the String type’s append() method:

var welcome = "hello there"

let exclamationMark: Character = "!"
welcome.append(exclamationMark)
// welcome now equals "hello there!"
Gr8Warrior
  • 699
  • 7
  • 11
1
var stringName: String = "samontro"
var characterNameLast: Character = "n"
stringName += String(characterNameLast) // You get your name "samontron"
user3182143
  • 9,459
  • 3
  • 32
  • 39
0

I had to get initials from first and last names, and join them together. Using bits and pieces of the above answers, this worked for me:

  var initial: String = ""

            if !givenName.isEmpty {
                let char = (givenName as NSString).substring(with: NSMakeRange(0, 1))
               let str = String(char)
                initial.append(str)
            }

            if !familyName.isEmpty {
                 let char = (familyName as NSString).substring(with: NSMakeRange(0, 1))
                let str = String(char)
                initial.append(str)
            }
ICL1901
  • 7,632
  • 14
  • 90
  • 138
0

for those looking for swift 5, you can do interpolation.

var content = "some random string"
content = "\(content)!!"
print(content) // Output: some random string!!
Jiraheta
  • 471
  • 7
  • 17
-1
 let original:String = "Hello"
 var firstCha = original[original.startIndex...original.startIndex]

 var str = "123456789"
 let x = (str as NSString).substringWithRange(NSMakeRange(0, 4))

 var appendString1 = "\(firstCha)\(x)" as String!
 // final name
 var namestr = "yogesh"
 var appendString2 = "\(namestr) (\(appendString1))" as String!*
Raksha Saini
  • 604
  • 12
  • 28
Yogesh shelke
  • 438
  • 4
  • 12