Check if something exists in an NSMutableArray - iphone

I have an NSMutableArray which can hold several objects. I would like to check if an object exists, and if so, alter it. I was wondering about checking it. I thought this would work:
if ([[[self.myLibrary objectAtIndex:1] subObject] objectAtIndex:1]) // do something
However, this will crash if there aren't any subObjects at index 1.
So I guess the problem is that the above does not return nil if there isn't anything at this Index.
Is there another easy way to check or will I have to count through the array etc.? I know there are other posts on stackoverflow on this, but I haven't found a simple answer yet.
Any explanations / suggestions welcome. Thanks!

No check simply using :
[myArray indexOfObject:myObject];
or
[myArray containsObject:myObject];
These methods check every object using isEqual.
For example:
NSUInteger objIdx = [myArray indexOfObject: myObject];
if(objIdx != NSNotFound) {
id myObj = [myArray objectAtIndex: objIdx];
// Do some alter stuff here
}

If this is a pattern you use a lot, you could add a category to NSArray called something like safeObjectAtIndex:, which takes an index, does the bounds checking internally:
-(id)safeObjectAtIndex:(NSUInteger)index {
if (index >= [self count])
return nil;
return [self objectAtIndex:index];
}

Assuming the object you are using to search with and the actual object in the array are the exact same instance, and not two different objects that are equal according to an overridden isEqual: method, you can do this:
if ([array containsObject:objectToSearchFor]) {
// modify objectToSearchFor
}
If the two objects are different instances which are equal according to isEqual:, you will have to use code like this:
NSUInteger index = [array indexOfObject:objectToSearchFor];
if (index != NSNotFound) {
id objectInArray = [array objectAtIndex:index];
// modify objectInArray
}

NSArray (which is the NSMUtableArray superclass) has lots of methods for finding objects. Have a look at the documentation.
You can either rely on the equals method (e.g. indexOfObject:) or provide a block (e.g indexOfObjectPassingTest:) which is pretty funky.
It's fairly common in Objective C to be using the Mutable version of a class but rely on methods in the non mutable superclass so it's always a good idea when checking the online documentation to look at the superclass.

Related

Sorting IBOutletCollection Array of TextFields According to frame.origin.y

I'm trying to sort array of textFields according to frame.origin.y. But when I ran simulator it got stuck. Any Idea?
IBOutletCollection(UITextField) NSArray *textFields
My code :
-(NSMutableArray *)bubbleSort:(NSMutableArray *) unsortedArray{
NSInteger i,j;
//NSLog(#"%#",unsortedArray);
for(i=0;i<unsortedArray.count;i++)
{
for(j=0;j<i;j++)
{
if(((UITextField *)[unsortedArray objectAtIndex:i]).frame.origin.y > ((UITextField *)[unsortedArray objectAtIndex:j]).frame.origin.y)
{
UITextField *temp=[unsortedArray objectAtIndex:i];
[unsortedArray insertObject:[unsortedArray objectAtIndex:j] atIndex:i];
[unsortedArray insertObject:temp atIndex:j];
}
}
}
//NSLog(#"%#",unsortedArray);
return unsortedArray;
}
Like Learner said, you just keep on inserting object in your array, so you never leave your outer for loop. To swap two elements, you can use
- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2
so in your case, inside your if, you can simply write
[unsortedArray exchangeObjectAtIndex:i withObjectAtIndex:j];
Reference
look like this will keep on inserting object in array. so unsortedArray.count will keep increasing. in my opinion you need to remove object first and than insert it to upper rank or lower rank depending on algorithm.
se documentation for NSMutableArray insert object function.
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html

How will I be able to remove [NSNull Null] objects from NSMutableArray?

