Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
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?
 
Last edited:

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
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.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,558
6,058
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.
 

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
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.
}
 
Last edited:

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,558
6,058
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.
        }
    }
 

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
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.
        }
    }

Thanks!
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,558
6,058

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.