null returned by NSMutableString
I have an NSMutableString which I append other strings to.
When I print the String I get null.
in the .h file
NSMutableString *name;
@property(nonatomic, retain) NSMutable开发者_如何学CString *name;
in the .m file
@synthesize name;
-(IBAction) a {
if (uppercase == 1) {
[name appendString:@"A"];
NSLog(@"name: %@", name);
}
else
[name appendString:@"a"];
NSLog(@"name: %@", name);
}
I get the following print out when triggering the action.
name: (null)
Why is this returning null?
My guess is that you never initialized it. Something like:
- (id)init {
// Standard init code here...
self.name = [NSMutableString stringWithString:@""];
}
... to ensure that you have a valid object to work with?
Are you setting the value of 'name' anywhere in your code? In the code you've pasted in your question, 'name' never gets set. You can't append a string to something that doesn't exist - you're effectively trying to append a value to null, which is why nothing gets returned.
So I guess my answer would be: based on the code you've shown, name never has anything assigned to it. If you are assigning a string to 'name', your problem is elsewhere, but you'd need to post the code for the assignment etc.
精彩评论