nsset

How to turn an NSArray of strings into an array of unique strings, in the same order?

旧城冷巷雨未停 提交于 2019-11-29 21:26:17
If you have an NSArray of strings { @"ONE", @"ONE", @"ONE", "TWO", @"THREE", @"THREE" } How would I turn that into { @"ONE", @"TWO", @"THREE" } ..where the array follows the same order as the original. I think that you can turn an array into an NSSet to get unique items, but if you turn it back into an array you are not guaranteed to get the same order.. My initial thought was that you could do: NSArray * a = [NSArray arrayWithObjects:@"ONE", @"ONE", @"ONE", @"TWO", @"THREE", @"THREE", nil]; NSLog(@"%@", [a valueForKeyPath:@"@distinctUnionOfObjects.self"]); But that does not maintain order.

Quick way to sum a property of all objects within an NSSet?

拟墨画扇 提交于 2019-11-29 13:49:54
I thought there was a way to quickly ask a NSSet to poll its members and return a sum of say an NSInteger property in each of its objects, but I may very well be confusing this with the Mac OS X side of things. Does this exist in Cococa Touch? The closest thing I can find is objectEnumerator , whereby I suppose I could rifle through each object and increment my own variable. Does the better way exist? If you're trying to find the sum of a given property ( theIntegerPropertyToSum ) for each member of an array/set-derived class that's KVC-compliant ( theSet ), you can do the following: NSNumber*

NSSet implementation

梦想的初衷 提交于 2019-11-29 09:24:46
This question is just out of curiosity but, how is NSSet implemented? What data structure is behind it and what are the access times for adding and removing elements? If I had to guess, I'd say it was some sort of hashtable/dictionary data structure, but in that case why differentiate between NSSet and NSMutableSet? Well, as Bavarious pointed out in a comment, Apple's actual CoreFoundation source is open and available for your perusal too. NSSet is implemented on top of CFSet , whose code is generated (as is that of CFDictionary ) from a hash table template, using CFBasicHash to do the work.

Objective c - NSMutableSet unique object property

女生的网名这么多〃 提交于 2019-11-29 05:37:31
In my app I have a class Person with personId property. Now I need some data structure to hold a bunch of unique Person objects (unique = different personId) So I guess I should use NSMutableSet as my data structure, but how do I make the NSMutableSet compare the personId property when adding a person (so I won't add the same person more then ones)? My goal is to have a collection of unique persons all the time (even if I add two persons with the same id), I want that the NSMutableSet will do all the hard work for me and if I'm adding a person that already exists it won't add it twice. You can

What is the most efficient way to sort an NSSet?

China☆狼群 提交于 2019-11-28 20:59:46
What's the most efficient way to sort objects in an NSSet / NSMutableSet based on a property of the objects in the set? Right now the way I am doing it is by iterating through each object, add them to a NSMutableArray , and sort that array with NSSortDescriptor . try using [[mySet allObjects] sortedArrayUsingDescriptors:descriptors]; Edit : For iOS ≥ 4.0 and Mac OS X ≥ 10.6 you can directly use [mySet sortedArrayUsingDescriptors:descriptors]; The "most efficient way" to sort a set of objects varies based on what you actually mean. The casual assumption (which the previous answers make) is a

Getting an object from an NSSet

孤街醉人 提交于 2019-11-28 16:46:37
If you can't get an object with objectAtIndex: from an NSSet then how do you retrieve objects? There are several use cases for a set. You could enumerate through (e.g. with enumerateObjectsUsingBlock or NSFastEnumeration), call containsObject to test for membership, use anyObject to get a member (not random), or convert it to an array (in no particular order) with allObjects . A set is appropriate when you don't want duplicates, don't care about order, and want fast membership testing. NSSet doesn't have a method objectAtIndex: Try calling allObjects which returns an NSArray of all the objects

Quick way to sum a property of all objects within an NSSet?

て烟熏妆下的殇ゞ 提交于 2019-11-28 07:35:53
问题 I thought there was a way to quickly ask a NSSet to poll its members and return a sum of say an NSInteger property in each of its objects, but I may very well be confusing this with the Mac OS X side of things. Does this exist in Cococa Touch? The closest thing I can find is objectEnumerator , whereby I suppose I could rifle through each object and increment my own variable. Does the better way exist? 回答1: If you're trying to find the sum of a given property ( theIntegerPropertyToSum ) for

iOS - Most efficient way to find word occurrence count in a string

北城以北 提交于 2019-11-28 07:00:31
Given a string, I need to obtain a count of each word that appears in that string. To do so, I extracted the string into an array, by word, and searched that way, but I have the feeling that searching the string directly is more optimal. Below is the code that I originally wrote to solve the problem. I'm up for suggestions on better solutions though. NSMutableDictionary *sets = [[NSMutableDictionary alloc] init]; NSString *paragraph = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"text" ofType:@"txt"] encoding:NSUTF8StringEncoding error:NULL]; NSMutableArray

NSSet implementation

两盒软妹~` 提交于 2019-11-28 02:50:12
问题 This question is just out of curiosity but, how is NSSet implemented? What data structure is behind it and what are the access times for adding and removing elements? If I had to guess, I'd say it was some sort of hashtable/dictionary data structure, but in that case why differentiate between NSSet and NSMutableSet? 回答1: Well, as Bavarious pointed out in a comment, Apple's actual CoreFoundation source is open and available for your perusal too. NSSet is implemented on top of CFSet, whose code

Getting an object from an NSSet

元气小坏坏 提交于 2019-11-27 09:55:13
问题 If you can't get an object with objectAtIndex: from an NSSet then how do you retrieve objects? 回答1: There are several use cases for a set. You could enumerate through (e.g. with enumerateObjectsUsingBlock or NSFastEnumeration), call containsObject to test for membership, use anyObject to get a member (not random), or convert it to an array (in no particular order) with allObjects. A set is appropriate when you don't want duplicates, don't care about order, and want fast membership testing.