Re: Selecting an object based on the value of one of its attributes

2010-05-03 Thread Lynn Barton
Roland, the application in question uses Core Data. To build a dictionary as 
you suggest, I will need to wait until the array controller loads its content, 
then iterate over all members of the array, putting the property in question 
and the array index into the dictionary. How can I know immediately (without 
any user action) that the content has finished loading? I can't use 
awakeFromNib, as the content has not yet been loaded when that method is called.
--Lynn

On Apr 27, 2010, at 8:06 PM, Roland King wrote:

 Graham Cox wrote:
 On 28/04/2010, at 12:37 PM, Lynn Barton wrote:
 Newbie question: Consider an array of dictionary objects, all of the same 
 class. One of the ivars of that class is an NSString which is unique for 
 each instance. Does there already exist a method that will identify the one 
 dictionary object that has a given value of that ivar, without me having to 
 write code to examine all of the objects one by one? I have searched the 
 documentation without finding such a method.
 Lynn Barton
 You could use NSPredicate to filter your collection based on your unique 
 string property being equal to the one sought. If they are unique it will 
 return exactly one item (or none, if it doesn't exist).
 See [NSArray filteredArrayUsingPredicate:]
 It's unclear whether that would be actually any faster than doing a linear 
 search yourself - it might be slower, in that it wouldn't return as soon as 
 it found the item, but would always check every element.
 --Graham
 
 I think that as Graham suggests that would be slower than searching yourself, 
 but it is a method and it does exist and it's free, so you could try that 
 and if it's fast enough for you, that's great.
 
 Actually iterating the list however yourself is very simple  and possibly 
 only the same number of lines of code as making a predicate. (code typed in 
 mail)
 
 YourObject *found = nil;
 for( YourObject *obj in yourArrayOfObjects )
if( [ [ obj thePropertyYouWant ] isEqualToString:yourThing ] )
{
found = obj;
break;
}
 // if found isn't nil, you found one, if it is, you failed.
 
 Finally - are these all your objects and are you always looking for the same 
 property of them? If so instead of dumping them into an array you could build 
 a dictionary of them as you insert them, keyed by that string property, then 
 go look it up later when you want it.

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Selecting an object based on the value of one of its attributes

2010-05-03 Thread Kyle Sluder
On Mon, May 3, 2010 at 3:24 PM, Lynn Barton lynnbar...@mac.com wrote:
 Roland, the application in question uses Core Data. To build a dictionary as 
 you suggest, I will need to wait until the array controller loads its 
 content, then iterate over all members of the array, putting the property in 
 question and the array index into the dictionary. How can I know immediately 
 (without any user action) that the content has finished loading? I can't use 
 awakeFromNib, as the content has not yet been loaded when that method is 
 called.

This sounds like something that should be happening at the model
layer, not at the controller layer.

--Kyle Sluder
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Selecting an object based on the value of one of its attributes

2010-04-28 Thread vincent habchi
Le 28 avr. 2010 à 04:37, Lynn Barton a écrit :

 Newbie question: Consider an array of dictionary objects, all of the same 
 class. One of the ivars of that class is an NSString which is unique for each 
 instance. Does there already exist a method that will identify the one 
 dictionary object that has a given value of that ivar, without me having to 
 write code to examine all of the objects one by one? 

Use a hash code. That's the way NSDictionary class search for keys. It is way 
faster than simple linear search, especially if you have thousands of elements. 
But it is more code and memory expensive.

Vincent

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Selecting an object based on the value of one of its attributes

2010-04-27 Thread Lynn Barton
Newbie question: Consider an array of dictionary objects, all of the same 
class. One of the ivars of that class is an NSString which is unique for each 
instance. Does there already exist a method that will identify the one 
dictionary object that has a given value of that ivar, without me having to 
write code to examine all of the objects one by one? I have searched the 
documentation without finding such a method.
Lynn Barton
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Selecting an object based on the value of one of its attributes

2010-04-27 Thread Graham Cox

On 28/04/2010, at 12:37 PM, Lynn Barton wrote:

 Newbie question: Consider an array of dictionary objects, all of the same 
 class. One of the ivars of that class is an NSString which is unique for each 
 instance. Does there already exist a method that will identify the one 
 dictionary object that has a given value of that ivar, without me having to 
 write code to examine all of the objects one by one? I have searched the 
 documentation without finding such a method.
 Lynn Barton


You could use NSPredicate to filter your collection based on your unique 
string property being equal to the one sought. If they are unique it will 
return exactly one item (or none, if it doesn't exist).

See [NSArray filteredArrayUsingPredicate:]

It's unclear whether that would be actually any faster than doing a linear 
search yourself - it might be slower, in that it wouldn't return as soon as it 
found the item, but would always check every element.

--Graham


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Selecting an object based on the value of one of its attributes

2010-04-27 Thread Roland King

Graham Cox wrote:

On 28/04/2010, at 12:37 PM, Lynn Barton wrote:



Newbie question: Consider an array of dictionary objects, all of the same 
class. One of the ivars of that class is an NSString which is unique for each 
instance. Does there already exist a method that will identify the one 
dictionary object that has a given value of that ivar, without me having to 
write code to examine all of the objects one by one? I have searched the 
documentation without finding such a method.
Lynn Barton




You could use NSPredicate to filter your collection based on your unique 
string property being equal to the one sought. If they are unique it will return exactly 
one item (or none, if it doesn't exist).

See [NSArray filteredArrayUsingPredicate:]

It's unclear whether that would be actually any faster than doing a linear 
search yourself - it might be slower, in that it wouldn't return as soon as it 
found the item, but would always check every element.

--Graham




I think that as Graham suggests that would be slower than searching 
yourself, but it is a method and it does exist and it's free, so you 
could try that and if it's fast enough for you, that's great.


Actually iterating the list however yourself is very simple  and 
possibly only the same number of lines of code as making a predicate. 
(code typed in mail)


YourObject *found = nil;
for( YourObject *obj in yourArrayOfObjects )
if( [ [ obj thePropertyYouWant ] isEqualToString:yourThing ] )
{
found = obj;
break;
}
// if found isn't nil, you found one, if it is, you failed.

Finally - are these all your objects and are you always looking for the 
same property of them? If so instead of dumping them into an array you 
could build a dictionary of them as you insert them, keyed by that 
string property, then go look it up later when you want it.

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Selecting an object based on the value of one of its attributes

2010-04-27 Thread Lynn Barton

On Apr 27, 2010, at 8:06 PM, Roland King wrote:

 Graham Cox wrote:
 On 28/04/2010, at 12:37 PM, Lynn Barton wrote:
 Newbie question: Consider an array of dictionary objects, all of the same 
 class. One of the ivars of that class is an NSString which is unique for 
 each instance. Does there already exist a method that will identify the one 
 dictionary object that has a given value of that ivar, without me having to 
 write code to examine all of the objects one by one? I have searched the 
 documentation without finding such a method.
 Lynn Barton
 You could use NSPredicate to filter your collection based on your unique 
 string property being equal to the one sought. If they are unique it will 
 return exactly one item (or none, if it doesn't exist).
 See [NSArray filteredArrayUsingPredicate:]
 It's unclear whether that would be actually any faster than doing a linear 
 search yourself - it might be slower, in that it wouldn't return as soon as 
 it found the item, but would always check every element.
 --Graham
 
 I think that as Graham suggests that would be slower than searching yourself, 
 but it is a method and it does exist and it's free, so you could try that 
 and if it's fast enough for you, that's great.
 
 Actually iterating the list however yourself is very simple  and possibly 
 only the same number of lines of code as making a predicate. (code typed in 
 mail)
 
 YourObject *found = nil;
 for( YourObject *obj in yourArrayOfObjects )
if( [ [ obj thePropertyYouWant ] isEqualToString:yourThing ] )
{
found = obj;
break;
}
 // if found isn't nil, you found one, if it is, you failed.
 
 Finally - are these all your objects and are you always looking for the same 
 property of them? If so instead of dumping them into an array you could build 
 a dictionary of them as you insert them, keyed by that string property, then 
 go look it up later when you want it.
GREAT IDEA ! I'll try it. Thanks to you and Graham.
Lynn
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com