0

I have an array - placeObjectsArray, that hold a lot of objects called place. Place is object of class PlaceHolder, in which i create different properties, filled with data:

self.place = [[PlaceHolder alloc]init];
// A lot of code here during parson XML with data
[self.placeObjectsArray addObject:self.place];

Header of this file look like this:

@interface PlaceHolder : NSObject

@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *description;
@property (strong, nonatomic) NSString *webPage;

It actually a container for an entity, each one hold data for name, description, image links etc. At now, i have array with place objects. What i want to, to manipulate with that objects inside an array. For example, how could i find all of data for specific "name"? (Name is one of properties in PlaceHolder class). How could i make an array that contain only names? How could i see in console 10 random "description"?

Any advice would be appreciated, thanks!

3 Answers 3

2

You're asking a bunch of separate questions.

First, how to select items in your array that match a particular name: Create an NSPredicate and use filteredArrayUsingPredicate. Google NSPredicate and you should find lots of examples.

Alternately you could use indexesOfObjectsPassingTest to get an index set of the items in the array that match your search criteria, and then use objectsAtIndexes: to turn the index set into a sub-array.

As for how to get all the names from the entries in your array, you can use a very cool trick in key value coding.

If you send an array the valueForKey message, it tries to fetch an item from each entry in the array using that key, and return them all in a new array. The code would look like this:

NSArray *names = [placeObjectsArray valueForKey @"name"];

Fetching 10 random descriptions is a little more complicated. You would need to write code that loops through the array, selecting 10 random items, and appends the description of each one into a new mutable array.

The trick there is to use arc4random_uniform to get a random index in your array:

NSUInteger random_index = arc4random_uniform(placeObjectsArray.count);

I leave the rest to you as a learning exercise.

If you want to fetch 10 random descriptions and make sure you never fetch the same description twice it's more complicated. You need to create a mutable copy of your array, then loop through the copy, fetching a random item, adding it's description to an array, and deleting the item from the array.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use NSPredicates:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name LIKE[cd] %@", nameSearch];
NSArray *filtered = [self.placeObjectsArray filteredArrayUsingPredicate:predicate];

Comments

1

You could iterate over your array looking for the PlaceHolder with a given name, like:

PlaceHolder *namedPlaceholder = nil;
for (PlaceHolder *placeholder in theArray) {
    if ([placeholder.name isEqualToString:"whateverName"]) {
         namedPlaceholder = placeholder;
         break;
    }
}

If you want to find PlaceHolders by name efficiently you might consider using a dictionary instead of an array. With a dictionary you can map names to objects, like:

NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
myDictionary[@"foo"] = somePlaceholder;
myDictionary[@"bar"] = someOtherPlaceholder;

and retrieve them like:

PlaceHolder *somePlaceholder = myDictionary[@"foo"];

To get random objects from an array, I recommend getting random indexes using arc4random_uniform. This gives pseudo-random numbers with a better uniform distribution than rand or random, and does not require you to explicitly seed the sequence with srand or srandom.

PlaceHolder *randomPlaceholder = theArray[arc4random_uniform(theArray.count)];

or

const NSUInteger arrayCount = theArray.count;
for (NSUInteger j = 0; j < 10; j++) {
    PlaceHolder *randomPlaceholder = theArray[arc4random_uniform(arrayCount)];
    // Do something with randomPlaceholder.
}

5 Comments

Thank you Aaron for such a nice answer!
I try to avoid giving all the code for solutions because then the questioner just copy/pastes the answer and doesn't learn anything. Instead, I give code fragments and an outline on how to implement the full solution, and let the OP ask follow-on questions if they get stuck. I advise doing the same.
Dunkan, i like your attitude, person who ask answer should start thinking instead of copy paste and learn the lesson :) Still, I very pleasant with fact, that Aaron try to help me with code snippets.
@DuncanC: Don't get too hung up on "the code." Writing code is the easiest part of software development, after all. It's better to help askers get past the trivial bits quickly so they can move on to the bigger problems/questions relevant their applications.
Except I find that new developers sometimes use copy-paste of code they don't understand to the exclusion of real learning, and never really learn design, data structures, etc, much less writing code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.