I have an issue in getting a string that has been converted from a float to display in a label in a custom cell in a UITableView.
The program will crash if I try to assign the value of the float to the label. If I leave "strAIAreaIncrease = @"6.5%";" uncommented, the program works fine - but without the calculated value.
strAIAreaIncrease = [NSString stringWithFormat:@"%.2f",fltAreaIncrease];
strAIAreaIncrease = [strAIAreaIncrease stringByAppendingString:@"%"];
DebugLog(@"The value of float num is %.2f", fltAreaIncrease);
DebugL开发者_Python百科og(@"The value of the string is %@", strAIAreaIncrease);
// strAIAreaIncrease = @"6.5%";
The Debugger Console shows the following:
2010-11-14 19:52:38.122 Building Use[855:207] The value of float num is 2.50 2010-11-14 19:52:38.123 Building Use[855:207] The value of the string is 2.50%
I have used this format on other custom cell labels without issue. Anyone have any idea as to what is going on?
Thanks.
I think you should use NSMutableString instead:
e.g.
NSMutableString* straIAreaIncrease
= [NSMutableString stringWithFormat:@"%.2f", fltAreaIncreas];
[strAIAreaIncrease appendString:@"%%"];
OK, I have tried to implement NSMutabeString without success. It is obvious that I am missing a fundemental understanding of the MSMutableString.
I think that the way that I am creating it is part of the problem. I need to be able to pass the variable to several methods, so I am trying to create it in the header. It seems all of the examples I come across create the string within a method.
In my header I have:
NSMutableString *strAIAreaIncrease;
I think that this is a pert of the problem and I think that I need to assign it a value, but am not sure how. The Debugger Console gives the following:
2010-11-21 07:40:40.456 Building Use[1274:207] *** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '+[NSMutableString string:]: unrecognized selector sent to class 0x2317c0'
Another question is this: If I am then using the MSMutableString value in an array, does the array have to be an MSMutable Array?
Thanks.
The problem is not in the header. It seems that you are using
strAIAreaIncrease = [NSMutableString string:@"someString"];
But there is no method "string:" in the Class NSMutableString. Try to use
strAIAreaIncrease = [NSMutableString stringWithString:@"someString"];
or
strAIAreaIncrease = [NSMutableString stringWithFormat:@"someString %d", someInteger];
About your second question: No you don't have to use a NSMutableArray, when dealing with NSMutableString. The "Mutable" in NSMutableArray means that you can add and remove objects from the array after it is generated.
精彩评论