Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6210793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:13:25+00:00 2026-05-24T06:13:25+00:00

I have a XML parser which will parse 17 different XML documents (I’m simplifying

  • 0

I have a XML parser which will parse 17 different XML documents (I’m simplifying this).
When the parser has finished its job, it calls the object that did the request.

First way

A single method that looks like

- (void)didReceiveObject:(NSObject *)object ofType:(MyObjectType)type

with MyObjectType being an enum.

In this method, I check the type and redirect the object to the corresponding method.

Second way

There is a callback method for each of the 17 types of object I can receive.

- (void)didReceiveFoo:(MYFoo *)foo
- (void)didReceiveBar:(MYBar *)bar
... and so on

Which way of using delegates will be better?
We had a discussion about this with a colleague and couldn’t find one way more appealing than another. It seems like it’s just deciding what method to call from the parser or within the delegate….

Even when thinking about adding future methods/delegates callbacks, we don’t see any real problem.

Is one of these ways better than the other? Is there another way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:13:27+00:00Added an answer on May 24, 2026 at 6:13 am

    First method:

    Pros:

    • More flexible to future changes.

    Cons:

    • May result in a large switch statement or messy if … else if … else statement.
    • Probably results in a series of explicit methods anyway.
    • Requires type cast.

    Second method:

    Pros:

    • No type casting.
    • If methods are optional, delegate is only bothered with the objects it’s interested in.

    Cons:

    • If methods are not optional and the interface is expanded later, all delegates will have warnings until the new methods are implemented.
    • If methods are not optional, this can be a lot of methods to implement for every delegate.

    Generally when building delegate interfaces I lean towards generics for future extensibility. Changing an API, especially with open source code, can be very difficult. Also, I don’t quite understand why you have one XML parser doing so much. You may want to consider a different design. 17 different XML documents seems like a lot. That aside, I’ll propose a third method.

    Third method:

    Create a dictionary that maps strings to blocks. The blocks would probably be of type void(^BlockName)(id obj). Your parser would define a series of strings that will be the keys for your various blocks. For example,

    NSString * const kFooKey = @"FooKey"; 
    NSString * const kBarKey = @"BarKey";
    // And so on...
    

    Whoever creates the XML parser would register a block for each key they are interested in. They only need to register for the keys they are interested in and it’s completely flexible to future change. Since you are registering for explicit keys/objects, you can assert the passed in type without a type cast (essentially Design By Contract). This might be over kill for what you want, but I’ve found similar designs very beneficial in my code. It combines the pros of both of your solutions. It’s main downfall is if you want to use an SDK that doesn’t have blocks. However, blocks are becoming a de facto standard with Objective-C.

    On top of this you may want to define a protocol that encompasses the common functionality of your 17 objects, if you haven’t done so already. This would change your block type to void(^BlockName)(id<YourProtocol> obj).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file which XML parser choke on. A part of it
I have parsed XML file into objects, in which each object has a 1:1
I have an XML file which I need to parse using PHP and send
I have an XML file which I'd like to parse into a non-XML (text)
I have student.xml file and am parsing this file using SAX Parser and now
I have some simple XML which I would like to parse into an array
I've got a socket from which I'm reading XML data. However, this socket will
I have used the XML Parser before, and even though it worked OK, I
I have been tasked with finding an open source DOM XML parser. The parser
Just how much slower are events? I have written a streaming XML parser (that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.