PDA

View Full Version : Searching for an object in a set




multinode
May 30, 2011, 09:29 PM
I have an NSSet of NSArrays. Each of the NSArrays has a Name (NSString) field in it. I need to search the NSSet to find the NSArray that has a particular value in its Name field. Can somebody suggest how I can do that please? I'm thinking that somehow Key-Value programming should be involved, but KV programming is not my strong suit.



chown33
May 31, 2011, 12:39 AM
NSArray doesn't have a "Name field" (whatever that means).

The objects in an NSArray are stored by index, not by "field" or key. You could say that there is an NSString stored at index 3, or index 27, or whatever. It's not possible to say the NSArray has a "Name field".

If you meant NSDictionary instead of NSArray, and you meant there is a key called "Name", then that's a completely different thing.

If you didn't mean an NSDictionary, then please explain exactly how an NSArray has a "Name field".

multinode
May 31, 2011, 02:19 AM
Here's a better explanation:

Each NSArray in my NSSet has n NSStrings. The first such NSString is a name. I need to find the NSArray in my NSSet whose first NSString is a given name.

chown33
May 31, 2011, 03:10 PM
Exactly what are you trying to accomplish with this data structure?

Maybe an NSSet of NSArrays is a poor choice for what you want to accomplish? Beginners often make poor choices of data structures, which turns into a "How do I do X with the Y data structure?" question as difficulties arise. The real question should be "What data structure(s) can I use to accomplish goal Z?" Without knowing the goal, and all the things expected from the data structure, it's hard to answer either question well.

If I had a bunch of NSArrays, where each one's first item was treated as a name, and I had to retrieve an NSArray by name, I'd use an NSDictionary. The key would be the name to retrieve by (i.e. first item in NSArray), and the value would be the NSArray itself. This assumes names are unique, which is somewhat implied by the use of NSSet and the description provided so far. I might not use an NSArray for storing the items; it depends on what I was going to do with the items therein.

Hansr
May 31, 2011, 04:12 PM
Exactly what are you trying to accomplish with this data structure?

+1 . A beginner tutorial in data structures that takes maybe 5-6 hours should give you a glimpse at what's logical and then move on from there to read up on what suits you.

multinode
Jun 1, 2011, 12:48 AM
Hey guys ... I do know exactly what I'm doing and why. My question isn't about app logic ... it's an issue of my not knowing how to implement what I need in ObjC!

I have a set of NSArrays. Each NSArray is composed of n NSStrings. The first NSString in each NSArray is a unique customer name. If it helps, each NSArray is a customer's order. Given the customer's name, I need to be able to fetch his order (NSArray) out of the NSSet.

Back in the days of Carbon and C, I did this with linked lists.

Is that clear now?

I think CHOWN33's thoughts about using an NSDictionary are probably on target. I'm not familiar enough with using NSDictionary ... CHOWN33, could you please pseudo code a few lines to help me or refer me to some sample code snippets?

chown33
Jun 1, 2011, 11:21 AM
I have a set of NSArrays. Each NSArray is composed of n NSStrings. The first NSString in each NSArray is a unique customer name. If it helps, each NSArray is a customer's order. Given the customer's name, I need to be able to fetch his order (NSArray) out of the NSSet.

Back in the days of Carbon and C, I did this with linked lists.

Is that clear now?

What you want to do is now clear.

What isn't clear is why you'd use an NSSet. I see nothing in what you want to do that suggests storing the customer orders in an NSSet will do anything for you. You could have stored them in an NSArray, then used an NSPredicate or a block to search for the one array whose first item was SomeUniqueName. Or just enumerate the NSArray and test the first item of each sub-array for SomeUniqueName. I wonder if using NSSet actually prevented you from seeing these relatively simple possibilities.

A linked list is essentially a sequence. An NSArray's elements are sequential. Searching a linked list takes linear time. So does enumerating an NSArray's items.

I think CHOWN33's thoughts about using an NSDictionary are probably on target. I'm not familiar enough with using NSDictionary ... CHOWN33, could you please pseudo code a few lines to help me or refer me to some sample code snippets?

You should probably read the "Collections Programming Topics" guide:
http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Collections/Collections.html

The NSDictionary class reference doc has links to sample code projects. Try those.

If necessary, Google search terms: NSDictionary example

multinode
Jun 1, 2011, 04:43 PM
Thanx CHOWN33. I did originally consider using an NSArray to contain my customer NSArray(s). I went over to NSSet because I thought that NSSet was more free form and thereby less restrictive than an NSArray of NSArray(s). I thought that that would give me access to a richer repertoire of methods.

Yes ... I could enumerate the customer NSArray(s) and test each object's first item ... much like I did with linked lists. I just thought that ObjC might do a lot of the housekeeping "under the covers" and thereby give me tighter, more readable code.

I still think that your suggestion of using NSDictionary may be the best idea. I will look for some sample code in the ways you suggested.

chown33
Jun 1, 2011, 05:06 PM
Thanx CHOWN33. I did originally consider using an NSArray to contain my customer NSArray(s). I went over to NSSet because I thought that NSSet was more free form and thereby less restrictive than an NSArray of NSArray(s). I thought that that would give me access to a richer repertoire of methods.

I don't know why you would think that, assuming you Read The Fine Manual.

The basic class reference docs say that NSSet requires distinct objects, i.e. no two objects in an NSSet can be isEqual: to one another. That's more restrictive than NSArray, not less.

Yes ... I could enumerate the customer NSArray(s) and test each object's first item ... much like I did with linked lists. I just thought that ObjC might do a lot of the housekeeping "under the covers" and thereby give me tighter, more readable code.


You should probably invest a little time with NSArray's class reference doc. A once-through read seems like a good idea. I suggest after reading the aforementioned "Collections Programming Topics" guide.

NSArray has several methods whose name starts with indexOfObject that would be worth reading. There is also filteredArrayUsingPredicate: that uses an NSPredicate. Or if you dislike blocks for some reason, you could write a fairly simple category on NSArray that performs a "find first item using NSPredicate", which would then be available for all NSArrays.


Separate subject: will you be attending WWDC?

Nope.