7
votes

Comment initialiser Nstring à NsmutableString?

Y a-t-il un moyen d'initialiser Nstring à NsmutableString? et aussi l'ordre inverse?

-(void)st:(NSString *)st
{
  NSMutableString str = st; // gives warning..
  NSLog(@"string: %@", str);
}


0 commentaires

4 Réponses :


4
votes

Vous pouvez définir une nsmutableString sur une nstring, mais pas l'inverse

NSMutableString *mStr = [str mutableCopy];


0 commentaires

4
votes
NSString *someImmutableString = @"something";
NSMutableString *mutableString = [someImmutableString mutableCopy];
Important! mutableCopy returns an object that you own, so you must either release or autorelease it.

0 commentaires

11
votes

nsstring code> est une représentation immuable (ou une vue réadonnée au pire). Donc, vous auriez besoin d'être soit envoyé à un nsmutableString code> si vous savez qu'il est mutable ou crée une copie mutable:

-(void)st:(NSString *)st
{
  NSMutableString *str =  [[st mutableCopy] autorelease];
  NSLog(@"string: %@", str);
}


1 commentaires

Je ne sais pas si cela fonctionnera comment cela se passe, mais le nsmutableString devrait être un pointeur NsmutableString * str = [[ST MutableCopy] Autoréleuillet];



1
votes

SWIFT 2.0

Pour les utilisateurs SWIFT, les travaux suivants dans XCode 7. P>

var str : String = "Regular string"
var mutableStr : NSMutableString = NSMutableString(string: str)


0 commentaires