String is not convertible to NSMutableString

Suragch picture Suragch · Aug 31, 2015 · Viewed 7.7k times · Source

It works fine to cast a Swift String as an NSString.

let string = "some text"
let nsString = string as NSString

But when I do

let string = "some text"
let nsMutableString = string as NSMutableString

I get the error

'String' is not convertible to 'NSMutableString'

How to I convert it?

Answer

Suragch picture Suragch · Aug 31, 2015

You cannot cast a String as an NSMutableString, but you can use an NSMutableString initializer.

let string = "some text"
let nsMutableString = NSMutableString(string: string)