setString NSMutableString
I am new to programming in XCode, and as a first familiarisation exercise, I would like to make a small Calculator app. So obviously I came upon a few problems: I try to have the user use buttons to type the digits and operators into a string, which he then sees. When I use
displayString = [NSString stringWithFormat:@"%@%@", displayString, operatorString];
everything works fine. Then I decided to work with NSMutableStrings in order to get deletebuttons. I changed the headerfile appropriately, after which I still the earlier NSString functionality (? because NSMutableString inherits from NSString?) And then I got the following problem:
[displayString setString:@"test"];
NSLog(@"%@", displayString);
Even when this is the first method I call, I still get nul. I What am I doing wrong?
Unrelate开发者_StackOverflow社区d, is there a way to have this string write to a sort of function: I would like to be able to do
result = contentof:displayString
or something a like. Any ideas in how I could do this?
NSString
doesn't have a setString:
method. (Documentation)
For this case, you'd need an NSMutableString
. Here's how you'd implement it:
NSMutableString *mutableString = [[NSMutableString alloc] initWithFormat:"%@%@", displayString, operatorString];
... and then to change it:
[mutableString setString:@"test"];
精彩评论