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

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];
}
}

Related

NSInteger value not valid when passing in a variable, for custom table view class delegate method

I'm testing a custom table view style class:
HorizontalTable
It produces a horizontal table view.
One of the delegate methods equivalent to tableView:numberOfRowsInSection: is:
- (NSInteger)numberOfColumnsForTableView:(HorizontalTableView *)tableView.
If I give this a number (ex: return 10;) it is happy and it give me the number "cells" that I want. But if I feed it a value of someArray.count or an int or NSInteger variable, the table view just comes out blank, delivering no cells.
I think that the method in the custom table view class that receives the NSInteger value is this:
- (NSUInteger)numberOfPages {
NSInteger numPages = 0;
if (_delegate)
numPages = [_delegate numberOfColumnsForTableView:self];
return numPages;
}
Do I need to cast the result of someArray.count to an NSInteger?
Here you get the value from array means your array not nil so just debug and check the if condition that its come in that condition or not and what you get from NSLog
- (NSUInteger)numberOfPages {
NSInteger numPages = 0;
if (_delegate){
numPages = [_delegate numberOfColumnsForTableView:self];
NSLog(#"Total record %d",numPages);//what you get here?
}
return numPages;
}
numPages = [_delegate numberOfColumnsForTableView:self];
//self requires an object of type HorizontalTableView
NSMutableArray is editable, where as NSArray is read-only.
NSMutableArray is a subclass of NSArray and responds to messages such as addObject, removeObject and so forth; i.e. it is mutable, like the name says. Instead, NSArray is immutable, i.e. you can't add/remove objects.
In fact converting the value to be returned to the count of an NSArray, as opposed to the count an NSMutableArray fixes the issue. Why? I am not sure why an NSArray's count value is valid but not the NSMutableArray's. Anyone?

Typecasting array object

I struct at a point in my application.I have an array object called time and a global int variable as index..so when I need to use both I need to type cast array object to integer value.How can I do this?Please help..
NSArray *time=[NSArray arrayWithObjects:#"1.08",#"1.12",#"1.14",#"1.18",#"1.20",#"1.24",#"1.25",#"1.29",#"1.30",#"1.34",#"1.45",#"1.50",#"1.51",#"1.55",#"1.56",#"2.00",#"2.01",#"2.06",#"2.07",#"2.11",#"2.12",#"2.16",#"2.17",#"2.21",nil];
index = 0;
[self performSelector:#selector(startAnimation) withObject:self afterDelay:[time objectAtIndex:index]];
This is my code,but it is giving error and telling to typecast
Try [[time objectAtIndex:index] floatValue]. The objects in array are strings and you can get the float value by calling [myStr floatValue]. You can also use doubleValue here.

Check if something exists in an NSMutableArray

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.

How to updates array values in iphone app

I have to updates array values at position 2 and 4. How will I update array values. I am initializing my array like below.
ArrayrData=[[NSArray alloc]initWithArray:statuses]; // where statuses is also an array
You can't change the value of NSArray. So initialize your array as Mutable Array
NSMutableArray *ArrayrData=[[NSMutableArray alloc]init];
[ArrayrData addObjectsFromArray:statuses];
You can update the value in a NSMutableArray using,
– replaceObjectAtIndex:withObject:
– replaceObjectsAtIndexes:withObjects:
– replaceObjectsInRange:withObjectsFromArray:range:
– replaceObjectsInRange:withObjectsFromArray:
Use the method
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
if u want to replace them with new objects or simply access the objects at index 2 and 4 and then modify them using
[ArrayrData objectAtIndex:idx]; //idx is the index of the object you want to access, in ur case it is 2 or 4.
You can change the NSMutableArray dynamically in the application as follows:
NSMutableArray *ArrayData = [[NSMutableArray alloc]init];
[ArrayData addObjectsFromArray:statuses];
And now whenever you want to replace any object in array then you can simply replace using the "replaceObjectAtIndex" method as follows:
[ArrayData replaceObjectAtIndex:2 withObject:#"your Object or data"];
[ArrayData replaceObjectAtIndex:4 withObject:#"your Object or data"];
Let me know if you have any questions.

Core Data object into an NSDictionary with possible nil objects

I have a core data object that has a bunch of optional values. I'm pushing a table view controller and passing it a reference to the object so I can display its contents in a table view. Because I want the table view displayed a specific way, I am storing the values from the core data object into an array of dictionaries then using the array to populate the table view. This works great, and I got editing and saving working properly.
(i'm not using a fetched results controller because I don't have anything to sort on)
The issue with my current code is that if one of the items in the object is missing, then I end up trying to put nil into the dictionary, which won't work.
I'm looking for a clean way to handle this, I could do the following, but I can't help but feeling like there's a better way.
*passedEntry is the core data object handed to the view controller when it is pushed, lets say it contains firstName, lastName, and age, all optional.
if ([passedEntry firstName] != nil) {
[dictionary setObject:[passedEntry firstName] forKey:#"firstName"]
}
else {
[dictionary setObject:#"" forKey:#"firstName"]
}
And so on. This works, but it feels kludgy, especially if I end up adding more items to the core data object down the road.
What you could do is iterate through all of the object's properties using the objc_* runtime functions like so:
unsigned int property_count;
objc_property_t * prop_list = class_copyPropertyList([CoreDataObject class], &property_count);
for(int i = 0; i < property_count; i++) {
objc_property_t prop = prop_list[i];
NSString *property_name = [NSString stringWithCString:property_getName(prop) encoding:NSUTF8StringEncoding];
id obj = [passedEntry valueForKey:property_name];
[dictionary setObject:((obj != nil) ? obj : [NSNull null]) forKey:property_name];
}
free(prop_list);