0

I want to append a unichar to NSMutableString. I have

var outputString: NSMutableString = "Some string" 
let letterA: unichar = 0x1820

I saw the question Appending unichar to NSMutableString. The accepted answer (in Objective C) says:

[s appendFormat:@"%C", c];

but I can't figure out the Swift syntax. The documentation doesn't help much, either.

I even tried

outputString += letterA

but that also didn't work.

Note: I'm not trying to append a Swift String or append a Swift Character.

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393

3 Answers3

1

outputString.appendFormat("%C", letterA) appends the character in Swift 1.2 running in Xcode 6.4. It appears to be the same method you've referenced in the Objective-C sample.

Rikki Gibson
  • 4,136
  • 23
  • 34
1

Use the same method appendFormat in swift like this

Rewriting your code to elaborate

    var outputString: NSMutableString = "Some string"
    let letterA: unichar = 0x1820
    outputString.appendFormat("%C", letterA)
Nofel Mahmood
  • 898
  • 7
  • 12
  • 1
    Why couldn't I figure this out? Thank you! Both answers (this and @rikkigibson) are correct and were added at almost the same time. I will mark this one as the accepted since code is a little easier to read and test. +1 to both – Suragch Jul 12 '15 at 05:04
0

NSMutableString

The NSMutableString reference does not need to be a var

let outputString:NSMutableString = "Some string"
outputString.appendFormat("%C", 0x0041) // Letter A

Native Swift String

Solution skipping NSFoundation and unichar classes:

var outputString = "Some string"
outputString += "\u{41}" // Letter A is actually 0x0041
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
  • All of these are Swift questions and answers. Your answer is the way most people would do it in Swift, but I had a specific need to use `NSMutableString`, not `String`, because of the way `String` treats characters. See my [previous question](http://stackoverflow.com/questions/31272561/working-with-unicode-code-points-in-swift). – Suragch Jul 12 '15 at 15:27
  • Correct. Then solution using `NSMutableString` shall be `let`. – SwiftArchitect Jul 12 '15 at 18:12
  • That's interesting about `NSMutableString` not needing to be a `var`. Where is your quote from? I tried searching for it but couldn't find it. – Suragch Jul 13 '15 at 02:47