Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String is not convertible to NSMutableString

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?

like image 773
Suragch Avatar asked Aug 31 '15 09:08

Suragch


2 Answers

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

let string = "some text"
let nsMutableString = NSMutableString(string: string)
like image 56
Suragch Avatar answered Nov 12 '22 15:11

Suragch


I tried your code it shows error

   'NSString' is not a subtype of 'NSMutableString'

If you want to convert string to NSMutableString in swift by simply constructing it with NSMutableString(string: ...)

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

Above code works fine.

like image 32
user3182143 Avatar answered Nov 12 '22 16:11

user3182143