Is there any way to initialize NSString to NSMutableString? and also reverse order?
-(void)st:(NSString *)st
{
  NSMutableString str = st; // gives warning..
  NSLog(@"string: %@", str);
}
                NSString is an immutable representation (or a readonly view at worst).  So you would need to either cast to a NSMutableString if you know it's mutable or make a mutable copy:
-(void)st:(NSString *)st
{
  NSMutableString *str =  [[st mutableCopy] autorelease];
  NSLog(@"string: %@", str);
}
I autoreleased it because mutableCopy returns a newly initialized copy with a retain count of 1.
NSString *someImmutableString = @"something";
NSMutableString *mutableString = [someImmutableString mutableCopy];
Important! mutableCopy returns an object that you own, so you must either release or autorelease it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With