Detect number of different strings in Array - objective-c

Basically I need a function which counts the amount of different values in an array and one other function to give me the actual count of each different value in my array.
I have an Array which contains a changing amount of values:
Array = (This, This, This, This, Is, Is, Is, Is, Is, It, It, It, It)
I want to make a list view, each section should contain the different categories like:
This
- Element 1
- Element 2
- ...
Is
- Element 1
- ...
It
- Element 1
- ...
So I need the number 3 for my number of sections and the number of child-elements for each section.
How can I achieve that? Is there a better way than a for-statement with counting indexes for each section?
Thank you!

Add each array element to an NSCountedSet. Then the count of the set is the number of distinct objects you added, and you can use countForObject: to ask the set how many there are of each distinct object.

First, make o copy of your array, to work with.
To count different values, remove all duplicates from the copied
array, and copiedArray.count will tell you what you need. (search on
your own how to remove duplicates).
To get the actual count of each group: you have the copied array already without duplicates, so make a for loop within the copied array:
for (NSString *stringer in copiedArrayWithoutDuplicates){
int counter = 0;
for(NSString *iniStr in initialArrayWithDuplicates){
if([stringer isEqualToString: iniStr]){
counter++;
}
}
NSLog(#"string %# was detected %i times", stringer, counter);
}

Related

Associating a value with another value generated randomly

Here's what I'm looking to do:
I have a series of survey questions that are generated randomly for the user.
What I want is to associate two values with each other, the integer value of the question number that was generated randomly, and a integer value representative of how many times this user has answered this question. I want to be able to save and retrieve these associated values later so I can increment how many times the user has answered this question.
So, how do you associate two different integer values to each other, save, and update them later?
I'm starting with this code.
QuestionSelected = arc4random_uniform(4);
For example once this outcome was generated:
QuestionSelected = 1
I would need to establish a new value that I can associate with that question's value (1), increment it, and then save each time the user returns to this question.
Thanks for your help!
Association of one value with another is a "map". NSDictionary is the standard mapping collection in Cocoa, so you can use that:
NSNumber * timesSeen = myMutableDictionary[#(selectedQuestion)];
if( !timesSeen ){
timesSeen = #(0);
}
myMutableDictionary[#(selectedQuestion)] = #([timesSeen intValue] + 1);
Since an array in essence associates its indexes, which are numbers, with values, another option is to use an NSMutableArray:
NSNumber * timesSeen = myMutableArray[selectedQuestion];
myMutableArray[selectedQuestion] = #([timesSeen intValue] + 1);
An NSArray can't be sparse, so you would have to fill it up with zeroes when you create it:
for( NSUInteger i = 0; i < numQuestions; i++ ){
[myMutableArray addObject:#(0)];
}
As you may notice, dealing with numerical values that need to change in a Cocoa collection is a bit of a pain, because the NSNumbers have to be unboxed and replaced each time. So a third option would be a C array:
int * viewCounts;
viewCounts = calloc(numQuestions, sizeof(int));
viewCounts[selectedQuestion]++;
calloc() gets a chunk of memory for you and fills it with zeros. You will need to release that memory by calling free(viewCounts) when you are done with the array.

objects in array no index position

I have an array which is a parsed xml feed which i want to add to another array using the code....
int insertIdx = [blogEntries count];
for (RSSItem *nextItem in feedItems) {
[blogEntries insertObject:nextItem atIndex:insertIdx];
//[blogEntries addObject:nextItem];
insertIdx += 1;
}
For some reason all of the objects in the blogEntries array all have an index of 0, when i slog them all out using...
for (RSSItem *nextItem in blogEntries)
NSLog(#"title - %#, pos - %i", nextItem.title, [blogEntries IndexOfObject:nextItem]);
do you know why the index might not be updating?
Any help would be appreciated
Did you try to use addObject: instead of insertObject:atIndex?
Are you sure that objects are different as:
indexOfObject:
Returns the lowest index whose corresponding array value is equal to a given object.
I was not able to refrain myself On the Topic of adding many item at once into a NSMutableArray, I do prefer to use this method :
- (void)addObjectsFromArray:(NSArray *)otherArray
it remove that useless for loop. (unless you need to do some other work in that loop)
And as of your problem, viperking have a big hint about a possible problem you may be facing. (if that is the case you may need to validate your isEqual: method.

change element value nsmutablearray

hi i have an array which holds 200 objects or so. Each of these objects is another array with 6 fields of mixed types ( ints, strings and bools).
2 questions...
can i search the array to find the objects that have a certain element i.e say all objects that have element "A" = TRUE.
How do i update a single element from one of the objects? DO i have to find that object (from the parent array hence why i asked the first question) , remove it then add a new object with the updated field? seems a bit overkill but is this what i need to do? is there anyway just to update that single element ?
Yes you can search for that, and yes you must if you're going to change a value. You can use indexOfObjectPassingTest to find the object. in your posted example, you would use it like this (assuming that your objects are each dictionaries with one of the fields being "A"):
NSUInteger indx =[myArray indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return [[obj valueForKey:#"A"] isEqualToNumber:[NSNumber numberWithBool:TRUE]];
}];
indx will be the index of the object that passes that test in your array.

Implementing an NSOutlineView to edit the contents of a plist file

I'm writing a game using cocos2d-iphone and our stages are defined in .plist files. However, the files are growing large - so I've developed an editor that adds some structure to the process and breaks most of the plist down into fixed fields. However, some elements still require plist editor type functionality, so I have implemented an NSOutlineView on the panels that show 'other parameters'. I am attempting to duplicate the functionality from XCode's 'Property List Editor'.
I've implemented the following system; http://www.stupendous.net/archives/2009/01/11/nsoutlineview-example/
This is very close to what I need, but there is a problem that I've spent most of today attempting to solve. Values for the key column are calculated 'backwards' from the selected item by finding the parent dictionary and using;
return [[parentObject allKeysForObject:item] objectAtIndex:0];
However, when there are multiple items with the same value within a given dictionary in the tree, this statement always returns the first item that has that value (it appears to compare the strings using isEqualToString: or hash values). This leads to the key column showing 'item1, item1, item1' instead of item1, item2, item3 (where items 1-3 all have value ''). I next tried;
-(NSString*)keyFromDictionary:(NSDictionary*)dict forItem:(id)item
{
for( uint i = 0; i < [[dict allKeys] count]; i++ ) {
id object = [dict objectForKey:[[dict allKeys] objectAtIndex:i]];
if ( &object == &item ) {
return [[dict allKeys] objectAtIndex:i];
}
}
return nil;
}
But this always returns nil. I was hoping that somebody with a bit more experience with NSOutlineView might be able to provide a better solution. While this problem only appears once in the linked example, I've had to use this a number of times when deleting items from dictionaries for instance. Any help would be greatly appreciated.
return [[parentObject allKeysForObject:item] objectAtIndex:0];
However, when there are multiple items with the same value within a given dictionary in the tree, this statement always returns the first item that has that value …
Well, yeah. That's what you told it to do: “Get me all the keys for this value; get me the first item in the array; return that”.
… this statement always returns the first item that has that value (it appears to compare the strings using isEqualToString: or hash values).
It's not that statement that's doing it; it's how dictionaries work: Each key can only be in the dictionary once and can only have exactly one object as its value, and this is enforced using the hash of the key and by sending the keys isEqual: messages (not the NSString-specific isEqualToString:—keys are not required to be strings*).
The values, on the other hand, are not uniquified. Any number of keys can have the same value. That's why going from values to keys—and especially to a key—is so problematic.
*Not in NSDictionary, anyway. When you attempt to generate the plist output, it will barf if the dictionary contains any non-string keys.
I next tried;
-(NSString*)keyFromDictionary:(NSDictionary*)dict forItem:(id)item
{
for( uint i = 0; i < [[dict allKeys] count]; i++ ) {
id object = [dict objectForKey:[[dict allKeys] objectAtIndex:i]];
if ( &object == &item ) {
return [[dict allKeys] objectAtIndex:i];
}
}
return nil;
}
But this always returns nil.
That's the least of that code's problems.
First, when iterating on an NSArray, you generally should not use indexes unless you absolutely need to. It's much cleaner to use fast enumeration.
Second, when you do need indexes into an NSArray, the correct type is NSUInteger. Don't mix and match types when you can help it.
Third, I don't know what you meant to do with the address-of operator there, but what you actually did was take the address of those two variables. Thus, you compared whether the local variable object is the same variable as the argument variable item. Since they're not the same variable, that test always returns false, which is why you never return an object—the only other exit point returns nil, so that's what always happens.
The problem with this code and the earlier one-liner is that you're attempting to go from a value to a single key, which is contrary to how dictionaries work: Only the keys are unique; any number of keys can have the same value.
You need to use something else as the items. Using the keys as the items would be one way; making a model object to represent each row would be another.
If you go the model-object route, don't forget to prevent multiple rows in the same virtual dictionary from having the same key. An NSMutableSet plus implementing hash and isEqual: would help with that.
You probably should also make the same change to your handling of arrays.
To clarify, I eventually resolved this problem by creating proxy objects for each of the collections in the plist file (so, for every NSMutableArray or NSMutableDictionary). This meant that I essentially mirrored the Plist structure and included references back to the original objects at each level. This allowed me to store the array index for each object or the dictionary key, so when saving items back from the outline view to the Plist structures, I used the 'key' or 'index' properties on the proxy object.

Arrays in Objective-C

If you want to use an array of characters/badguys is it better to have one array of badguy objects and each object have properties like Badguy1: color=blue, health=80. Then you loop through your array of characters and pull that information.... OR is it better to have multiple small arrays like character array, color array, health array and the index of all 3 arrays align to be the same character?
I know how to pull information from each array if it is separate but I do not know how to get the properties of each character if it is all wrapped up in 1 array.
I ask just because it seems like it would make more sense to use a single array and pull the parts that you need.
I know how to pull information from each array if it is separate but I do not know how to get the properties of each character if it is all wrapped up in 1 array. I ask just because it seems like it would make more sense to use a single array and pull the parts that you need.
You are correct in that it's generally smarter and a better design decision to keep related things together. If you'd like to store them this way, you can simply do:
// Get the character at index 3 in the "characters" array
// and print how many hit points it has left.
GameCharacter *ch = [characters objectAtIndex:3];
NSLog(#"This character has: %# hit points", ch.hitPointsRemaining);
Actually, now that I think about it. I guess I should just have an array of pointers and then get my values from the objects and not try to store all of that in the array.