简体   繁体   English

String不可转换为NSMutableString

[英]String is not convertible to NSMutableString

It works fine to cast a Swift String as an NSString . 它可以很好地将Swift StringNSString

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' 'String'不能转换为'NSMutableString'

How to I convert it? 我该怎么转换呢?

You cannot cast a String as an NSMutableString , but you can use an NSMutableString initializer. 您不能将String NSMutableStringNSMutableString ,但可以使用NSMutableString初始值设定项。

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

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: ...) 如果你想通过简单地用NSMutableString(string:...)构造它来将字符串转换为swift中的NSMutableString

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

Above code works fine. 以上代码工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM