How to merge 2 NSDictionaries in NSMutableArray having same value in iOS? - ios

I have an NSMutableArray with NSDictionaries like below format
(
{
"login_id" = 95;
consDays = "Mon,Tue";
consEndTime = 12600000;
consStartTime = 9000000;
duration = 10;
id = 58;
isActive = 1;
},
{
"login_id" = 95;
consDays = "Wed,Thur";
consEndTime = 41400000;
consStartTime = 23400000;
duration = 10;
id = 59;
isActive = 1;
},
{
"login_id" = 98;
consDays = "Mon,Tue,Wed,Thur,Fri,Sat,Sun";
consEndTime = 45000000;
consStartTime = 9000000;
duration = 30;
id = 60;
isActive = 1;
},
I can see here first 2 object's with "login_id" 95 are same and i need to merge the contents and make something like this one
{
"login_id" = 95;
consDays = "Mon,Tue-Wed,Thur";
consEndTime = 12600000-41400000;
consStartTime = 9000000-23400000;
duration = 10;
id = 58;
isActive = 1;
},
i have tried with for loop and its not getting clear
for(int i=0;i<[[arr valueForKey:#"data"] count];i++){
NSString *bloginId=[NSString stringWithFormat:#"%#",[[[arr valueForKey:#"data"] valueForKey:#"branchLogin_id"] objectAtIndex:i]];
for(int j=0;j<[[arr valueForKey:#"data"] count];j++){
NSString *firstId=[NSString stringWithFormat:#"%#",[[[arr valueForKey:#"data"] valueForKey:#"branchLogin_id"] objectAtIndex:i]];
NSString *secondId=[NSString stringWithFormat:#"%#",[[[arr valueForKey:#"data"] valueForKey:#"branchLogin_id"] objectAtIndex:j]];
NSLog(#"%#-%#",firstId,secondId);
if([firstId isEqualToString:secondId]){
NSLog(#"%#",[consultationDetailsArray valueForKey:#"branchLogin_id"]);
if([[consultationDetailsArray valueForKey:#"branchLogin_id"] containsObject:secondId]){
NSString *str=[NSString stringWithFormat:#"%#,%#",[[consultationDetailsArray valueForKey:#"consDays"] objectAtIndex:j],[[[arr valueForKey:#"data"] valueForKey:#"consDays"] objectAtIndex:j]];
NSLog(#"consDays %#",str);
}else{
[consultationDetailsArray addObject:[[arr valueForKey:#"data"] objectAtIndex:j]];
}
}
}
}
can anyone help me ?

Ironically I am doing just this same thing right now. Looks like the easiest way is to use a NSMutableDictionary.
// Assume two starting dictionaries.
dictionaryOne;
dictionaryTwo;
//
// Create a mutable copy of the first
NSMutableDictionary *merged = dictionaryOne.mutableCopy;
[merged addEntriesFromDictionary: dictionaryTwo];
//
// merged now contains all keys of both dictionaries
// keys that are in both dictionaries will have values
// from dictionaryTwo.
Hope this helps.

Related

How can I add in multiple items in model by using array method?

I have an original code as below which is working properly:-
- (ZYSideSlipFilterRegionModel *)commonFilterRegionModelWithKeyword:(NSString *)keyword selectionType:(CommonTableViewCellSelectionType)selectionType {
ZYSideSlipFilterRegionModel *model = [[ZYSideSlipFilterRegionModel alloc] init];
model.containerCellClass = #"SideSlipCommonTableViewCell";
model.regionTitle = keyword;
model.customDict = #{REGION_SELECTION_TYPE:#(selectionType)};
model.itemList = #[[self createItemModelWithTitle:[NSString stringWithFormat:#"Local"] itemId:#"0" selected:NO],
[self createItemModelWithTitle:[NSString stringWithFormat:#"Oversea"] itemId:#"1" selected:NO]];
return model;
}
Now, I plan to change the static value (Oversea / local) to dynamic value. But only 1 item will be displayed.
for (int i = 0; i < filteredArray.count; i++) {
int intItemID = i + 1;
NSString *myNewString = [NSString stringWithFormat:#"%i", intItemID];
model.itemList = #[[self createItemModelWithTitle:[filteredArray[i] valueForKey:#"attribute_name"] itemId:myNewString selected:NO] ];
}
How can I put 2 item in model.itemList? Please help. Thank you.
You can use this way
//step:1 fetch Dictionary like this
for (int i = 0; i < filteredArray.count; i++)
{
NSMutableDictionary *dict = (NSMutableDictionary *)filteredArray[i] ;
int intItemID = i + 1;
NSString *myNewString = [NSString stringWithFormat:#"%i", intItemID];
model.itemList = #[[self createItemModelWithTitle:dict];
}
//Step 2 : You can define your method for Model Like this
- (CommonItemModel *)createItemModelWithTitle:(NSMutableDictionary *)dictModel
{
CommonItemModel *model = [[CommonItemModel alloc] init];
model.itemId = [dictModel valueForKey : #"itemId"];
model.itemName = [dictModel valueForKey:#"itemTitle"];
model.selected = [dictModel valueForKey:[NSNumber numberWithBool:
[[dictModel valueForKey:#"selected"]]]];
return model;
}
//One more thing you are write this in first step
model.itemList = #[[self createItemModelWithTitle:dict];
But the method only return the Model class (CommonItemModel) so if you need any help you shared here
Thanks :)

iOS: How to separate key values in multiple dictionary which is in one array?

i haveNSDictionary like this:
(
{
bp = "158/56";
dateTime = "05/12/2016 11:51:12 AM";
doc = {
"email_id" = "gkalal64#gmail.com";
exception = 0;
gender = Male;
id = 0;
"mobile_no" = 8149910113;
name = Kabadia;
"profile_id" = 0;
qualification = MBBS;
"reg_id" = 100;
salutation = Mr;
"wellness_id" = IN8S2D29A2;
};
"follow_up" = 14;
id = 6;
medicine = (
"Injection,Crocin,5,0-1-0,After Meal,2",
"Powder,churan,10,1-0-0,Before Meal,14",
no,
no,
no,
no,
no,
no,
no,
no
);
patient = {
"email_id" = "bishtrohit1989#gmail.com";
exception = 0;
gender = Male;
id = 0;
"mobile_no" = 8624088776;
name = kamlesh;
"profile_id" = 0;
qualification = "";
"reg_id" = 101;
salutation = Mr;
"wellness_id" = IND16HRR65;
};
weight = 47;
}
)
I want to display above information like following way:
But i have no idea about how to do that. i searched for this but i didn't get exactly how to do this.
What i did:
In previous View controller i have displayed multiple prescription title. after click on particular cell, i called this view and i got above response from server, which i want to display like above image.
I have taken UITableView on screen for this.
I have taken 1 static custom cell for this.
But i don't know how to do.
Please anyone can solve my issue. help will be appreciable.
As per your data structure i guess your data is kind of single object so it's clear that its not a collection(array) of same kind of object so i would suggest you to create only single custom cell for this entire single record.
In this case your TableView will have only single row.
In the Custom cell: lets call it 'PatientDetail'
Add all the labels and controls which you want to show for each
property and connect outlets whatever is required.
If you are using an Autolayout then give relative constraints to all the controls else set frame of each control dynamically by calculating the height of it's content.
How it will work ?
As you said its detail screen of particular list so we will show single row in tableview with all the details in it.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
In the follwing code
responseDict the NSDictionary which is the first object of response as your response is kind of array.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PatientDetail *cell=[tableView dequeueReusableCellWithIdentifier:#"PatientDetail" forIndexPath:indexPath];
//Setting
[cell.lbDateTime setText:[responseDict valueForKey:#"dateTime"]];
//Doctor name Just
[cell.lbDoctorName setText:[[responseDict objectForKey:#"doc"]valueForKey:#"name"]];
//Follow the above steps for all the details
return cell;
}
Updated Answer
if (indexPath.row < responseArray.count){
NSString *docName = #"";
NSString *contact = #"";
NSString *wellness_id = #"";
if ([selectedDict valueForKey:#"doc"] != nil){
NSDictionary *doctorDict = [selectedDict valueForKey:#"doc"];
NSString *lDocName = [doctorDict valueForKey:#"name"];
if(lDocName.length > 0){
docName = lDocName;
}
NSString *qulification = [doctorDict valueForKey:#"qualification"];
if(qulification.length > 0){
docName = [NSString stringWithFormat:#"%# %#", docName, qulification];
}
NSString *mobile_no = [doctorDict valueForKey:#"mobile_no"];
if(mobile_no.length > 0){
contact = mobile_no;
}
NSString *email_id = [doctorDict valueForKey:#"email_id"];
if(email_id.length > 0){
contact = [NSString stringWithFormat:#"%# / %#", contact, email_id];
}
NSString *wellness_idTemp = [doctorDict valueForKey:#"wellness_id"];
if(wellness_idTemp.length > 0){
wellness_id= [NSString stringWithFormat:#"Wellness_Id : %#", wellness_idTemp];
}
}
cell.docNameLabel.text = docName;
cell.contact.text = contact;
cell.wellnessID.text = wellness_id;
NSString *patientName = #"";
NSString *patientcontact = #"";
NSString *patientWellness_id = #"";
NSString *bloodPressure = [selectedDict valueForKey:#"bp"];
NSString *weight = [selectedDict valueForKey:#"weight"];
if ([selectedDict valueForKey:#"patient"] != nil){
NSDictionary *patientDict = [selectedDict valueForKey:#"patient"];
NSString *lName = [patientDict valueForKey:#"name"];
if(lName.length > 0){
patientName = lName;
}
NSString *mobile_no = [patientDict valueForKey:#"mobile_no"];
if(mobile_no.length > 0){
patientcontact = mobile_no;
}
NSString *email_id = [patientDict valueForKey:#"email_id"];
if(email_id.length > 0){
patientcontact = [NSString stringWithFormat:#"%# / %#", contact, email_id];
}
NSString *wellness_idTemp = [patientDict valueForKey:#"wellness_id"];
if(wellness_idTemp.length > 0){
patientWellness_id = [NSString stringWithFormat:#"Wellness Id : %#", wellness_idTemp];
}
}
cell.patientNameLabel.text = patientName;
cell.contact.text = patientcontact;
cell.wellnessID.text = wellness_id;
cell.bp.text = bloodPressure;
cell.weight.text = weight;
}
Hope this will help.

add simple line chart in ios application

am new for charts in ios developing apps..
am having two array values like
ProgramenameArray
BudgetCostArray
i want to draw a simple line chat using those arrays use as x-axis and Y-axis respectively...
i tried like this..
- (void)loadChartWithDates
{
NSMutableArray* chartData = [NSMutableArray arr ayWithCapacity:programNmaeIdArray.count];
NSLog(#"the array ^^^^^ values are %# %# ",programNmaeIdArray,programNameArray);
for(int i=0;i<programIdArray.count;i++)
{
chartData[i] = programIdArray[i];
}
NSMutableArray *months = [[NSArray alloc]init];
months=projectNameArray;
// Setting up the line chart
// _chartWithDates.verticalGridStep = 6;
//_chartWithDates.horizontalGridStep = 3;
_chartWithDates.fillColor = nil;
_chartWithDates.displayDataPoint = YES;
_chartWithDates.dataPointColor = [UIColor blackColor];
_chartWithDates.dataPointBackgroundColor = [UIColor fsDarkBlue];
_chartWithDates.dataPointRadius = 2;
_chartWithDates.color = [_chartWithDates.dataPointColor colorWithAlphaComponent:0.3];
_chartWithDates.valueLabelPosition = ValueLabelLeftMirrored;
_chartWithDates.labelForIndex = ^(NSUInteger item)
{
return months[item];
};
_chartWithDates.labelForValue = ^(CGFloat value)
{
return [NSString stringWithFormat:#"%.02f €", value];
};
[_chartWithDates setChartData:chartData];
}
so can anyone help...Thanks in advance.. :)

how to get array response from json in string?

I am getting the response from json like this
parcel = (
{
datetime = "2015-08-31 21:48:45";
height = 2;
id = 21;
invoice = NO;
length = 2;
mtype = IN;
"num_item" = 2;
"parcel_number" = 1;
pname = "Parcel number - 1";
"pro_number" = tanu;
status = 1;
type = Exact;
uid = 185;
weight = 2;
width = 2;
wtype = KG;
}
);
I want to get the height,date time,invoice in a string.. How can this be done? AnyOne can help me for this...
NSArray * arr_Parcel = [[NSArray alloc]initWithArray:data];
NSMutableDictionary * temp_Dict = [[NSMutableDictionary alloc]init];
for (int i = 0 ; i < arr_Parcel.count; i++)
{
temp_Dict = [arr_Parcel objectAtIndex:i];
NSString * strHeight = [temp_Dict objectForKey:"height"];
NSString * strdate = [temp_Dict objectForKey:"datetime"];
}
If you are using Swift, use SwiftJSON to work with JSON.
It's easy to use. For example:
var jsonData = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let json = JSON(data: jsonData)
print(json["height"].double) // 2
print(json["datatime"].string) // "2015-08-31 21:48:45"
...
Create a NSObject Class and pass the NSDictionary the make a model class.
In your ViewController Class:
" parcel" is your Array
ModelClass * modelClass = [ModelClass alloc] initWithNewsDictionary: parcel[0]];
Create needed variables in ModelClass.h Class.
-(id)initWithNewsDictionary:(NSDictionary *)dictionary
{
self = [super init];
if (self) {
self.height = dictionary[#"height"];
self.datetime = dictionary[#"datetime"];
self.invoice = dictionary[#"invoice"];
self.weight = dictionary[#"weight"];
}
return self;
}
Try this :-
NSString *height=[NSString stringWithFormat:#"%#",[[[YourJson valueForKey:#"parcel"] objectAtIndex:0] objectForKey:#"height"]];
NSString *dateTime=[NSString stringWithFormat:#"%#",[[[YourJson valueForKey:#"parcel"] objectAtIndex:0] objectForKey:#"datetime"]];
NSString *invoice=[NSString stringWithFormat:#"%#",[[[YourJson valueForKey:#"parcel"] objectAtIndex:0] objectForKey:#"invoice"]];
NSLog(#"height : %# , date=%# ,invoice=%#",height,dateTime,invoice);

NSDictionary: Find a specific object and remove it

I have NSMutableDictionary that looks like this:
category = (
{
description = (
{
id = 1;
name = Apple;
},
{
id = 5;
name = Pear;
},
{
id = 12;
name = Orange;
}
);
id = 2;
name = Fruits;
},
{
description = (
{
id = 4;
name = Milk;
},
{
id = 7;
name = Tea;
}
);
id = 5;
name = Drinks;
}
);
Now, when a user performs an action in my application, I get the #"name"-value of the object, like "Apple". I would like to remove that object from the dictionary, but how will can I reach this object with the
[myDictionary removeObjectForKey:]
method?
At a high level you need to get a reference to the "description" array. Then iterate through the array getting each dictionary. Check the dictionary to see if it has the matching "name" value. If so, remove that dictionary from the "description" array.
NSString *name = ... // the name to find and remove
NSMutableArray *description = ... // the description array to search
for (NSUInteger i = 0; i < description.count; i++) {
NSDictionary *data = description[i];
if ([data[#"name"] isEqualToString:name]) {
[description removeObjectAtIndex:i];
break;
}
}
To remove something from an INNER array:
NSString* someFood = <search argument>;
NSArray* categories = [myDictionary objectForKey:#"category"];
for (NSDictionary* category in categories) {
NSArray* descriptions = [category objectForKey:#"description"];
for (int i = descriptions.count-1; i >= 0; i--) {
NSDictionary* description = [descriptions objectForIndex:i];
if ([[description objectForKey:#"name"] isEqualToString:someFood]) {
[descriptions removeObjectAtIndex:i];
}
}
}
To remove an entire group (eg, "Fruits") from the outer array is simpler.

Resources