How to fetch each value from Array of strings inside an Array - ios

I have an array inside an array named as officeList in the following format :-
(
(
Prospect,
Propose
),
(
Open,
Fund,
Propose
),
(
Settle,
Review
)
)
Problem is I got this array by converting the JSON file into an array and as a result of that I got this array of string inside an array. What I want is to fetch each value one by one like : "prospect", "propose", etc.
I am using following code to fetch data :
for (int i = 0;i<[_officelist count];i++)
{
id val=[officeSize objectAtIndex:i];
CGFloat val1=[val floatValue];
UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, 20,val1,15)];
newLabel.text=[NSString stringWithFormat:#"%#",_officelist[i]];
newLabel.textAlignment = NSTextAlignmentCenter;
[self.roadmapCollectionView addSubview:newLabel];
x=x+val1;
}

Have you try this
NSMutableArray *arr = [[NSMutableArray alloc] init];
for (NSArray *objArr in jsonArr) {
for (NSString *str in ObjArr) {
[arr addObject:str];
}
}
Here jsonArr is the array that you getting from response. Now use arr to access every object that you want.
Hope this will help you

Apply below code,
for (int i = 0; i < [_officelist count]; i++)
{
NSArray *valList=[_officelist objectAtIndex:i];
CGFloat val1 = [valList floatValue]; // Your Float val
for (int j = 0; j < [valList count]; j++)
{
UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, 20,val1,15)];
newLabel.text = [NSString stringWithFormat:#"%#",valList[j]];
newLabel.textAlignment = NSTextAlignmentCenter;
[self.roadmapCollectionView addSubview:newLabel];
}
x=x+val1;
}
Hope this helps you.

Related

NSDictionary Getting Repeated values

