The string is not converted to NSMutableString

It works great to pass 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 an error

'String' does not convert to 'NSMutableString'

How to convert it?

+7
string ios swift nsmutablestring
source share
2 answers

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

 let string = "some text" let nsMutableString = NSMutableString(string: string) 
+16
source share

I tried your code, it shows an error

  'NSString' is not a subtype of 'NSMutableString' 

If you want to convert a string in NSMutableString to swift just by creating it using NSMutableString (string: ...)

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

It works fine on code.

+1
source share

All Articles