2015-08-31 35 views
7

Funziona correttamente per trasmettere uno Swift String come NSString.La stringa non è convertibile in NSMutableString

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

Ma quando faccio

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

ottengo l'errore

'String' non è convertibile in 'NSMutableString'

Come ho convertirlo?

risposta

16

Non è possibile trasmettere un String come NSMutableString, ma è possibile utilizzare un inizializzatore NSMutableString.

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

ho provato il codice di errore mostra

'NSString' is not a subtype of 'NSMutableString' 

Se si desidera convertire stringa NSMutableString in rapida semplicemente costruendo con NSMutableString (string: ...)

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

Il codice sopra funziona correttamente.

Problemi correlati