I have 2 NSArrays with a bunch of NSStrings. I want to do an array subtraction, so I get all the elements from Array1 that are only in Array1 and not also in Array2. These NSString objects are different objects, but with the same string values.
Is there an easy way to do this, or do I need a double loop? In python, for example I could use a set operation, but I'm not sure how to do it in Obj-C.
NSMutableSet gives you much of set operations you get in python
Related
How do you fill a NSMutableArray with a set capacity for later use?
Basically I want to set up a NSMutableArray to act as a map for my game objects, so I have this line...
gameObjects = [[NSMutableArray alloc] initWithCapacity:mapWidth*mapHeight];
Which I had hoped would create and fill my MutableArray so I can get then access it with this kind of index...
int ii = (cellY*mapWidth)+cellX;
NSDictionary *currentObject = [gameObjects objectAtIndex:ii];
But I just learned initWithCapacity doesn't fill the array, so should I create blank objects to fill it with, or is there a Null that I can fill it with? Also would I do that with 2 for loops or is there an instruction something like "initWith:myObject" ?
I want to be able to check at a certain index within the array to see if there's an object there or not, so I need to be able to acces that index point, and I can only do that if there's something there or I get an out of bounds error.
I'll be using this NSMutableArray pretty much as a grid of objects, it's a 1 dimensional array organised as a 2 dimensional array, so I need to be able to fill it with mapWidth*mapHeight of something, and then calculate the index and do a check on that index within the array.
I've looked on here and googled but couldn't find anything like what I'm asking.
Thanks for any advice.
I think what you are looking for is [NSNull null]. It is exactly what you want- a placeholder value.
You can find more information on the topic in this question.
initWithCapacity is just a performance optimization -- it has no effect on the array behavior, it just keeps the code "under the covers" from having to repeatedly enlarge the internal array as you add more entries.
So if you want a "pre-allocated" array, you'd need to fill it with NSNull objects or some such. You can then use isKindOfClass to tell if the object is the right type, or simply == compare the entry to [NSNull null]. (Since there's only ever one NSNull object it always has the same address).
(Or you could use a C-style array of pointers with nil values for empty slots.)
But you might be better off using an NSMutableDictionary instead -- no need to pre-fill, and if the element isn't there you get a nil pointer back. For keys use a NSNumber object that corresponds to what would have been your array index.
initWithCapacity only hints to NSMutableArray that it should support this many objects. It won't actually have any objects in it until you add them. Besides, every entry in the array is a pointer to an object, not a struct like you'd normally have in a standard c array.
You need to change how you're thinking about the problem. If you don't add an object to the array, it's not in there. So either you pre-fill the array with "empty" objects as you've said, which is weird. Or you can add the objects as you need them.
I have an array which contains objects some may be same and some are different.
How can I take each same objects and different objects separately ?
Below is the array
NSMutableArray *items = [[NSMutableArray alloc]
initWithArray:[NSArray arrayWithObjects:#"rat", #"rat", #"cat",#"Lion", #"cat", #"dog", #"dog", nil]];
I want to have four arrays which will contains these items :
First array with two rats
2nd array with two cats
3rd array with one lion
4th array with two dogs
What could be the best way to take the objects out ? Identical object should be placed in same array.
Here's a general answer:
Put the array into an NSCountedSet - that will store each object and a count of the number of times it has been added.
Then - for each object in this counted set create an array with that object repeated according to the count of each object.
It will work in your case, because you are using static strings, which are going to be the same if they are the same string. This will take more work if you are using custom objects.
But the real question we have to ask is why you need to create these repetitive structures. If we could know what you are doing with it, we could give you better advice about how to go about it. For example, if you just need to keep a running count of the number of each type of object you have, you could just use the NSCountedSet directly (it descends from NSMutableSet, so it is already mutable) and not bother with creating the arrays.
I have an NSArray consisting of NSNumbers and I want to convert this to an NSArray of NSStrings, by getting the stringValue of each NSNumber in the first array.
The method that comes to my mind is iterating each value in the first one, getting its string value and adding it into another array. But there should be a more elegant solution for this. Do you know one?
NSArray implements the Key-Value Coding method valueForKey: in such a way that it returns a new array. The new array contains the results of asking each object in the original array for the specified value. In this case, NSNumber has its stringValue, so all you have to do is:
NSArray * b = [a valueForKey:#"stringValue"];
Plain old fast enumeration (or enumerateObjectsUsingBlock:) wouldn't be a terrible solution, though. NSArray's implementation of valueForKey: most likely uses a for loop internally, and that would be pretty readily understood by anyone who reads it later.
OK this is probably a really stupid question but I can't seem to find the answer.
I know how to sort arrays using sort descriptors but what about a simple array containing only a series of numbers?
For example an array containing the following numbers:
21,3,11,58,32,76,19,45,7,92
I just need the numbers in ascending order.
I'm assuming you mean an NSArray of NSNumbers.
Fairly simple:
NSArray *unorderedNumbers; // assume exists
NSArray *sortedArray = [unorderedNumbers
sortedArrayUsingSelector:#selector(compare:)];
(See sortedArrayUsingSelector:).
This causes the objects (which happen to be numbers) to be compared using compare:.
The C standard library includes qsort
See: http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/
I want to have a mutable array with primitives in obj-c (selectors). What's the recommended way to do this? NSArray and those can only hold objects.
You should use an NSValue to wrap the selector or any other primitive type you need. In Cocoa SEL is some kind of pointer, so you can use [NSValue valueWithPointer:whatever] to construct it and [value pointerValue] to get it out. Or, in general you can use [NSValue valueWithBytes:&whatever objCType:#encode(SEL)]; this works for any type.
If you want to store an array of SEL objects, the easiest thing would be to convert the SELs to NSStrings using the NSStringFromSelector() function, store them in an NSMutableArray, and then convert them back to SELs when you pull them out using NSSelectorFromString() function.
Other than managing a C-style array yourself (which is definitely not the best option, IMO), your only option is to use NSArray/NSMutableArray, and store the numbers using NSNumber. It's slightly more annoying to get the value out than with the actual numeric type, but it does free you from managing the array's memory yourself.
Since the primitive types are generally just numbers (be they integer or floating-point) or pointers, what's the problem with using the classes used to wrap those up for your purposes? An NSMutableArray of NSNumbers, for example?