Suragch
Suragch

Reputation: 512676

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?

Upvotes: 13

Views: 8446

Answers (2)

Suragch
Suragch

Reputation: 512676

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

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

Upvotes: 26

user3182143
user3182143

Reputation: 9609

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.

Upvotes: 1

Related Questions