words from letters - ios

I am working in iOS, and I am facing one problem. I have 6 letters, and I want to find all strings that can be generated by these 6 letters. The sequence and length of the string doesn't matter here, also the meaning of the string doesn't matter because we can check the string with a dictionary to find if it is a valid word or not.
So is there any algorithm to find this?
For example:
Input 6 letters are : N T L P A E
Expected output will be:
plan
net
planet
lan
tea
lap
..
..
Here the words in the output are only the valid words, but the output should contain all possible words, even invalid words.

This should probably solve this:
+ (void) logPermutations:(NSArray*)objects
{
if (objects == nil || [objects count] == 0) {
return;
}
NSMutableArray* copy = [objects mutableCopy];
[self logPermutations:copy logedSoFar:#""];
}
+ (void) logPermutations:(NSMutableArray*)objects
logedSoFar:(NSString*)log
{
if (objects == nil || [objects count] == 0) {
return;
}
NSUInteger count = [objects count];
for (NSUInteger i = 0; i < count; ++i) {
id removed = [objects objectAtIndex:i];
[objects removeObjectAtIndex:i];
NSString* newlog = [NSString stringWithFormat:#"%#%#",log,[removed description]];
NSLog(#"%#",newlog);
[self logPermutations:objects logedSoFar:newlog];
[objects insertObject:removed atIndex:i];
}
}

-(NSMutableArray*)genValidWords:(NSString*)tiles
{
/*===============EMPTY PREVIOUS ARRAY=================*/
FINAL_VALID_WORDS=[[NSMutableArray alloc] init];
posWords=[[NSMutableArray alloc] init];
genWords=[[NSArray alloc] init];
/*============EMPTY PREVIOUS ARRAY FINISH=============*/
/*===============POSSIABLE NO OF WORDS FROM GIVEN LETTERS=================*/
int length=[tiles length];
int total=0;
for(long i=2;i<[tiles length]+1;i++)
total=total+([self factorialX:length]/[self factorialX:length-i]);
NSLog(#"POSSIABLE NO OF WORDS FROM LETTERS \"%#\" IS %d",tiles,total);
/*===============POSSIABLE NO OF WORDS FROM GIVEN LETTERS FINISH=================*/
/*===============GET ARRAY OF ALL POSSIABLE WORDS FROM GIVEN LETTERS=============*/
for(int i=2;i<[tiles length]+1;i++)
{
genWords=getPermutations(tiles, i);
[posWords addObjectsFromArray:genWords];
}
NSLog(#"%#",posWords);
return posWords;
}
static NSMutableArray *results;
void doPermute(NSMutableArray *input, NSMutableArray *output, NSMutableArray *used, int size, int level)
{
if (size == level)
{
NSString *word = [output componentsJoinedByString:#""];
//if(check(word))
[results addObject:word];
return;
}
level++;
for (int i = 0; i < input.count; i++)
{
if ([used[i] boolValue])
{
continue;
}
used[i] = [NSNumber numberWithBool:YES];
[output addObject:input[i]];
doPermute(input, output, used, size, level);
used[i] = [NSNumber numberWithBool:NO];
[output removeLastObject];
}
}
NSArray *getPermutations(NSString *input, int size)
{
results = [[NSMutableArray alloc] init];
NSMutableArray *chars = [[NSMutableArray alloc] init];
for (int i = 0; i < [input length]; i++)
{
NSString *ichar = [NSString stringWithFormat:#"%c", [input characterAtIndex:i]];
[chars addObject:ichar];
}
NSMutableArray *output = [[NSMutableArray alloc] init];
NSMutableArray *used = [[NSMutableArray alloc] init];
for (int i = 0; i < chars.count; i++)
{
[used addObject:[NSNumber numberWithBool:NO]];
}
doPermute(chars, output, used, size, 0);
return results;
}
-(double) factorialX: (int) num {
double tempResult = 1;
for (int i=2; i<=num; i++) {
tempResult *= i;
}
return tempResult;
}
/*=======================Call Like this====================
NSmutableArray=getPermutations("Pass your string", pass your lenght);
NsmutableArray=getPermutations("Janak", 2);
NSMutableArray=[self genValidWords:#"Hello"];
will return all possiable combination of two letters
=======================xxxxxxxxxx====================*/

I got the solution,
I want something like following.
-(NSArray*)totalWords:(NSArray*)letters
{
NSMutableArray *output=[[NSMutableArray alloc]init];
for (int i=0; i<letters.count; i++)
{
for (int j=0; j<letters.count; j++)
{
if (i==j) continue;
for (int k=0; k<letters.count; k++)
{
NSString *str=[NSString stringWithFormat:#"%#%#%#",letters[i],letters[j],letters[k]];
if(i!=j && j!=k && i!=k &&[self checkmeaning:str])
[output addObject:str];
for (int l=0; l<letters.count; l++)
{
NSString *str=[NSString stringWithFormat:#"%#%#%#%#",letters[i],letters[j],letters[k],letters[l]];
if(i!=j && j!=k && i!=k && l!=i && l!=j && l!=k &&[self checkmeaning:str])
[output addObject:str];
for (int m=0; m<letters.count; m++)
{
NSString *str=[NSString stringWithFormat:#"%#%#%#%#%#",letters[i],letters[j],letters[k],letters[l],letters[m]];
if(i!=j && j!=k && i!=k && l!=i && l!=j && l!=k && m!=i && m!=j && m!=k && m!=l &&[self checkmeaning:str])
[output addObject:str];
for (int n=0; n<letters.count; n++)
{
NSString *str=[NSString stringWithFormat:#"%#%#%#%#%#%#",letters[i],letters[j],letters[k],letters[l],letters[m],letters[n]];
if(i!=j && j!=k && i!=k && l!=i && l!=j && l!=k && m!=i && m!=j && m!=k && n!=i && n!=j && n!=k &&n!=m &&n!=l && m!=l&&[self checkmeaning:str])
[output addObject:str];
}
}
}
}
}
}
NSLog(#"count :%i",[output count]);
NSLog(#"output array :\n%#",output);
return output;
}

Related

Objective-C check if item exists in NSMutableArray

I have this section of code here:
for (int i = 0; i<[taskData count]; i++)
{
for (int j=0; j<[allJobTaskArray count]; j++)
{
NSLog(#"%d", i);
NSLog(#"%d", j);
PunchListDataCell *pldCell = [[[PunchListDataCell alloc]init] autorelease];
pldCell.stringData= [self reverseStringDate:[[[allJobTaskArray objectAtIndex:j] objectAtIndex:i] substringToIndex:10]];
pldCell.cellSelected = NO;
[punchListData addObject:pldCell];
}
}
Now let me explain:
taskData count is 57 and is a NSArray
allJobTaskArray count is 12 and is a NSMutableArray
This code will crash at this line: pldCell.stringData= [self reverseStringDate:[[[allJobTaskArray objectAtIndex:j] objectAtIndex:i] substringToIndex:10]]; when j is 6 and i is 36 simple because in allJobTaskArray objectAtIndex: 6 objectAtIndex: 36 does not exist.
This is the error I am getting: [__NSCFArray objectAtIndex:]: index (36) beyond bounds (36)
What I am trying to do is if the item does not exist, then pldCell should equal #"";
My question is how would I check if I am item exist or not ?
I have tried the following:
if([[allJobTaskArray objectAtIndex:j] objectAtIndex:i] == [NSNull null]){
pldCell.stringData = #"";
}else{
pldCell.stringData= [self reverseStringDate:[[[allJobTaskArray objectAtIndex:j] objectAtIndex:i] substringToIndex:10]];
}
Altogether it should look something like -
for (int i = 0; i<[taskData count]; i++)
{
for (int j=0; j<[allJobTaskArray count]; j++)
{
NSLog(#"%d", i);
NSLog(#"%d", j);
PunchListDataCell *pldCell = [[[PunchListDataCell alloc]init] autorelease];
if ([[allJobTaskArray objectAtIndex:j] count] > i) {
pldCell.stringData= [self reverseStringDate:[[[allJobTaskArray objectAtIndex:j] objectAtIndex:i] substringToIndex:10]];
} else {
pldCell.stringData = #"";
}
pldCell.cellSelected = NO;
[punchListData addObject:pldCell];
}
}

Replace simultaneously multiple char in NSString

I am trying to replace multiple character found in a string at once not using stringByReplacingOccurrencesOfString. So if my string is: #"/BKL_UO+C-" I what to change / to _ ; - to +; + to -; _ to /.
In my code I have tried to do this by applying for each of the signs that I want to replace first the stringByReplacingOccurrencesOfString and save it as a new string and created an NSArray of chars with them. Compared the differences and save them in another array where I push all the differences from all 4 arrays. Then apply all differences to the initial string.
Initial hash string is: #"lRocUK/Qy+V2P3yDhCd74RvHjCDzlTfrGMolZZE0pcQ"
Expected result: #"lRocUK_Qy-V2P3yDhCd74RvHjCDzlTfrGMolZZE0pcQ"
NSMutableArray *originalHashArrayOfCharFromString = [NSMutableArray array];
for (int i = 0; i < [hash length]; i++) {
[originalHashArrayOfCharFromString addObject:[NSString stringWithFormat:#"%C", [hash characterAtIndex:i]]];
}
//1 change
NSString *backslashRplecedInHashString = hash;
backslashRplecedInHashString = [backslashRplecedInHashString stringByReplacingOccurrencesOfString:#"/" withString:#"_"];
NSMutableArray *hashConvertBackslashToUnderline = [NSMutableArray array];
for (int i = 0; i < [hash length]; i++) {
[hashConvertBackslashToUnderline addObject:[NSString stringWithFormat:#"%C", [backslashRplecedInHashString characterAtIndex:i]]];
}
//2 change
NSString *minusRplecedInHashString = hash;
minusRplecedInHashString = [minusRplecedInHashString stringByReplacingOccurrencesOfString:#"-" withString:#"+"];
NSMutableArray *hashConvertMinusToPlus = [NSMutableArray array];
for (int i = 0; i < [hash length]; i++) {
[hashConvertMinusToPlus addObject:[NSString stringWithFormat:#"%C", [minusRplecedInHashString characterAtIndex:i]]];
}
//3 change
NSString *underlineRplecedInHashString = hash;
underlineRplecedInHashString = [underlineRplecedInHashString stringByReplacingOccurrencesOfString:#"_" withString:#"/"];
NSMutableArray *hashConvertUnderlineToBackslash = [NSMutableArray array];
for (int i = 0; i < [hash length]; i++) {
[hashConvertUnderlineToBackslash addObject:[NSString stringWithFormat:#"%C", [underlineRplecedInHashString characterAtIndex:i]]];
}
//4 change
NSString *plusRplecedInHashString = hash;
plusRplecedInHashString = [plusRplecedInHashString stringByReplacingOccurrencesOfString:#"+" withString:#"-"];
NSMutableArray *hashConvertPlusToMinus = [NSMutableArray array];
for (int i = 0; i < [hash length]; i++) {
[hashConvertPlusToMinus addObject:[NSString stringWithFormat:#"%C", [plusRplecedInHashString characterAtIndex:i]]];
}
NSMutableArray *tempArrayForKey = [[NSMutableArray alloc] init];
NSMutableArray *tempArrayForValue = [[NSMutableArray alloc] init];
int possitionInTempArray = 0;
//Store all changes of original array in a temp array
//1 replace
for (int countPosssition = 0 ; countPosssition < [hash length]; countPosssition++) {
if ([originalHashArrayOfCharFromString objectAtIndex:countPosssition] != [hashConvertBackslashToUnderline objectAtIndex:countPosssition]) {
[tempArrayForValue addObject:[hashConvertBackslashToUnderline objectAtIndex:countPosssition]];
[tempArrayForKey addObject:[NSNumber numberWithInt:countPosssition]];
possitionInTempArray++;
}
}
//2 replace
for (int countPosssition = 0 ; countPosssition < [hash length]; countPosssition++) {
if ([originalHashArrayOfCharFromString objectAtIndex:countPosssition] != [hashConvertMinusToPlus objectAtIndex:countPosssition]) {
[tempArrayForValue addObject:[hashConvertMinusToPlus objectAtIndex:countPosssition]];
[tempArrayForKey addObject:[NSNumber numberWithInt:countPosssition]];
possitionInTempArray++;
}
}
//3 replace
for (int countPosssition = 0 ; countPosssition < [hash length]; countPosssition++) {
if ([originalHashArrayOfCharFromString objectAtIndex:countPosssition] != [hashConvertUnderlineToBackslash objectAtIndex:countPosssition]) {
[tempArrayForValue addObject:[hashConvertUnderlineToBackslash objectAtIndex:countPosssition]];
[tempArrayForKey addObject:[NSNumber numberWithInt:countPosssition]];
possitionInTempArray++;
}
}
//4 replace
for (int countPosssition = 0 ; countPosssition < [hash length]; countPosssition++) {
if ([originalHashArrayOfCharFromString objectAtIndex:countPosssition] != [hashConvertPlusToMinus objectAtIndex:countPosssition]) {
[tempArrayForValue addObject:[hashConvertPlusToMinus objectAtIndex:countPosssition]];
[tempArrayForKey addObject:[NSNumber numberWithInt:countPosssition]];
possitionInTempArray++;
}
}
NSLog(tempArrayForKey.debugDescription);
NSLog(tempArrayForValue.debugDescription);
// use the them array to submit changes
for (int count = 0; count < tempArrayForKey.count; count++) {
[originalHashArrayOfCharFromString setObject:[tempArrayForValue objectAtIndex:count] atIndexedSubscript:(int)[tempArrayForKey objectAtIndex:count]];
}
[hash setString:#""];
//Reassemble the hash string using his original array that sufferet modificvations
for (int count = 0; count<originalHashArrayOfCharFromString.count; count++) {
[hash appendString:[NSString stringWithFormat:#"%#", [originalHashArrayOfCharFromString objectAtIndex:count]]];
}
NSLog(hash);
What if you temporarily swapped out the occurrences to a "temp" character so you wouldn't lose data on the swaps and do something like this:
NSString * initialString = #"lRocUK/Qy+V2P3yDhCd74RvHjCDzlTfrGMolZZE0pcQ";
initialString =[initialString stringByReplacingOccurrencesOfString:#"/" withString:#"^"];
initialString =[initialString stringByReplacingOccurrencesOfString:#"-" withString:#"*"];
initialString =[initialString stringByReplacingOccurrencesOfString:#"+" withString:#"-"];
initialString =[initialString stringByReplacingOccurrencesOfString:#"*" withString:#"+"];
initialString =[initialString stringByReplacingOccurrencesOfString:#"_" withString:#"/"];
initialString =[initialString stringByReplacingOccurrencesOfString:#"^" withString:#"_"];

Find the highest repeated character in string and the count of the repeated character

I need to get the highest repeated character in string and the count of the repeated character.
For that i stored the each character of the string in the array and using the for loops i got each character and the count. is there any other delegate methods to find it to reduce the code?
for example
NSRange theRange = {0, 1}; //{location, length}
NSMutableArray * array = [NSMutableArray array];
for ( NSInteger i = 0; i < [myFormattedString length]; i++) {
theRange.location = i;
[array addObject:[myFormattedString substringWithRange:theRange]];
}
int countForChar = 0;
for (int i=0; i<[array count]; i++) {
NSString *firstCharacter = [array objectAtIndex:i];
for (int j=1; j< [array count]; j++) {
if ([firstCharacter isEqualToString:[array objectAtIndex:j]]) {
countForChar = countForChar + 1;
}
}
NSLog(#"The Charcter is %# The count is %d", firstCharacter, countForChar);
countForChar = 0;
}
Thanks in advance...
Because the string may have more than a char have same most repeat count, so here is my solution:
- (NSArray *)mostCharInString:(NSString *)string count:(int *)count{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
int len = string.length;
NSRange theRange = {0, 1};
for (NSInteger i = 0; i < len; i++) {
theRange.location = i;
NSString *charStr = [string substringWithRange:theRange];
int preCount = 0;
if ([dict objectForKey:charStr]) {
preCount = [[dict objectForKey:charStr] unsignedIntegerValue];
}
[dict setObject:#(preCount+1) forKey:charStr];
}
NSArray *sortValues = [[dict allValues] sortedArrayUsingSelector:#selector(compare:)];
*count = [[sortValues lastObject] unsignedIntegerValue];
return [dict allKeysForObject:#(*count)];
}
How to use and test:
int mostRepeatCount = 0;
NSArray *mostChars = nil;
mostChars = [self mostCharInString:#"aaabbbcccc" count:&mostRepeatCount];
NSLog(#"count:%d char:%#", mostRepeatCount, mostChars);
the result is:
count:4 char:(
c
)
try:
mostChars = [self mostCharInString:#"aaabbbccccdddd" count:&mostRepeatCount];
the result is:
count:4 char:(
d,
c
)
Hope to help you.
Here is my code might be not good enough but I think its the fastest
NSString *myFormattedString = #"oksdflajdsfd";
NSMutableDictionary *lettersCount = [[NSMutableDictionary alloc] init];
for (NSInteger i = 0; i < [myFormattedString length]; i++) {
unichar charAtIndex = [myFormattedString characterAtIndex:i];
NSNumber *countForThisChar = [lettersCount objectForKey:[NSString stringWithFormat:#"%c",charAtIndex]];
int count = 1;
if(countForThisChar) {
count = [countForThisChar integerValue] + 1;
[lettersCount setObject:#(count) forKey:[NSString stringWithFormat:#"%c",charAtIndex]];
} else {
// not added yet, add it with 1 count
[lettersCount setObject:#(count) forKey:[NSString stringWithFormat:#"%c",charAtIndex]];
}
}
// for now the work is O(n)
// ignoring the work of this cycle or consider it as O(1)
NSString *mostFrequentChar = nil;
NSInteger maxCount = 0;
for(NSString *oneChar in lettersCount.keyEnumerator) {
NSNumber *count = [lettersCount objectForKey:oneChar];
if([count integerValue] > maxCount) {
mostFrequentChar = oneChar;
maxCount = [count integerValue];
}
}
NSLog(#"the char %# met for %d times", mostFrequentChar, maxCount);
Remember the search for an object in NsDictionary is O(1) for the average case scenario.
Here is an example that would work correctly with any string and has linear time complexity. This uses the NSCountedSet which can be pretty useful.
NSString* string = #"This is a very wonderful string. Ølsen & ジェイソン";
NSCountedSet* characterCounts = [[NSCountedSet alloc] init];
// This ensures that we deal with all unicode code points correctly
[string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[characterCounts addObject:substring];
}];
NSString* highestCountCharacterSequence = nil;
NSUInteger highestCharacterCount = 0;
for (NSString* characterSequence in characterCounts) {
NSUInteger currentCount = [characterCounts countForObject:characterSequence];
if (currentCount > highestCharacterCount) {
highestCountCharacterSequence = characterSequence;
highestCharacterCount = currentCount;
}
}
NSLog(#"Highest Character Count is %# with count of %lu", highestCountCharacterSequence, (unsigned long)highestCharacterCount);
Sadly, my silly example string ends up having space characters as the most repeated :)
Every character can be presented by its int value. Make an instance of NSArray with n size (n number of unique characters string can have). Loop through string and add +1 on (int)character index in array at every cycle. When you finish the character with greatest value in array is the highest repeated character.

How to delete the number from non repeating number array?

I am trying this code for generating a random number and saving the list of numbers in an array, then i am trying to delete those numbers from the list one by one which appeared once, e.g
1, 5, 9 , 4, 3, 7 ,6 ,10, 11, 8, 2 are the list of integers now 9 is appeared once and now i do not need 9 again.. this is my code of random non repeating numbers array.
NSMutableArray *storeArray = [[NSMutableArray alloc] init];
BOOL record = NO;
int x;
for (int i=0; [storeArray count] < 10; i++) //Loop for generate different random values
{
x = arc4random() % 10;//generating random number
if(i==0)//for first time
{
[storeArray addObject:[NSNumber numberWithInt:x]];
}
else
{
for (int j=0; j<= [storeArray count]-1; j++)
{
if (x ==[[storeArray objectAtIndex:j] intValue])
record = YES;
}
if (record == YES)
{
record = NO;
}
else
{
[storeArray addObject:[NSNumber numberWithInt:x]];
}
}
}
Try this,
NSArray *arrRandoms = [[NSArray alloc]initWithObjects:1,5,8,7,36,17,96,32,5,7,8,13,36,nil] ; // This contains your random numbers
NSMutableArray *arrFresh = [[NSMutableArray alloc]init];
// Now removing the duplicate numbers
BOOL checkRepeat = NO;
int _Current;
for (int i=0; i<[arrRandoms count]; i++)
{
_Current = [arrRandoms objectAtIndex:i];
if (i == 0)
[arrFresh addObjects:_Current];
else
{
checkRepeat = NO;
for(int j=0; j< [arrFresh count]; j++)
{
if ( _Current == [arrFresh objectAtIndex:j])
checkRepeat = YES;
}
if (checkRepeat == NO)
[arrFresh addObjects:_Current];
}
}
I think this code will work. Check It.
try it
//**************remove repeat objects from array***************************//
NSArray *noDuplicates = [[NSSet setWithArray: yourArray] allObjects];
you add
.h file
BOOL isSame;
NSMutableArray *countArray;
NSInteger randomNumber;
.m file
countArray=[[NSMutableArray alloc]init];
//get randon no
-(NSInteger)getRandomNo:(NSInteger)range
{
isSame=TRUE;
while (isSame){
isSame = FALSE;
randomNumber = arc4random() % range;
for (NSNumber *number in countArray){
if([number intValue] ==randomNumber){
isSame = TRUE;
break;
}
}
}
[countArray addObject:[NSNumber numberWithInt:randomNumber]];
return randomNumber;
}
Tested Please try this,
NSMutableArray *storeArray = [[NSMutableArray alloc] init];
NSMutableSet * setUnique = [[NSMutableSet alloc] init];
for (int i=0; [setUnique count] < 10; i++) //Loop for generate different random values
{
[setUnique addObject:[NSNumber numberWithInt:arc4random() % 10]];
}
storeArray = [[setUnique allObjects] mutableCopy];
// check this how to get random number in array (i.e.. this array(below arr_numbers) contain non repeated number)
// for general purpose i am posting this.. so people cannot check for another answer
int num_count = 10;
int RandomNumber;
NSMutableArray *arr_numbers = [[NSMutableArray alloc] init];
for (int j =0; j < num_count; j++)
{
RandomNumber = 0 + arc4random() % num_count;
NSLog(#"%d",RandomNumber);
if ([arr_numbers count]>0)
{
if (![arr_numbers containsObject:[NSNumber numberWithInt:RandomNumber]])//
{
[arr_numbers addObject:[NSNumber numberWithInt:RandomNumber]];
}
if (j == num_count-1)
{
if ([arr_numbers count] != num_count)
{
j = 0;
}
}
}
else
{
[arr_numbers addObject:[NSNumber numberWithInt:RandomNumber]];
}
}
NSLog(#"%#",arr_numbers);

split array of objects into groups of 4 - ios

How do you split an array of objects into an array of array of objects?
say I want to split into groups of 4, how do I do that?
[a,b,c,d,e,f,g,h] =>
[a,b] [c,d] [e,f] [g,h]
or maybe if I specify that I want to split into groups of 3, then the result should be
[a,b,c], [d,e,f], [g,h]
it should also work if h doesn't exist.
Try this logic.....
NSArray *arr = [[NSArray alloc]initWithObjects:#"One",#"Two",#"Three",#"Four",#"Five",#"Six",#"Seven",nil];
NSMutableArray *arrNew = [[NSMutableArray alloc]init];
int numberofSubArrs = 3; // change this to check the logic
for (int i=0; i<numberofSubArrs; i++) {
NSMutableArray *arrrr = [[NSMutableArray alloc]init];
[arrNew addObject:arrrr];
}
int m = 0;
for (int k=0; k<[arr count]; k++) {
[[arrNew objectAtIndex:m]addObject:[arr objectAtIndex:k]];
m++;
if (m == numberofSubArrs) {
m=0;
}
}
int g=0;
int p=0;
while(p<[arr count]) {
for (int z=0; z<[[arrNew objectAtIndex:g] count]; z++) {
[[arrNew objectAtIndex:g] replaceObjectAtIndex:z withObject:[arr objectAtIndex:p++]];
}
g++;
}
NSLog(#"Required Array is:%#",[arrNew description]);
Try this as a starting point:
NSArray *array = [NSArray arrayWithObjects:#"a", #"b", #"c", #"d", #"e", #"f", #"g", #"h", nil];
NSMutableArray *manyArrays = [NSMutableArray array];
int numberOfElementsInSubArrays = 2;
int numberOfSubArrays = ceil((float)[array count] / (float)numberOfElementsInSubArrays);
for (int i = 0; i < numberOfSubArrays; i++) {
NSMutableArray *subArray = [NSMutableArray array];
for (int j = 0; j < numberOfElementsInSubArrays; j++) {
if (i*numberOfElementsInSubArrays+j < [array count]) {
NSLog(#"Array: %d Value:%#", i, [array objectAtIndex:i*numberOfElementsInSubArrays+j]);
[subArray addObject:[array objectAtIndex:i*numberOfElementsInSubArrays+j]];
}
}
[manyArrays addObject:subArray];
}
Give this a try.
NSMutableArray *splitted = [NSMutableArray array];
id firstItem, secondItem;
for (NSInteger i=0; i <= [originalArray count]-1; i+=2) {
#try {
firstItem = [originalArray objectAtIndex:i];
secondItem = [originalArray objectAtIndex:i+1];
}
#catch (NSException *exception) {
secondItem = [NSNull null];
}
#finally {
[splitted addObject:#[firstItem,secondItem]];
}
}

Resources