How to store values at key =0 into nsarray? - ios

I want to store all the emails into an array. What do I need to do for the same ?

Use this code and change it according to your requirement
NSMutableDictionery * repsonseDict = [[NSMutableDictionery alloc]init];
repsonseDict = [reponseObj objectForKey#"response"];
NSArray * allKeys = [repsonseDict allKeys];
NSMutableArray * emailArr = [[NSMutableArray alloc]init];
for(int i = 0; i < [allKeys count]; i ++ ){
NSString * numberKey = [NSString stringWithFormat:#"%d",i];
[emailArr addObject:[[repsonseDict objectForKey:numberKey]objectForKey:#"email"]];
}

Related

Set values of NSMutableDictionary to values in another

I did post request for retrieving data from the server. Now, I have NSDictionary with certain data. I need to display it in a chart, so I have to convert data to appropriate. Here's data which I get from the server:
dates = (
"01.01.2016",
"01.01.2016",
"01.01.2016"
...)
closes = {
0 = (
1,
1,
1,
...)
}
I need to transform it to this format:
{
"01.01.2016" = 1;
"01.01.2016" = 1;
"01.01.2016" = 1;
...
}
How can I do this?
NSArray *dates = ...
NSArray *closes = ...
NSMutableDictionary *result = [NSMutableDictionary new];
for (int i = 0; i < dates.count; i++) {
result[dates[i]] = closes[i];
}
NSLog(#"%#", result)
Note that dates and closes arrays must have same length
Assuming the number of 'dates' and closes elements are equal:
NSMutableDictionary dataOut = [NSMutableDictionary dictionary];
for (int index = 0; index < dataIn[#"dates"].count; index++)
{
dataOut[dataIn[#"dates"][index]] = dataIn[#"closes"][index];
}
Depending on how certain you are and on how fool proof you need this code to be, you may want to add checks (and error handling) to cover the above assumption.
If you have to 2 array and you need to create dictionary then
_productArray2 & _productArray1
NSDictionary * productDictionary = [NSMutableDictionary dictionary];
for (NSUInteger index =0; index < _productArray1.count; index ++) {
[productDictionary setValue:_productArray1[index] forKey:_productArray2[index]];
}
and if you already have a dictionary want to replace of set value to it
NSArray * allKeys = [productDictionary allKeys];
for (NSUInteger index =0; index < allKeys.count; index ++) {
[productDictionary setValue:_productArray[index] forKey:allKeys [index]];
}

Copy specific index value of array to another array?

I am trying to copy a specific values at specific index from one array to another like this:
for (int i = 0; i < 100; i++) {
if ([subID[i] isEqual: #"0"]) {
NSLog(#"state : %#",arrayTempState[i]);
NSString *str = arrayTempState[i];
[arrayState addObject:str];
NSLog(#"%#",arrayState[i]);
}
arrayState is NSMutableArray and arrayTempState is NSArray
but arrayState is null every time.
I tried arrayState[i] = arrayTempState[i]; but it did not work.
arrayState was not initialised that is why it couldn't store the value.
plz try this
arrayState = [NSMutableArray array];
for (int i = 0; i < 100; i++) {
if ([subID[i] isEqual: #"0"]) {
NSLog(#"state : %#",arrayTempState[i]);
NSString *str = arrayTempState[i];
[arrayState addObject:str];
NSLog(#"%#",arrayState[i]);
}

Can't store all data from NSArray into NSMutableDictionary? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
What's the problem with this code?? I am trying to put the data from an NSArray to a NSMutableDictionary but I do not want to first split the initial nsarray into two and then send the data to the nsdcitionary.
The problem is that when I NSLog de mutabledictionary it returns me just the 1 item which happens to be the last data from the NSArray.
NSString *str = #"13:00,2.00,13:05,2.03,13:10,2.07,13:15,2.01,13:20,2.08,13:25,2.10,13:30,2.15";
NSArray *arrayFinal = [str componentsSeparatedByString:#","];
NSMutableDictionary *dict = [NSMutableDictionary new];
for (int i = 0; i < [arrayFinal count ]; i = i + 2) {
[dict setObject:[arrayFinal objectAtIndex:i] forKey:#"hora"];
[dict setObject:[arrayFinal objectAtIndex:i+1] forKey:#"preco"];
}
The result is:
2013-09-04 20:27:33.732 separa[1438:c07] {
hora = "13:30";
preco = "2.15";
}
Any help will be appreciated.
for (int i = 0; i < [arrayFinal count ]; i = i + 2) {
[dict setObject:[arrayFinal objectAtIndex:i+1] forKey:[arrayFinal objectAtIndex:i]];
}
You need each key to point to an array of values. Something like this:
NSString *str = #"13:00,2.00,13:05,2.03,13:10,2.07,13:15,2.01,13:20,2.08,13:25,2.10,13:30,2.15";
NSArray *arrayFinal = [str componentsSeparatedByString:#","];
NSMutableArray *horas = [NSMutableArray new];
NSMutableArray *precos = [NSMutableArray new];
for (int i = 0; i < [arrayFinal count]; i += 2) {
[horas addObject:arrayFinal[i]];
[precos addObject:arrayFinal[i + 1]];
}
NSMutableDictionary *dict = [NSMutableDictionary new];
dict[#"hora"] = horas;
dict[#"preco"] = precos;
You're going to need to separate the two (hours and prices) into their own NSMutableArrays, then store each array as one of the keys, something like this:
NSMutableArray *hora = [NSMutableArray alloc] init];
NSMutableArray *preco = [NSMutableArray alloc] init];
NSString *str = #"13:00,2.00,13:05,2.03,13:10,2.07,13:15,2.01,13:20,2.08,13:25,2.10,13:30,2.15";
NSArray *arrayFinal = [str componentsSeparatedByString:#","];
NSMutableDictionary *dict = [NSMutableDictionary alloc] init];
for (int i = 0; i < [arrayFinal count ]; i = i + 2)
{
[hora addObject:[arrayFinal objectAtIndex:i];
[preco addObject:[arrayFinal objectAtIndex:i+1];
}
[dict setObject:hora forKey:#"hora"];
[dict setObject:preco forKey:#"preco"];
Probably not exactly the way I'd do it, but I think it is the concept you're looking for.

How to retrieve values from two Mutablearrays and store them in MutableDictionary with keys as an array?

I have 3 NSMutableArrays k,names,numbers
k array contains {a,b,c,d,e,.....}
names array contains {apple,bag,banana,car,cat,dall,elephant,.....}
numbers array contains {100,200,300,400,500,600,700,...}
All the 3 arrays are dynamic here.
I want to add names[],numbers[] to NSMutableDictionary with k[] as key array..
My output should be like this when i pint that dictionary
a
apple 100
b
bag 200
banana 300
c
car 400
cat 500
d
dall 600
e
elephant 700
Could somebody help me.
thank you.
NSMutableDictionary *result = [NSMutableDictionary new];
for(int i = 0; i<k.count; i++){
unichar letter = [k objectAtIndex:i]; //I'm assuming you have chars in your k array,
// if not, you have to format it here
NSString* name = [names objectAtIndex:i];
if([[name characterAtIndex:0] isEqual:letter]){
NSString *objectToInsert = [NSString stringWithFormat:#"%#: %#", #"name", [numbers objectAtIndex:i]];
[result setObject:objectToInsert forKey:letter];
}
}
You can probably optimize this more, but it should work ;)
Try with following code
NSMutableDictionary *mydic = [[NSMutableDictionary alloc] init];
For(int k = 0; k < firstArray.count ; k++)
{
NSPredicate *pred =[NSPredicate predicateWithFormat:#"SELF beginswith[c] %#", [firstArray objectAtIndex:k]];
NSArray *filteredArr = [MySecodArray filteredArrayUsingPredicate:pred];
NSString *myString = #"";
for (int t = 0 ; t < filteredArr.count ; t ++)
{
myString = [MyThirdArray objectAtIndex:[MySecodArray indexOfObject:[filteredArr objectAtIndex:t]]];
myString = [[filteredArr objectAtIndex:t] stringByAppendingString:myString];
[mydic setValue: myString forKey:[firstArray objectAtIndex:k]];
myString = #"";
}
}
NSLog(#"%#", myDic);
You can do like This,I am assuming nameArry and NumberArray count is same-->
try This code
NSSMutableDictionary *finalDictionary = [[NSSmutableDictionary alloc]init];
for(int i=0;i<k.count;i++)
{
NSString *alphaBet = [k objectAtIndex:i];
//initialize array here
NSMutableArray *insideDictArray = [[NSMutableArray alloc]init];
for(int j=0;j<nameArray.count;j++)
{
NSString *name = [nameArray objectAtIndex:j];
if([name hasPreffix:alphabet])
{
//get Number String
NSString *numberForName = [numberArray objectAtIndex:j];
//Now Concate Number and Name
NSString *concateString = [name stringByAppendingFormat:#"
%#",numberForName];
//Put this in Array we just intialized outside of secondLoop
[insideDictArray addObject:concateString];
}
}
//Now Add array to finalDictionary
[finalDictionary addObject:insideDictArray forKey:alphabet];
}
OutPut Will Be like this i hope :
finalDictionary =
{
a = [aple 100];
b = [bag 200,banana 300];
.
.
}

How to pass URL Dynamically from xml using Attribute in Objective c

How to pass URL Dynamically from xml using Attribute in Objective c and then i want to store url in matches string.
NSString * home= #"http://sports.xyz.rss/rss.aspx";
MSXMLParser * homenew=[[MSXMLParser alloc]init];
[homenew parseWithURLString:home FileName:#"file" xmlType:#"MATCHES"];
NSMutableDictionary * mainDict = [MSDataDictionary newsDictionary];
for (int i=0; i<=[mainDict count]; i++)
{
NSString * theKey = [NSString stringWithFormat:#"%d",i];
NSMutableDictionary * itemDict = [mainDict objectForKey:theKey];
matches = [itemDict objectForKey:#"title"];
if ([matches isEqualToString:#"cric"])
{
matches=[itemDict objectForKey:#"link"];
break;
}
}
NSString * home=matches;
What is MSDataDictionary? I think it should start with NS not MS.
I searched for MSDataDictionary but didn't get any thing.

Resources