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 7043631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:18:49+00:00 2026-05-28T02:18:49+00:00

I have a small function which I want to rewrite, so that function is

  • 0

I have a small function which I want to rewrite, so that function is valid for every class.
At the moment I have 10 of the same functions which all work same but every function is for another class.
I know, that I have to do it with reflections, but I am not so sure how to do it.
I already read this link:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html

The functions I am talking about are:

-(NSCountedSet *)MissionGetReferecedNested:(id)modelObject
{
    setOfObjects = [[NSCountedSet alloc]initWithArray:modelObject.MissionSectionList];
    return setOfObjects;
}
-(NSCountedSet *)MissionGetSectionReferecedNested:(id)modelObject
{
    setOfObjects = [[NSCountedSet alloc]initWithArray:modelObject.DamageAccountList];
    return setOfObjects;
}

MissionSectionList and DamageAccountList are both NSMutableArrays from two different classes.
Is it possible to see if a class consists a NSMutableArray and if yes then it should call the …. modelObject.MyMutableArray?

  • 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-28T02:18:49+00:00Added an answer on May 28, 2026 at 2:18 am

    You can use reflection like this:

    - (NSCountedSet *)MissionGet:(id)modelObject
    {
        SEL propertySelector = NULL;
    
        if ([modelObject respondsToSelector:@selector(MissionSectionList)]) {
            propertySelector = @selector(MissionSectionList);
        } else if ([modelObject respondsToSelector:@selector(DamageAccountList)]) {
            propertySelector = @selector(DamageAccountList);
        }
    
        if (!propertySelector) {
          [NSException raise:@"Invalid modelObject value" format:@"Model object %@ does not contain any recognised selectors", modelObject];
        }
    
        return [[NSCountedSet alloc] initWithArray:[modelObject performSelector:propertySelector]];
    }
    

    But a more common technique among cocoa programmers would be:

    - (NSCountedSet *)MissionGet:(id <MyCustomProtocol>)modelObject
    {
        return [[NSCountedSet alloc] initWithArray:[modelObject missionArray]];
    }
    

    Where you would accept any object which confirms to the protocol MyCustomProtocol. The protocol is defined in a header files somewhere, using:

    @protocol MyCustomProtocol
    
    @property (readonly) NSArray *missionArray;
    
    @end
    

    And then in each of your classes, declare it as implementing the protocol:

    @interface MissionSectionListClass <MyCustomProtocol>
    

    And add a method implementation:

    @implementation MissionSectionListClass <MyCustomProtocol>
    
    - (NSArray *)missionArray
    {
        return self.MissionSectionList;
    }
    
    @end
    

    Using protocols is a bit more code, but it’s the “right” way to go. It allows you to add support for new classes, without any change to your MissiongGet... method.

    More info about protocols: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProtocols.html

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

Sidebar

Related Questions

I have a class which has many small functions. By small functions, I mean
For example, I have a small function that I want it to keep adding
I have a small JS function that does Ajax for me and another like
I have a small C# function that takes any generic SQLDataReader and converts it
I have functions that contribute to small parts of a figure generation. I'm trying
I have written a small function, which uses ElementTree and xpath to extract the
I want to globalize my application. I have created a small form which asks
Hello I have a small function which I use for searching for a users
I am currently have a small program in Common Lisp, which I want to
I have written a small function in Emacs that does a git stash git

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.