Problem with NSString
I have done this many times before and never got a issue but i have got a problem now i just cant solve.
I have a NSMutableString declared in the H File. Named TheString. I have a IBOutlet of a UItextfield declared named TextField In viewdidload i fill this string with some text. That goes fine and when i NSLOG it everything works.
Then o开发者_如何学运维n a button press i do:
TheString = [TheString stringByReplacingOccurrencesOfString:@"And"
withString:TextField.text];
I have done this loads of times and it always works. However this time i build the app with no errors and press the button and it crashes. I checked the console and there are no errors when it crashes. EDIT
In the H File i have this also:
@property(nonatomic, retain) NSMutableString *TheString;
And in viewdidload i do this
TheString = [NSMutableString alloc];
And i synthesize I give it some text and NSLOG it and it is all working. If anyone could please help me with this it would be great!
Thanks
Have you allocated TheString
.
Try this
TheString = [[NSMutableString alloc] initWithString:@"Some text here"];
TheString = [TheString stringByReplacingOccurrencesOfString:@"And" withString:TextField.text];
Make sure you release
it when you are done using it
This TheString = [NSMutableString alloc];
should not be done. Change it to
TheString = [[NSMutableString alloc] init];
and after button event call
[TheString replaceOccurrencesOfString: @"And"
withString: TextField.text options: 0 range: NSMakeRange(0, [receiver length])];
精彩评论