views:

566

answers:

3

I have written an Objective-C framework which builds some HTML code with NSMutableString which returns the value as an NSString.

I have declared an NSString and NSMutableString in the inteface .h file:

NSString *_outputLanguage;        // Tests language output
NSMutableString *outputBuilder;
NSString *output;

This is a sample from the framework implementation .m code (I cannot print the actual code as it is proprietary):

-(NSString*)doThis:(NSString*)aString num:(int)aNumber {
if ([outputBuilder length] != 0) {
    [outputBuilder setString:@""];
}
if ([_outputLanguage isEqualToString:@"html"]) {
    [outputBuilder appendString:@"Some Text..."];
    [outputBuilder appendString:aString];
    [outputBuilder appendString:[NSString stringWithFormat:@"%d", aNumber]];
}
else if ([_outputLanguage isEqualToString:@"xml"]) {
    [outputBuilder appendString:@"Etc..."];
}
else {
    [outputBuilder appendString:@""];
}
output = outputBuilder;
return output;
}

When I wrote a text program, NSLog simply printed out "(null)". The code I wrote there was:

TheClass *instance = [[TheClass alloc] init];
NSString *testString = [instance doThis:@"This String" num:20];
NSLog(@"%@", testString);
[instance release];

I hope this is enough information!

+2  A: 

I'm guessing that you're forgetting to alloc/init your strings...

Dave DeLong
To give an example: have you at any point in your .m file got a line like this: `outputBuilder = [[NSMutableString alloc] init];`If not, you need it.
JeremyP
Thanks! I forgot to alloc/init - I've added a custom init method to take care of this.
BWHazel
A: 

Your doThis: method doesn't seem to initialise outputBuilder. So if it is a null pointer, then nothing will be done to it.

Paul Lynch
Uh... `doThis:` is an instance method, and it's being invoked on `instance`, so it would seem that `instance` was indeed initialized... I don't really get what you're saying.
Dave DeLong
maybe he meant `outputBuilder`?
Erich Mirabal
Sorry, yes. Edited.
Paul Lynch
Thanks! I've added a new init method to take care of allocating and initialising.
BWHazel
A: 

Make sure outputBuilder is valid. Where are you alloc/init'ing it?

Erich Mirabal
Thanks! I forgot to actually alloc/init! I have created a new init method to take care of this.
BWHazel