Crtx
Crtx

Reputation: 13

NSMutableString append does nothing

I am new to Objective-C after using C++. I got a problem with using NSMutableString during XML file parsing. I have struct with NSMutableString like this:

struct HotelStruct {
...
NSMutableString *h_name;
...
};

Then, I declare CurrentStruct as instance variable of my ParserDelegate:

@interface ParserDelegate : NSObject <NSXMLParserDelegate> {
...
struct HotelStruct CurrentStruct;
...
}

Finally, I am trying to append a string to CurrentStruct.h_name (m_isName == YES) :

-(void)parser:(NSXMLParser*)parser foundCharacters:(NSString *)string {
if (m_isName) {
[CurrentStruct.h_name appendString:string];
}

But debugger says that CurrentStruct.h_name remains 0x0 after executing AppendString as before it, while string variable has the needed value. I am kinda confused because it looks like appendString is just skipped. I'd appreciate any help. Thank you!

Upvotes: 0

Views: 152

Answers (3)

Shamsudheen TK
Shamsudheen TK

Reputation: 31311

It will work fine

 -(void)parser:(NSXMLParser*)parser foundCharacters:(NSString *)string {

     if (m_isName) {

         if(!CurrentStruct.h_name)
             CurrentStruct.h_name = [[NSMutableString alloc] init];

           [CurrentStruct.h_name appendString:string];

           NSLog(CurrentStruct.h_name);
           NSLog(m_isName);
       }
    }

Note:Please check the console logs and Don't forget to release the memory after usage.

Upvotes: 0

Rui Peres
Rui Peres

Reputation: 25917

Is there a reason for not using a standard object instead of structure? That's probably happening because the NSString has not been init/allocated.

Try this:

CurrentStruct-> h_name = [[NSString alloc] init];

Upvotes: 0

giorashc
giorashc

Reputation: 13713

You need to initialize your string object :

h_name = [[NSString alloc] init];

and don't forget to release it when you are done :

[h_name release];

Upvotes: 3

Related Questions