BWHazel
BWHazel

Reputation: 1474

How do I convert an NSMutableString to NSString when using Frameworks?

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!

Upvotes: 0

Views: 6111

Answers (3)

Paul Lynch
Paul Lynch

Reputation: 19789

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

Upvotes: 0

Erich Mirabal
Erich Mirabal

Reputation: 10038

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

Upvotes: 0

Dave DeLong
Dave DeLong

Reputation: 243156

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

Upvotes: 3

Related Questions