I know this may be a repeated question but I googled a lot but not able to find a suitable answer for me.
I have a NSMutableArray which has two NSDictionary with Keys and values which I need to populated on a UITableView. I have retrieved the value of the dictionary which I'm going populate using
NSMutableArray *mutArray = [responseArray valueForKey:#"Table"];
And I did like
NSMutableSet *names = [NSMutableSet set];
NSMutableArray *mutArray1 = [[NSMutableArray alloc] init];
for (id obj in mutArray) {
NSString *destinationName = [obj valueForKey:#"AssetClassName"];
if (![names containsObject:destinationName]) {
[mutArray1 addObject:destinationName];
[names addObject:destinationName];
}
}
Because the value AssetClassName is repeated. Now I have three values in mutArray1 which I need to show as UITableView section. Under AssetClassName I have Some data which determines the row in that section.
For retrieving that data I'm doing like
for (int i = 0; i < [mutArray1 count]; i++) {
NSMutableDictionary *a = [[NSMutableDictionary alloc] init];
NSMutableDictionary *b = [[NSMutableDictionary alloc] init];
for (NSDictionary *dict in mutArray) {
if ([[mutArray1 objectAtIndex:i] isEqualToString:[dict valueForKey:#"AssetClassName"]]) {
[a setObject:[dict objectForKey: #"SubAssetClassName"] forKey:#"Investment Categories"];
[a setObject:[dict valueForKey:#"Amount"] forKey:#"Amount (EUR)"];
[a setObject:[dict valueForKey:#"AllocationPercentage"] forKey:#"%"];
[a setObject:[dict valueForKey:#"ModelAllocationPercentage"] forKey:#"ModelAllocationPercentage"];
[b setObject:a forKey:[dict valueForKey:#"SubAssetClassName"]];
[mutdict setObject:b forKey:[dict valueForKey:#"AssetClassName"]];
}
}
}
mutdict is a NSMutableDictionary declared globally and is instantiate in viewdidLoad
mutdict = [[NSMutableDictionary alloc] init];
The values are inserted into mutdict as I needed. Each SubAssetClassName is added into AssetclassName accordingly.
But my problem is in my final dictionary i.e mutdict the values for SubAssetClassName is repeated.
Can anybody tell how to solve this.
My console
"AssetClassName" = {
"SubAssetClass" = {
"%" = 0;
"Amount (EUR)" = 0;
"Investment Categories" = "HIGH YIELD BONDS";
"ModelAllocationPercentage" = 22;
};
"SubAssetClass" = {
"%" = 0;
"Amount (EUR)" = 0;
"Investment Categories" = "HIGH YIELD BONDS";
"ModelAllocationPercentage" = 22;
};
"SubAssetClass" = {
"%" = 0;
"Amount (EUR)" = 0;
"Investment Categories" = "HIGH YIELD BONDS";
"ModelAllocationPercentage" = 22;
};
};
"AssetClassName" = {
"SubAssetClass" = {
"%" = 0;
"Amount (EUR)" = 0;
"Investment Categories" = "EMERGING MARKETS EQUITIES";
"ModelAllocationPercentage" = 10;
};
};
"AssetClassName" = {
"SubAssetClass" = {
"%" = 0;
"Amount (EUR)" = 0;
"Investment Categories" = "STRUCTURED PRODUCTS";
"ModelAllocationPercentage" = 10;
};
"SubAssetClass" = {
"%" = 0;
"Amount (EUR)" = 0;
"Investment Categories" = "STRUCTURED PRODUCTS";
"ModelAllocationPercentage" = 10;
};
"SubAssetClass" = {
"%" = 0;
"Amount (EUR)" = 0;
"Investment Categories" = "STRUCTURED PRODUCTS";
"ModelAllocationPercentage" = 10;
};
};
}
Here I can see that all SubAssetClass values are same for each section but actually its not.
How can I solve this.
You need to create a new instance of your mutable dictionary inside the loop. Right now you create one instance and update it over and over. This results in one dictionary being added over and over.
Change you code as follows:
for (NSInteger i = 0; i < [mutArray1 count]; i++) {
NSMutableDictionary *b = [[NSMutableDictionary alloc] init];
for (NSDictionary *dict in mutArray) {
if ([[mutArray1 objectAtIndex:i] isEqualToString:[dict valueForKey:#"AssetClassName"]]) {
NSMutableDictionary *a = [[NSMutableDictionary alloc] init];
[a setObject:[dict objectForKey: #"SubAssetClassName"] forKey:#"Investment Categories"];
[a setObject:[dict valueForKey:#"Amount"] forKey:#"Amount (EUR)"];
[a setObject:[dict valueForKey:#"AllocationPercentage"] forKey:#"%"];
[a setObject:[dict valueForKey:#"ModelAllocationPercentage"] forKey:#"ModelAllocationPercentage"];
[b setObject:a forKey:[dict valueForKey:#"SubAssetClassName"]];
[mutdict setObject:b forKey:[dict valueForKey:#"AssetClassName"]];
}
}
}
Also, in most cases you should not be using valueForKey:. Use objectForKey: unless you have a clear and specific need to use key-value coding instead of simply getting an object from the dictionary for a given key.

How to randomize a multidimensional NSMutableArray?

So, I was tasked with randomizing a multidimensional array in Objective-C.
I needed to make this:
[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
Look something like this:
[[4,8,6],[1,7,9],[2,11,10],[3,5,12]]
I did that by hand, so please excuse me for the poor randomization.
This is what I came up with, please feel free to critique or improve it. I borrowed certain parts from existing SO contributions.
-(NSMutableArray *)shuffleArray:(NSMutableArray *)array
{
NSMutableArray *flatArray = [array valueForKeyPath:#"#unionOfArrays.self"];
NSUInteger count = flatArray.count;
for (NSUInteger i = 0; i < count; i++) {
NSInteger remainingCount = count - i;
NSInteger exchangeIndex = i + arc4random_uniform((u_int32_t)remainingCount);
[flatArray exchangeObjectAtIndex:i withObjectAtIndex:exchangeIndex];
}
NSMutableArray *set = [[NSMutableArray alloc] init];
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
for (int i = 0; i < flatArray.count; i++) {
[tempArray addObject:flatArray[i]];
if ((i + 1) % 3 == 0) {
[set addObject:[tempArray copy]];
[tempArray removeAllObjects];
}
}
return set;
}

How to add new object in an array without affecting the other objects iOS

Hi I would like to know how to do when you click a button it will add a new object in the array and it will not affect the other objects like this:
here is my code:
x = x + 1;
dictData = [[NSMutableDictionary alloc] init];
for (int i = 0; i < x; i++) {
[dictData setObject:[menuData itemName] forKey:#"dishName"];
[dictData setObject:#"1" forKey:#"quantity"];
[dictData setObject:[menuData itemPrice] forKey:#"price"];
[arrMut addObject:dictData];
}
This is the current output
{
name = charles1;
},{
name = charles1;
}
This is the expected output
{
name = charles1;
},{
name = charles2;
}
because you are adding a same dictionary
change it to
for (int i = 0; i < x; i++) {
dictData = [[NSMutableDictionary alloc] init]; // move this line to inside of the loop
[dictData setObject:[menuData itemName] forKey:#"dishName"];
[dictData setObject:#"1" forKey:#"quantity"];
[dictData setObject:[menuData itemPrice] forKey:#"price"];
[arrMut addObject:dictData];
}
or
for (int i = 0; i < x; i++) {
[arrMut addObject:[#{#"dishName" : [menuData itemName],
#"quantity" : #"1",
#"price" : [menuData itemPrice],
} mutableCopy]];
}
The [menuData itemName] method seems to be returning the same object everytime.
You could insert menuData objects in an array and then access a menuData object on each iteration of the for loop.
NSMutableArray *arrMenuData = //This array should contain the menuData objects
for (int i = 0; i < x; i++) {
dictData = [[NSMutableDictionary alloc] init]; // move this line to inside of the loop
[dictData setObject:[[arrMenuData objectAtIndex:i] itemName] forKey:#"dishName"]; //A typecast to menuData type may be required, please test.
[dictData setObject:#"1" forKey:#"quantity"];
[dictData setObject:[[arrMenuData objectAtIndex:i] itemPrice] forKey:#"price"];
[arrMut addObject:dictData];
}

How to create a Multidimensional Array using loop in IOS xcode

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

Create an array of CGPoints by pairing values from two different NSArrays

How to create an array of CGPoints by pairing values from two different NSArrays in objective-c?
Lets say I have an Array "A" with the values: 0, 1, 2, 3, 4
And I also have an Array "B" with the values: 21, 30, 33, 35, 31
I would like to create Array "AB" with CGPoint values: (0,21), (1,30), (2,33), (3,35), (4,31)
Thanks for your help.
Note that Objective-C collection classes can only hold objects, so I have assumed your input numbers are held in NSNumber objects. This also means that the CGPoint structs must be held in a NSValue object in the combined array:
NSArray *array1 = ...;
NSArray *array2 = ...;
NSMutableArray *pointArray = [[NSMutableArray alloc] init];
if ([array1 count] == [array2 count])
{
NSUInteger count = [array1 count], i;
for (i = 0; i < count; i++)
{
NSNumber *num1 = [array1 objectAtIndex:i];
NSNumber *num2 = [array2 objectAtIndex:i];
CGPoint point = CGPointMake([num1 floatValue], [num2 floatValue]);
[pointArray addObject:[NSValue valueWithCGPoint:point]];
}
}
else
{
NSLog(#"Array count mis-matched");
}
Someone else posted on making an NSArray of CGPoints, but you asked for an array of CGPoints. This ought to do that:
NSArray* a = #[ #(0.), #(1.), #(2.), #(3.), #(4.) ];
NSArray* b = #[ #(21.), #(30.), #(33.), #(35.), #(31.) ];
const NSUInteger aCount = a.count, bCount = b.count, count = MAX(aCount, bCount);
CGPoint* points = (CGPoint*)calloc(count, sizeof(CGPoint));
for (NSUInteger i = 0; i < count; ++i)
{
points[i] = CGPointMake(i < aCount ? [a[i] doubleValue] : 0 , i < bCount ? [b[i] doubleValue] : 0.0);
}

Resources