Fast enumeration on an NSIndexSet - cocoa

Can you fast enumerate a NSIndexSet? if not, what's the best way to enumerate the items in the set?

In OS X 10.6+ and iOS SDK 4.0+, you can use the -enumerateIndexesUsingBlock: message:
NSIndexSet *idxSet = ...
[idxSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
//... do something with idx
// *stop = YES; to stop iteration early
}];

A while loop should do the trick. It increments the index after you use the previous index.
/*int (as commented, unreliable across different platforms)*/
NSUInteger currentIndex = [someIndexSet firstIndex];
while (currentIndex != NSNotFound)
{
//use the currentIndex
//increment
currentIndex = [someIndexSet indexGreaterThanIndex: currentIndex];
}

Fast enumeration must yield objects; since an NSIndexSet contains scalar numbers (NSUIntegers), not objects, no, you cannot fast-enumerate an index set.
Hypothetically, it could box them up into NSNumbers, but then it wouldn't be very fast.

Short answer: no. NSIndexSet does not conform to the <NSFastEnumeration> protocol.

Supposing you have an NSTableView instance (let's call it *tableView), you can delete multiple selected rows from the datasource (uhm.. *myMutableArrayDataSource), using:
[myMutableArrayDataSource removeObjectsAtIndexes:[tableView selectedRowIndexes]];
[tableView selectedRowIndexes] returns an NSIndexSet.
No need to start enumerating over the indexes in the NSIndexSet yourself.

These answers are no longer true for IndexSet in Swift 5. You can perfectly get something like:
let selectedRows:IndexSet = table.selectedRowIndexes
and then enumerate the indices like this:
for index in selectedRows {
// your code here.
}

Related

xcode array access with IBOutletCollections. Mind-boggled.

I have 2 arrays. One is a collection of buttons. The other one should be an array of dictionaries. I am logging the length/size/count of object in the arrays in the console using [arrayListName count]:
2012-07-19 19:56:59.001 ABC[3224:707] lastest_badge_outlet_collection count: 4
2012-07-19 19:56:59.007 ABC[3224:707] badges_array count: 1
ok so when running this loop I want to populate the images with the key value 'name' from each of the dictionaries in existence in the array (that is badges_array). At the moment I have dictionary stored in that array (which is fine). However when I run this through the loop it always populates the third image along
for(int i = 0; i < [lastest_badge_outlet_collection count]; i++){
UIButton *button = [lastest_badge_outlet_collection objectAtIndex:i];
if(i < [badges_array count]){
NSDictionary *badge_d = [badges_array objectAtIndex:i];
NSString *badge_d_image_string = [badge_d objectForKey:#"image"];
UIImage *badge_d_image = [UIImage imageNamed: badge_d_image_string];
[[button imageView] setImage:badge_d_image];
[[button imageView] setContentMode: UIViewContentModeScaleAspectFit];
[button setAlpha:1.0f];
} else {
[button setAlpha:0.5f];
}
}
Theoretically it should be populating the first image. Why is it doing this? How does it decide which order the items are in the outlet collection for example...
Mind boggled.
I tried rewiring them to the collection 1 by 1 by wiring them up in order with no success....
Here is a screenshot.
Thanks!
I don't think there is a way to predict the order of IBOutletCollection.
I guess what you need to do is sort the collection before use it.
You can set tag property for each button and sort them by those number.
Check this answer for more detail.

Why can I not identify simple string key values in a database (core data)

I am a newbie so I apologise if I am missing something obvious.
I am trying to write an app in Xcode 4 to produce stats for my local sports team.
This is the relevant detail of the problem area of my programme:
NSError *error;
NSArray *games = [context executeFetchRequest:fetchRequest error:&error];
if (games == nil) {
NSLog(#"There was an error!");
}
int noOfBatOuts = games.count;
int noOfNotOuts=0;
for (NSManagedObject *oneMatch in games) {
if ([oneMatch valueForKey:#"batOut"]==#"NO") {
noOfBatOuts = noOfBatOuts - 1;
noOfNotOuts = noOfNotOuts + 1;
NSLog(#"Not Out!");
}
NSLog(#"How out %#",[oneMatch valueForKey:#"batOut"]);
}
notOuts.text=[NSString stringWithFormat:#"%d",(noOfNotOuts)];
NSLog(#"No of Not Outs is %#",notOuts.text);
When I run with data that has a not out string - NO, - the NSLog(#"Not Out!") is never called and the final NSLog(#"No of Not Outs...) reports zero. However I know that the data is there and it is identified in the middle NSLog(#"How Out....).
I am starting to tear my hair out in frustration and not having the knowledge yet to know the answer. Can anybody help please?
I would assume that batOut is a Boolean rather than string type, in which case you should be checking for truth rather than string equality.
If batOut really is a string, then you still can't compare strings this way. You need to use isEqual: or isEqualToString:. For example:
if ([[oneMatch valueForKey:#"batOut"] isEqual:#"NO"]) {
Normally you'd use isEqualToString: for string comparisons, but -valueForKey: is an id so isEqual: is safer.
The == operator checks that two pointers have the same value. Two strings can have the same contents but not be stored in the same memory.
You can't compare strings with ==. Use NSString's isEqualToString:

Apply a block to all objects of an NSArray

Is there a way to apply a block to all the objects of an array?
Let's say I have an amazing block:
void (^myAmazingBlock)(NSNumber *) = ^(NSNumber *aFooNumber) {
NSLog(#"Log message from an Amazing Block: %#", aFooNumber);
};
To apply my block to all the objects of my array, this works:
for (NSNumber *aNumber in myArray) {
myAmazingBlock(aNumber);
}
Is it possible to get rid of the fast enumeration loop, and have something similar in the spirit to:
[myArray valueForKeyPath:#"#distinctUnionOfObjects.^myAmazingBlock"]; // This code doesn't work. It's just to show the style of what I'm trying to write.
I don't have any specific use in mind ; I was just being curious of the other possible ways to write this.
Edit:
It's possible to use enumerateObjectsUsingBlock: (Thank you paulbailey).
You then have to declare your block like this:
void (^myAmazingBlock2)(id, NSUInteger, BOOL *) = ^(id aFooNumber, NSUInteger idx, BOOL *stop) {
NSLog(#"Amazing BLOCK 2 %#", aFooNumber);
};
[myArray enumerateObjectsUsingBlock:myAmazingBlock2];
Unfortunately, the fast enumeration loop is still the most readable solution.
You could use enumerateObjectsUsingBlock: perhaps? Obviously you'd have to adapt the parameters of your block to match those specified.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html

How do I select a random key from an NSDictionary?

When I was using an NSArray, it was easy:
NSArray *array = ...
lastIndex = INT_MAX;
...
int randomIndex;
do {
randomIndex = RANDOM_INT(0, [array count] - 1);
} while (randomIndex == lastIndex);
NSLog(#"%#", [array objectAtIndex:randomIndex]);
lastIndex = randomIndex;
I need to keep track of the lastIndex because I want the feeling of randomness. That is, I don't want to get the same element twice in a row. So it shouldn't be "true" randomness.
From what I can tell, NSDictionary doesn't have something like -objectAtIndex:. So how do I accomplish this?
You can get an array of keys with allKeys (undefined order) or keysSortedByValueUsingSelector (if you want sorting by value). One thing to keep in mind (regarding lastIndex) is that even with sorting, the same index may come to refer to a different key-value pair as the dictionary grows.
Either of these (but especially keysSortedByValueUsingSelector) will come with a performance penalty.
EDIT: Since the dictionary isn't mutable, you should just be able to call allKeys once, and then just pick random keys from that.
You could use the code below:
- (YourObjectType *)getRandomObjectFromDictionary:(NSDictionary *)dictionary
{
NSArray *keys = dictionary.allKeys;
return dictionary[keys[arc4random_uniform((int)keys.count)]];
}
To make it more efficient, you can cache keys in an instance variable. Hope this helps.

Get NSIndexSet from NSArray

NSArray has useful methods to find objects for specified indexes
// To find objects by indexes
- (id)objectAtIndex:(NSUInteger)index
- (NSArray *)objectsAtIndexes:(NSIndexSet *)indexes
// To find index by object
- (NSUInteger)indexOfObject:(id)anObject
However, I want to get NSIndexSet (multiple indexes) for given objects. Something like:
- (NSIndexSet *)indexesOfObjects:(NSArray *)objects
This method does not exist for NSArray. Am I missing something? Does someone know another standard method? Otherwise I have to write this as a category method.
Newer NSArray versions (OSX 10.6 and iOS 4) provides the indexesOfObjectsPassingTest: method.
NSIndexSet *indexesOfObjects = [[array1 indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return [array2 containsObject:obj];
}];
It might be useful to implement it using a set to specify the objects to find, such as:
- (NSIndexSet *) indicesOfObjectsInSet: (NSSet *) set
{
if ( [set count] == 0 )
return ( [NSIndexSet indexSet] );
NSMutableIndexSet * indices = [NSMutableIndexSet indexSet];
NSUInteger index = 0;
for ( id obj in self )
{
if ( [set containsObject: obj] )
[indices addIndex: index];
index++;
}
return ( [[indices copy] autorelease] );
}
This requires visiting every object in the array, but at least only does so once and makes use of fast enumeration while doing so. Using an NSSet and testing each object in the array against that set is also much faster than testing for inclusion in an array.
There's a potential optimization here, but it would break in the case where a single object is stored in the receiving array multiple times:
if ( [set containsObject: obj] )
{
[indices addIndex: index];
if ( [indices count] == [set count] )
break;
}
That way if you're scanning a 20'000-item array for two objects and they're both inside the first ten, you'll be able to avoid scanning the other 19'990 objects in the array. As I said though, that doesn't help if the array contains duplicates, because it'll stop as soon as it's found 2 indices (even if they both point to the same object).
Having said that, I agree with Mike's comment above. Chances are you're setting yourself up for some pain come optimization-time. It may be worth thinking about different data types; for instance, while NSArray seems the most logical choice for a simple flat container, if you don't actually need the ordering information it's better to use an NSSet instead; this has the added advantage that it won't store the same object (calculated using -isEqual:) twice. If you do want to keep track of duplicates, but don't need ordering, you can use NSCountedSet, which behaves as NSSet except it keeps track of how many times each objects has been added/removed without actually storing duplicates.
You have to implement your own category, as far as I can see.

Resources