Resolved Best way to access all properties of an object whose names begin with a string?

Discussion in 'iOS Programming' started by moonman239, Nov 13, 2013.

  1. moonman239, Nov 13, 2013
    Last edited: Nov 18, 2013

    moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    Suppose I have a piece of code like this:

    Code:
    Person *JohnDoe;
    Person *JaneDoe;
    Person *RichardRowe;
    JohnDoe.friends = [NSArray arrayWithObjects:JaneDoe,RichardRowe];
    
    Now suppose that I want to access the variable in JohnDoe.friends whose name begins with the letter J. How do I do that?
     
  2. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #2
    I think I found the answer.

    1) All NSArrays have a method that is named filteredArrayUsingPredicate. This method returns objects that meet the condition or set of conditions set forth in the predicate that is passed to the method.

    2) Each NSObject has a description string that contains the name of the variable. This is normally used during development for making sure a variable has a value, but according to the author of the 1st answer on this page I can use it for other reasons, mine included.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    Using description for anything other than debugging is frowned upon. Also, your Person description method won't be useful unless you write it.

    Use the name property directly in your predicate.
     
  4. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    I agree with PhoneyDeveloper - use the name property of Person as your predicate. If Person doesn't have a name property, make it one and use it.
     
  5. moonman239, Nov 18, 2013
    Last edited: Nov 18, 2013

    moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #5
    I was just using a Person object as an example. Here's a better example to demonstrate what I wanted to do:

    Code:
    NSObject *anObject;
    NSObject *anotherObject;
    -(void-)viewDidLoad
    {
    NSObject *objectImThinkingOf = [self objectWhoseNameContainsLetter:@"r"];
    }
    
    -(NSObject *)objectWhoseNameContainsLetter:(NSString *)theLetter
    {
    // Code that is used to find the object to use.
    }
    
    
     
  6. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #6
    The major difference between the two pieces of code you gave us has nothing to do with Person - it's actually the fact you went from having an NSArray to not having anything.

    To do that, you'll want to look into the objc-runtime header.

    Here's some incomplete code to help you get started...

    Code:
    #import <objc/objc-runtime.h>
    Code:
        unsigned int outCount;
        Ivar *ivars = class_copyIvarList(self.class, outCount);
        if (ivars) {
            for (int i = 0; i < outCount; i++) {
                Ivar ivar = ivars[i];
                const char *ivarName = ivar_getName(ivar);
                NSString *ivarNameString = [NSString stringWithCString:ivarName encoding:NSASCIIStringEncoding];
                // You now have the name of the ivar as an NSString. You can compare it against whatever and decide what you want to do with it from here.
            }
        }
     
  7. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #7
    Thanks!
     
  8. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #8
    I would like to mention this is very low level stuff. I use code like this to find private classes/methods/properties in Cocoa/Cocoa Touch. If you're using it in actual production code, it would indicate your code was very poorly engineered.

    IE, just the other day I used this to find the private method _setCollapsed:forView: on Apple's NSSplitView, so that I could call that method within my own code.
     

Share This Page