initialise an NSArray with an unknown size - objective-c

Once an Array is initialized, in order to set value of desired position, I am using
[self.appName replaceObjectAtIndex:x withObject:[self.appCell objectAtIndex: 0]];
Problem is that if I initialize appName array without objects, this array keeps empty and I must initialize it using initWithObjects and then works. Problem is that I do not know the size of an array and if I set it like:
NSMutableArray *nameArray = [[ NSMutableArray alloc] initWithObjects:#"test",#"test",#"test",#"test", nil];
self.appName = nameArray;
[nameArray release];
For example, works from 0 to 3 but from position 4 to following ones, after replaceObjectAtIndex, position has a nil value. How to solve it? Thank you

It's a mutable array, you can add objects (to the end) and insert objects (in the middle) as well, whenever you like:
NSMutableArray *nameArray = [[NSMutableArray alloc] init];
[nameArray addObject:#"test"];
[nameArray insertObject:#"another test" atIndex:0];
[nameArray removeObjectAtIndex:1];
You can do this anywhere you have a pointer to the mutable array.

Look at the count methon on NSArray it gives you the number of elements in the array. So check that your index x is less than the count then you can remove the element.

Related

access object properties from a multidimensional array in objective-c

I am new to Objective-C programming and I am trying to access object properties from a 2 dimensional array.
First I created two arrays, each of those arrays contains objects, then I made a 2 dimensional array that contains those arrays of objects by using NSMutableArray
NSMutableArray *team1 = [[NSMutableArray alloc] init];
[team1 addObject:tank1];
[team1 addObject:btr1];
[team1 addObject:ambulance1];
NSMutableArray *team2 = [[NSMutableArray alloc] init];
[team2 addObject:tank2];
[team2 addObject:btr2];
[team2 addObject:ambulance2];
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:2];
[array addObject:team1];
[array addObject:team2];
What I want to do now is to access the properties of these objects by referring them from my 2d array and print them by using NSLog. Is this possible?
Please excuse me my question looks complicated, this is something new for me.
Use the above reference code to access the object like this.
1. To access btr 1
index for this will be : item 0, object 1 for array so can access by this code.
[[array objectAtIndex:0] objectAtIndex:1]
2. To access ambulance2
index for this will be : item 0, object 1 for array so can access by this code.
[[array objectAtIndex:1] objectAtIndex:2]
You can print them with same code given here as
NSlog(Item at object: %d index of 'array' and item in 'array' at index : %d is == %#,outerarray(array) index, innerarray(item array) index, [[array objectAtIndex:outerarrayindex] objectAtIndex:innerarrayindex]);
Or simply NSlog(#"%#",[[array objectAtIndex:outerarrayindex] objectAtIndex:innerarrayindex]);

Remove null objects from mutable array in objective c

I have a mutable array of checked box
NSMutableArray *arrayOfCheckedBox = [NSMutableArray arrayWithObjects:namePropertyString, lastNamePropertyString, companyPropertyString, workEmailPropertyString, personalEmailPropertyString, workPhonePropertyString, cellNumberPropertyString, nil];
[arrayOfCheckedBox removeObjectIdenticalTo:[NSNull null]]; //not working
NSLog(#"array of check box = %#", arrayOfCheckedBox);
If I click on check boxes at index 0, 1 and 4, it will only collect object at indexes 0 and 1 only and will not detect index 4 at all.
I get the values at the selected index in log before getting it in arrayOfCheckedBox. How to get checked values in this case?
The problem is that you're hitting a nil value, so the arrayWithObjects: method thinks you're at the end of the list of objects.
Something like this will work:
NSMutableArray *arrayOfCheckedBox = [NSMutableArray arrayWithCapacity:7];
if (namePropertyString)
[arrayOfCheckedBox addObject:namePropertyString];
if (lastNamePropertyString)
[arrayOfCheckedBox addObject:lastNamePropertyString];
I would suggest storing the check box values in an array of BOOLs.
Wrap them in NSNumbers first and unwrap them to retrieve them.
Wrap to store,
[NSNumber numberWithBool:YES]]
Unwrap to retrieve,
[[your_array objectAtIndex:index] boolValue]
For null values appear to be string literals #"" rather than the NSNull.So use following way to remove null object :
NSLog(#"%d", [arrayOfCheckedBox count]);
NSString *nullString = #"<null>";
[arrayOfCheckedBox removeObject:nullString];
NSLog(#"%d", [arrayOfCheckedBox count]);

Load an element value of an array to another array Xcode Objective-C

Here I am getting the cityName1 with the city names like Piscataway, Iselin, Broklyn etc fetched from the tgpList1 array and I need to put the values into an array called item5.
There are 133 records fetched by the above iteration. The following code stores only the last record's cityName1 and not the entire list of city names though inside the loop.
I tried many ways but I am missing something.
tgpList1 is an array.
tgpDAO is an NSObject with two objects NSString *airportCode and NSString *cityName
NSArray *item5 = [[NSArray alloc]init];
for (int currentIndex=0; currentIndex<[tgpList1 count]; currentIndex++)
{
tgpDAO *tgpTable = (tgpDAO *)[self.tgpList1 objectAtIndex:currentIndex];
NSLog(#"The array values are %#",tgpList1);
NSString *cityName1 = tgpTable.cityName;
item5 =[NSArray arrayWithObjects:cityName1, nil];
}
Use mutable array.
{
NSMutableArray *item5 = [[NSMutableArray alloc]initWithArray:nil];
for (int currentIndex=0; currentIndex<[tgpList1 count]; currentIndex++) {
tgpDAO *tgpTable = (tgpDAO *)[self.tgpList1 objectAtIndex:currentIndex];
NSLog(#"The array values are %#",tgpList1);
NSString *cityName1 = tgpTable.cityName;
[item5 addObject:cityName1];
}
}
Instead of
item5 =[NSArray arrayWithObjects:cityName1, nil];
use
[item5 addObject:cityName1];
There are more ways of achieving that. However, this is the one that is designed for that purpose and the most "readable" from my pont of view.
If you need to clear the contents of item5 before then call
[item5 removeAllObjects];
right before the for loop.
What you were doing: arrayWithObjects allways creates a new array that ist made of the objects that are passed to it as aguments. If you do not use ARC, then you would create some serious memory leak with your code because arrayWithObjects creates and retains an object on every loop and on the next loop all references to the array object, that was just created, are lost without being released. If you do ARC then you do not have to worry about in this case.
NSMutableArray *myCities = [NSMutableArray arrayWithCapacity:2]; // will grow if needed.
for( some loop conditions )
{
NSString* someCity = getCity();
[myCities addObject:someCity];
}
NSLog(#"number of cities in array: %#",[myCities count]);

How does one populate an NSMutable array of NSMutableSets?

I am using this code in a loop to populate an NSMutable Array of NSMutableSets (of NSString objects). The index of the NSSet is based on the length of the word.
// if set of this length not initialized yet, initialize set.
wordIndex = [NSString stringWithFormat:#"%d", currentWordLength];
if ([myWordArray objectForKey:wordIndex] == nil)
[myWordArray setObject:[[NSMutableSet alloc] initWithObjects:currentWord, nil] forKey:wordIndex];
else
[[myWordArray objectForKey:wordIndex] addObject:currentWord];
The final intention is to split up an array of words into an array of sets of words grouped by their lengths.
However, I see that [myWordArray count] is 0 after this. Why?
You are confusing the methods of NSMutableDictionary and NSMutableArray: In Objective-C arrays do not have keys but have indexes. If you change the class for myWordArray to NSMutableDicitionary it should work.
Try this, it looks very much like your logic, but (1) it uses NSNumbers as keys, which makes a little more sense, (2) handles the missing set condition more simply, but just adding the set, and (3) breaks up the source lines somewhat for easier debugging...
NSArray *inputStrings = // however these are initialized goes here
NSMutableDictionary *result = [NSMutableDictionary dictionary];
for (NSString *currentString in inputStrings) {
NSInteger currentWordLength = currentString.length;
wordIndex = [NSNumber numberWithInt:currentWordLength];
NSMutableSet *wordSet = [result objectForKey:wordIndex];
if (!wordSet) {
wordSet = [NSMutableSet set];
[result setObject:wordSet forKey:wordIndex];
}
[wordSet addObject:currentWord];
}
If you still have an empty dictionary after running this, it might be simpler to watch what's happening by stepping through it.

Trouble removing NSStrings in NSArray

In an NSArray, I have a 10 values. The values look something like this:
Hello
Hello2
My
My2
Name
Name2
Is
Is2
XcodeDev
XcodeDev2
And I want to delete every second NSString from the NSArray, so I am left with an array like the following. The values are random, and do not have 2 appended to every second one! How can I do this?
Hello
My
Name
Is
XcodeDev
First, you are going to need an instance of NSMutableArray, because NSArrays are immutable and therefore, you cannot change its contents.
NSMutableArray *ary = [NSMutableArray arrayWithArray:anImmutableArray];
Then, you can create an index set that holds all the odd indexes:
NSMutableIndexSet *indexSet = [[NSMutableIndexSet alloc] init];
for (int i = 1; i < [ary count]; i=i+2) {
[indexSet addIndex:i];
}
Finally, just call removeObjectsAtIndexes: method on the array.
[ary removeObjectsAtIndexes:indexSet];