NSXMLParser init Error message.

Discussion in 'iOS Programming' started by CBX, Jan 20, 2012.

  1. CBX macrumors regular

    Joined:
    May 15, 2007
    #1
    Hi:

    Im using Xcode 4 to parse an xml string in to a custom object. Ive followed a couple of online examples and can get it to work.

    However, when I follow them I get an error with one of the lines of code. If I comment it out it works, but I'm concerned (as I'm new to this) that perhaps it will cause a problem down the line...

    My problem is in initXML and the line is [super init];

    With this in I get error: Result of delegate init call must be immediately returned or assigned to self.

    If I comment it out it works.

    Can someone explain, as I can't see a difference between examples online and my code...

    .h
    Code:
    #import <Foundation/Foundation.h>
    
    @class MyRecord;
    
    @interface XMLParser : NSObject
    {
        NSMutableString *currentElementValue;
        MyRecord *record;
        NSMutableArray *records;
    }
    
    @property (nonatomic, retain) MyRecord *record;
    @property (nonatomic, retain) NSMutableArray *records;
    
    - (XMLParser *) initXMLParser;
    
    @end
    
    .m
    Code:
    #import "XMLParser.h"
    #import "MyRecord.h"
    
    @implementation XMLParser
    
    @synthesize record, records;
    
    - (XMLParser *) initXMLParser 
    {
    //    [super init];
        
        records = [[NSMutableArray alloc] init];
        return self;
    }
    
     
  2. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #2
    What does this error tell you ? Have you tried, to paraphrase "immediately returning" the result or "assigning it to self" ?
     
  3. CBX thread starter macrumors regular

    Joined:
    May 15, 2007
    #3
    No, as thats not what I'm asking.

    I can do what the error says as you suggest but I would like to understand why almost all the examples I have seen look to have the code as I do without an error!

    I don't just want to do something without understanding otherwise I will never learn and keep asking such questions...

    So, can you be of any help?

    ----------

    Here is one of the examples I looked at: here

    Can anyone explain why I get an error as described above even though I have seen variations on this code above all doing the init method the same way?

    Is it to do with changes introduced in Xcode 4 and I'm looking at an old way which doesn't work when using ARC or something?
     
  4. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #4
    It has nothing to do with ARC.

    Remember, XCode 4 switches to LLVM instead of GCC as the default compiler. Different compilers implement the language standards differently when the standard is not quite clear on something (this is called undefined behavior, you can read about what that means in the comp.lang.c FAQ :

    http://c-faq.com/ansi/undef.html

    )

    But frankly, think about what [super init] does and how it works and then think about how your own init method, what it does and it works.

    Would you ever write this code :

    Code:
    [[XMLParser alloc] initXMLParser];
    
    Or would you rather go for something like :

    Code:
    XMLParser * parser = [[XMLParser alloc] initXMLParser];
    
    Why did you answer the way you did ? That should tell you why LLVM probably flags [super init] by itself as an error. Where is the return value going ? What was the point of calling the method ?
     
  5. CBX thread starter macrumors regular

    Joined:
    May 15, 2007
    #5
    I am using:

    Code:
    XMLParser * parser = [[XMLParser alloc] initXMLParser];
    (as per the code example I was following).

    [super init] I believe would call the init method of the superclass of the class I am in.

    So the intention would be to call any super class initialisation and then to perform my own initialisation such as initialise my array and return it to the caller.

    Im struggling to get a handle on this.

    Appreciate your help with this as I said I want to understand what is wrong with my code even thought I can make it work, but I'm missing something (thats probably obvious).
     
  6. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #6
    Why do you assign the result of your own init method to a variable, but the init method of the super class woulldn't be assigned to a variable ?

    Think about this little point.
     
  7. CBX thread starter macrumors regular

    Joined:
    May 15, 2007
    #7
    Ok I think I get that your saying:

    My init returns a value which I assign to a var

    yet

    when I call the superclass init I don't assign it to a var an this is what the compiler is saying when it says:

    Result of delegate init call must be immediately returned or assigned to self

    If I have understood this then I'm fine with this as I should either

    return the result of the super call to assign to a var
    or
    assign it to self (as per the message)

    But if thats the case, I still don't understand why I see many examples using the code this way but not doing what I say above.

    Perhaps I think I understand but don't?
     
  8. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #8
  9. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #9
    Refer to my paragraph about undefined behavior above. Technically, when calling [super init], you're doing it from within an allocated instance already. That means that "self" in the super class should refer to the same "self" as in your own init method.

    However, this is probably not well defined and thus becomes implementation specific. This is why LLVM and GCC (the examples on the net are mostly based on GCC as LLVM is a new thing really) have different behavior on compiling such codes.
     
  10. CBX thread starter macrumors regular

    Joined:
    May 15, 2007
    #10
    Thanks, I think I have it now. The compiler just needs me to be specific and hence the need to assign to self or return directly from my super call.

    Thanks again.
     
  11. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    It has always been wrong to not assign the results of [super init] to self but the compiler has only begun complaining about it recently. That, in part, is why you see wrong code in examples.
     
  12. CBX thread starter macrumors regular

    Joined:
    May 15, 2007
    #12
    Thats what was confusing me, thanks.
     

Share This Page