I need to remove Null object added by
[mutArrSkills addObject:[NSNull null]];
Do I need to iterate? Is there any function to remove all null values from NSMutableArray?
If need to Iterate, how will I do that?
You can use NSMutableArray's removeObjectIdenticalTo: method, as follows
[mutArrSkills removeObjectIdenticalTo:[NSNull null]];
to remove the null values. No need to iterate.
removeObjectIdenticalTo:
Removes all occurrences of a given object in the array.
Discussion
This method uses the indexOfObjectIdenticalTo: method to locate matches and then removes them by using removeObjectAtIndex:. Thus, matches are determined using object addresses. If the array does not contain anObject, the method has no effect (although it does incur the overhead of searching the contents).
You can try doing this,
NSNull *nullValue = [NSNull null];
[mutArrSkills removeObjectIdenticalTo:nullValue];
I hope this helps.
In Swift you first have to cast your Swift Array to NSArray, make and make it mutable so you can remove the Objective-C leftover elements, then cast it back to Array.
Fatal error: NSArray element failed to match the Swift Array Element type
// my crashing array, containing a not String element, like NSNull or anything else
let myUnsafeSwiftArray: [String]
// make it safely NSArray, then make it mutable
let mutableUnsafeArray = NSMutableArray(array: myUnsafeSwiftArray as NSArray)
// remove leftover class, like [NSNull null] aka NSNull.init()
unsafeTextures.removeObject(identicalTo: NSNull.init())
// Cast the safe array back to its supposed to by element type
let safeArray = unsafeTextures as? [String]
You may iterate like this.
for(int i=0,i<[mutArrSkills count]; i++)
{
if([[mutArrSkills objectAtIndex:i] isKindOfClass:[NSNull Class]])
{
[mutArrSkills removeObjectAtIndex:i];
}
}

I have a Objective C function that takes any type of object by reference. But when i pass a NSMutableArray My function does not recognise It

