I need to sort array (in IOS) like this...
let us suppose that i have an NSMutableArray with : 1,2,3,4,5
i need to build a new array with the last object in the middle and the middle will replace the last : 1.2.5.4.3...
please help:
this is what i doing for now, i got only the 1.2.5 but i not success to put the 4.3 ...
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:#1,#2,#3,#4,#5, nil];
int arrCount = (int)arr.count/2;
NSMutableArray *arr2 = [[NSMutableArray alloc] init];
for (int i = 0;i < arr.count; i++) {
if (i < arrCount) {
[arr2 addObject:arr[i]];
}
else{
index = i;
[arr2 addObject:arr.lastObject];
}
}
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:#1,#2,#3,#4,#5, nil];
NSMutableArray *arr2 = [[NSMutableArray alloc] initWithArray:arr];
int middleIndex = (int)arr.count/2;
id middleObject = arr2[middleIndex];
[arr2 replaceObjectAtIndex:middleIndex withObject:arr2.lastObject];
[arr2 replaceObjectAtIndex:arr2.count-1 withObject:middleObject];
NSLog(#"%#", arr2);
Related
for (int i=0; i<array.count; i++)
{
NSString *name = [NSString stringWithFormat:#"array%i",i];
NSMutableArray *stringName = [[NSMutableArray alloc] init];
}
I don't know its right or wrong i just want to create count number of times NSMutableArray Should create with different names like
the name string represent
name = array0
name = array1
name = array2.....name=array10
I want to Create
NSMutableArray *array0 = [[NSMutableArray alloc]init];
NSMutableArray *array1 = [[NSMutableArray alloc]init];
NSMutableArray *array2 = [[NSMutableArray alloc]init];
.
.
.
NSMutableArray *array10 = [[NSMutableArray alloc]init];
Like this
NSMutableArray *myArray = [NSMutableArray new];
for(int i=0;i<numOfSubArrays;i++){
NSMutableArray *tempArray = [NSMutableArray new];
[myArray addObject: tempArray];
}
[myArray[0] addObject:object00];
[myArray[0] addObject:object01];
.
.
[myArray[1] addObject:object10];
[myArray[1] addObject:object11];
//etc
//myArray[1][1] is object11
Below is my Code, When I Add Dictionary into NSMutableArray that time it works well but when I Put NSLog It will throw an Exception. So, Please Help me
//-->
arrMain = [[NSMutableArray alloc] init];
NSMutableArray *arrMainMenu = [[NSMutableArray alloc] init];
NSMutableArray *arrMainMenu2 = [[NSMutableArray alloc] init];
for(int i = 0; i < arrParent.count; i++)
{
NSMutableDictionary *dictMain = [arrParent objectAtIndex:i];
if([[dictMain valueForKey:E_UNDER] isEqualToString:#""])
{
NSString *titleMain = [dictMain valueForKey:E_PARAMETER_VALUE_NAME];
NSMutableArray *arrSubMenu = [[NSMutableArray alloc] init];
for (int j = 0; j <arrSub.count; j++)
{
NSMutableDictionary *dictSub = [[arrSub objectAtIndex:j] mutableCopy];
if([titleMain isEqualToString:[dictSub valueForKey:E_UNDER]])
[arrSubMenu addObject:dictSub];
}
if([arrSubMenu count])
{
[dictMain setValue:#"1" forKey:IS_EXAPANDABLE];
[dictMain setValue:#"0" forKey:IS_EXPANDED];
[arrSubMenu insertObject:dictMain atIndex:0]; //Here Add Key-Value Dictionary in Array
[dictMain setValue:arrSubMenu forKey:LIST_ID];
arrSubMenu = [[NSMutableArray alloc] init];
}
else
[dictMain setValue:#"0" forKey:IS_EXAPANDABLE];
[arrMainMenu addObject:dictMain];
}
}
NSLog(#"%#", arrMainMenu); //Here Throw Bad Access exception
[obj_DataModel setFilterMenu:arrMainMenu];
//-->
I am having a response data as below .I have an array populated by dictionaries, and I need to sort last two values from each index of multi dimensional array.
This is my array:
arrayListstatus
(
{
"Applicant_FirstName" = "";
"Applicant_LastName" = "";
"Application_ID" = test;
"Application_Status" = "";
},
{
"Applicant_FirstName" = John;
"Applicant_LastName" = Doe;
"Application_ID" = 0002;
"Application_Status" = Recertify;
}
)
Here is my code:
- (NSArray *)subItems
{
NSMutableArray *items = [[NSMutableArray alloc] init];
for (int ar = 0; ar < arrayListstatus.count; ar++)
{
NSArray *thirdarray=[arrayListstatus objectAtIndex:ar] ;
NSLog(#"thirdarray %#",third array);
[items addObject:thirdarray];
}
NSLog(#"itemss %#",items);
return items;
}
i think this should do the trick:
- (NSArray *)subItems
{
NSMutableArray *items = [[NSMutableArray alloc] init];
for (int ar = 0; ar < arrayListstatus.count; ar++)
{
NSArray *thirdarray= [arrayListstatus objectAtIndex:ar] ;
NSMutableDictionary *tmpDict = [NSMutableDictionary alloc] init];
[tmpDict setObject:[thirdarray objectAtIndex:2] forKey:#"Application_ID"];
[tmpDict setObject:[thirdarray objectAtIndex:3] forKey:#"Application_Status"];
[items addObject:tmpDict];
[items addObject:tmpDict];
}
NSSortDescriptor *sortDescriptor;
sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:"Application_Status" ascending:YES], nil]];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray = [items sortedArrayUsingDescriptors:sortDescriptors];
NSLog(#"itemss %#",items);
return items;
}
NSArray*arr = #[#"ram",#"Ram",#"vinoth",#"kiran",#"kiran"];
NSSet* uniqueName = [[NSSet alloc]initWithArray:arr];
NSLog(#"Unique Names :%#",uniqueName);
Output:
but i need the output as
You could first convert them all to lowercase strings.
NSArray *arr = #[#"ram",#"Ram",#"vinoth",#"kiran",#"kiran"];
NSArray *lowerCaseArr = [arr valueForKey:#"lowercaseString"];
NSSet* uniqueName = [[NSSet alloc] initWithArray:lowerCaseArr];
NSLog(#"Unique Names :%#",uniqueName);
Unique Names :{(
ram,
kiran,
vinoth
)}
Try this:
NSArray *arr = [NSArray arrayWithObjects:#"Ram",#"ram", nil]; //this is your array
NSMutableArray *arr1 = [[NSMutableArray alloc]init]; //make a nsmutableArray
for (int i = 0; i<[arr count]; i++) {
[arr1 addObject:[[arr objectAtIndex:i]lowercaseString]];
}
NSSet *set = [NSSet setWithArray:(NSArray*)arr1];//this set has unique values
This will always preserve casing form that was existing in your original container (although it's undefined which casing):
NSArray<NSString*>* input = ...
NSMutableDictionary* tmp = [[NSMutableDictionary alloc] init];
for (NSString* s in input) {
[tmp setObject:s forKey:[s lowercaseString]];
}
return [tmp allValues];
Create a mutable array the same size as arr. Fill it with lowercaseString versions of each element of arr. Make the set out of that.
#Updated
Using this you remove uppercase string from your array.
NSMutableArray *arr= [[NSMutableArray alloc]initWithObjects:#"ram",#"Ram",#"vinoth",#"kiran", nil];
NSMutableArray *arrCopy = [[NSMutableArray alloc]init];
for (int index = 0 ; index<arr.count; index++) {
NSUInteger count = [[[[arr objectAtIndex:index] componentsSeparatedByCharactersInSet:[[NSCharacterSet uppercaseLetterCharacterSet] invertedSet]] componentsJoinedByString:#""] length];
if (count == 0) {
[arrCopy addObject:[arr objectAtIndex:index]];
}
}
NSLog(#"Print Mutable Copy %#",arrCopy);
try this one
NSArray *copyArray = [mainArray copy];
NSInteger index = [copyArray count] - 1;
for (id object in [copyArray reverseObjectEnumerator]) {
if ([mainArray indexOfObject:object inRange:NSMakeRange(0, index)] != NSNotFound) {
[mainArray removeObjectAtIndex:index];
}
index--;
}
copyArray=nil;
I would like to create an array for iOS platform like the PHP syntax below, many thanks ~~
$createArray = array ();
for ($i = 0; $i<10; $i++) {
$createArray[$i]['name'] = $name;
$createArray[$i]['age'] = $age;
}
Save your values in NSDictionary and add that dictionary into your array
NSMutableArray *theArray = [NSMutableArray array];
for (int indexValue = 0; indexValue<10; indexValue++) {
NSMutableDictionary *theDictionary = [[NSMutableDictionary alloc] init];
[theDictionary setObject:name forKey:#"name"];
[theDictionary setObject:age forKey:#"age"];
[theArray addObject:theDictionary]
}
While Retrieving time,
NSString *name = [[theArray objectAtIndex:indexValue] objectForKey:#"name"];
NSString *age = [[theArray objectAtIndex:indexValue] objectForKey:#"age"];
you might find it helpful:
array = [[NSMutableArray alloc] init];
for (int i = 0; i < 8; i++) {
NSMutableArray *subArray = [[NSMutableArray alloc] init];
for (int j = 0; j < 8; j++) {
[subArray addObject:[NSNumber numberWithInt:0]];
}
[array addObject:subArray];
[subArray release];
}
also check this question
Try this.
array = [[NSMutableArray alloc] init];
for (int i = 0; i < 10; i++) {
NSMutableArray *subArray = [[NSMutableArray alloc] init];
for (int j = 0; j < 2; j++) {
//Do your Stuff
// [subArray addObject:name];
// [subArray addObject:Age];
}
[array addObject:subArray];
}
OR
Why can't try with the NSDictionary
You can use this. But this is not better way in IOS.
NSMutableArray *array[20];
for (int i=0;i< 20; i++)
{
array[i] = [NSMutableArray array];
for (int j=0;j<3;j++)
{
NSMutableDictionary *theDictionary = [[NSMutableDictionary alloc] init];
[theDictionary setObject:name forKey:#"name"];
[theDictionary setObject:age forKey:#"age"];
[[array[i] addObject:theDictionary]
}
}
First you to have set An NSMutableDictionary on .h file
#interface MSRCommonLogic : NSObject
{
NSMutableDictionary *twoDimensionArray;
}
then have to use following functions in .m file
- (void)setValuesToArray :(int)rows cols:(int) col value:(id)value
{
if(!twoDimensionArray)
{
twoDimensionArray =[[NSMutableDictionary alloc]init];
}
NSString *strKey=[NSString stringWithFormat:#"%dVs%d",rows,col];
[twoDimensionArray setObject:value forKey:strKey];
}
- (id)getValueFromArray :(int)rows cols:(int) col
{
NSString *strKey=[NSString stringWithFormat:#"%dVs%d",rows,col];
return [twoDimensionArray valueForKey:strKey];
}