I have a function That takes by reference any kind of object
-(BOOL)RemoteCall:(id**)DataClass;
in the implementation i use [*DataClass isMemberOfClass:[NSMutableArray class] to find out the type of the object. The problem is it does not work with NSMUtableArrays Does anybody have a solution to this problem ? Here is the relevant code:
Implementation:
-(BOOL)RemoteCall:(id**)DataClass
{
if([*DataClass isMemberOfClass:[NSMutableArray class] ] == YES)
{
NSMutableArray * SW =(NSMutableArray *)*DataClass;
//do something with SW
DataClass= (id**)SW;
return TRUE;
}
}
Any help and I mean anything at all will be appreciated, I'm stuck.
Method Call:
NSMutableArray * channelArray = [[NSMutableArray alloc]init]
Services * serv = [[Services alloc] init];
return [serv RemoteCall:&channelArray];
Pass by reference in Objective-C is almost never the right way.
There are a number of problems with that code.
(id**) is a pointer to a pointer to a pointer to an object. Probably not at all what you want.
YES and NO are BOOL return types; not TRUE
there is no reason in that code to be returning something by reference.
method names start with lower case letters. Arguments do, too.
There will never be an instance of NSMutableArray in an application; just subclasses
You can't tell the difference between a mutable and immutable array in the first place; check for isKindOfClass: or isMemberOfClass: for an NSMutableArray won't do you much good (it is useful, but misleading).
This is better:
-(BOOL)remoteCall: (id) dataThing
{
if([dataThing isKindOfClass:[NSMutableArray class]] == YES)
{
NSMutableArray *swArray = dataThing; // not strictly necessary, but good defensive practice
//do something with swArray
return YES;
}
return NO;
}
To be called like:
NSMutableArray * channelArray = [[NSMutableArray alloc]init]; // you'll need to release this somewhere
Services * serv = [[Services alloc] init];
return [serv remoteCall:channelArray];
Since you don't return a different array in remoteCall:, channelArray's contents will be manipulated by the method and the YES/NO return value.
If there is some reason why the above seemingly won't work for you, please explain why.
Note: The code obviously requires an NSMutableArray if you are going to muck with the contents. The isKindOfClass: could be checking for NSMutableArray or NSArray and it wouldn't matter either way. When using arrays in your code and requiring a mutable array, it is up to you to make sure the data flow is correct such that you don't end up w/an immutable array where you need a mutable array.
If you don't need to reassign your variable, then don't use this. id or NSObject * is just fine and works by reference anyway. id * or NSObject ** would be references. id ** doesn't make sense at all here.
Also, learn naming conventions (like upper/lowercase).
NSArray is a class cluster. That means that every NSArray instance is actually an instance of some subclass. Only isKindOfClass: is useful for class-membership testing with class clusters.
Also... thats horrible code - please accept this:
-(BOOL)remoteCall:(id)dataClass {
if([dataClass isKindOfClass:[NSMutableArray class]]) {
NSMutableArray *sw =(NSMutableArray *)dataClass;
return YES;
}
}
that should work.
Constructive critisism of coding: You need to adhere to coding conventions. Although your code will work... its not brilliant to read and theres a lot of unnecessary *s and such.
Function names should be camel coded with a preceeding lower-case letter as should variable names. Passing (id) into a function doesn't require *s at all. Objects you pass into a function only available throughout the scope of the method anyway and that method doesn't own it, I'm not sure what you're trying to do with all the extra *s, but just treat objects you pass into the method as if you don't own them. :)
As Eiko said before, i'd use just id and not double pointers to ID.
I'm also pretty sure that isMemberOfClass is your Problem. isMember does not check for inheritance, so you're only asking for Top level Classes. isKindOfClass is probably the better choice, as there is no guarantee that Apple doesn't use an internal subclass of NSMutableArray internally. Check the Apple Docs.
i'd write it as such:
-(BOOL)RemoteCall:(id)dataClass
{
if([dataClass isKindOfClass:[NSMutableArray class] ] == YES)
{
NSMutableArray * SW =(NSMutableArray *)dataClass;
//do something with SW
return TRUE;
}
}

How can we limit the capacity of an NSMutableDictionary?

Is there any way to create a mutable dictionary with a capacity limit rather than an initial capacity?
Say you want to create a dictionary that will only ever have, at most, 100 entries.
The capacity argument for the dictionary is an initial capacity that will be simply increased if you add more elements than you want so it's not suitable.
Subclass it and override addObject to check count before adding? No built-in way.
Here is a basic example... not tested, missing init etc, but this is the core. You should also override setValue:forKey: in a similar manner.
#interface MyMutableDictionary: NSMutableDictionary
- (BOOL) setObject:(id)anObject forKey:(id)aKey;
#end
#implementation MyMutableDictionary
- (BOOL) setObject:(id)anObject forKey:(id)key {
if ([self count] < yourLimit) {
[super setObject:anObject forKey:key];
return YES;
}
return NO;
}
#end
There is no API for this, but the code to do so is fairly simple:
void AddObjectToArray(NSMutableArray *array, id obj, NSUInteger maxCount) {
if ([array count] < maxCount)
[array addObject: obj];
}
Note that the above code is not thread-safe, but making it thread safe is about as simple as wrapping the function's contents in a #synchronized (array) directive.
If you want an array that can be bandied about and have this limit built-in, don't subclass the NSMutableArray class cluster. NSMutableArray's contract (the "agreement" it has with calling code) says that a call to -addObject: will add that object to the array, not that it will if the array is under a limit.
Rather, an array with a maximum length behaves differently enough from the standard behaviour that it ought not to be a subclass of NSMutableArray. This may seem like a contrivance--it's an array and you can mutate it, therefore it should be treatable as an NSMutableArray. But calling code has no way to know that its modifications will fail silently on your subclass, and will probably not be designed with such failures in mind.
If the calling code is yours, you can use the helper function defined above--you know the objects might not be added and you can code appropriately. If the calling code comes from elsewhere (e.g. Cocoa or other Apple frameworks), use a normal NSMutableArray, and cull it when the calling code is done adding objects.

Using the value of a string to determine the instance of a class

I have a switch statement similar to this one:
switch (number)
{
case 1:
if (imageView1.hidden == NO)
{
imageView1.hidden = YES;
} else
{
imageView1.hidden = NO;
}
break;
case 2:
if (imageView2.hidden == NO)
{
imageView2.hidden = YES;
} else
{
imageView2.hidden = NO;
}
break;
And so forth and so on.
My question is how do I use a string with a value say "imageView1" and use that to access the instance of my imageView class instead of having a different case for each instance of imageView? I know it muse be similar to creating an NSPath from a string or something like that, but I'm just not sure where to look or what it would be called.
Thanks in advance for any help!
I don't disagree with those who are concerned about the design, if this is actually the code. I will assume, however, that you are only posting a generalized version of your question. And since this is an important concept in Objective-C, so we should talk about it.
You can access an object's properties by name using Key Value coding, and the routine -valueWithKey:.
NSString *nameOfView = #"imageView1";
[[self valueForKey:nameOfView] setHidden:YES];
This will, in order, look for a method called -imageView1, an ivar named imageView1 and finally an ivar named _imageView1. This technique is very heavily used in Cocoa, and is important to understand. This is one of the many reasons we name things carefully, and yet another reason that we make accessors that handle memory management for us. Search the docs for "Key-Value Compliance" for more information.
Now for this specific case, I would tend towards something more like JimG's solution, using an NSArray of views, so I can loop through them and turn on or off the ones I want based on their index. But I can imagine a lot of cases where that wouldn't be appropriate, and KVC may be.
Why not put the instances in an NSArray and index into that?
NSArray *views = [NSArray arrayWithObjects: imageView1, imageView2, nil];
NSImageView *iview = [views objectAtIndex: number];
Also, you could consider something like:
iview.hidden = ! iview.hidden;
[Edit: missing asterisks, oops]