Add Array in nsmutableaary - ios

i am developing instragramm type app when user tap like button i want add username in likes arry
So I have a dictionary in a dictionary with arrays. I am trying to add to the arrays that are in the likes array.
{
caption = "Shakira Shakira...";
comments = (
);
"display_name" = Shakira;
ext = "<null>";
"image_id" = 53;
likes = (
);
"post_type" = i;
"total_comments" = 0;
"total_likes" = 0;
"upload_dt" = 3wk;
"user_id" = 7;
"user_liked" = 0;
},
{
caption = "Addicted to Shakira???";
comments = (
{
comment = "love your profile :)";
"display_name" = Rihanna;
"user_id" = 5;
},
{
comment = "You are my favorite celebrity on Follow Me.";
"display_name" = Pitbull;
"user_id" = 4;
},
{
comment = "Looking Good!";
"display_name" = "Justin Bieber";
"user_id" = 2;
}
);
"display_name" = Shakira;
ext = "<null>";
"image_id" = 52;
likes = (
{
"display_name" = "Justin Bieber";
"user_id" = 2;
},
{
"display_name" = Rihanna;
"user_id" = 5;
},
{
"display_name" = Pitbull;
"user_id" = 4;
}
);
"post_type" = i;
"total_comments" = 3;
"total_likes" = 3;
"upload_dt" = 2mth;
"user_id" = 7;
"user_liked" = 0;
},
i want add display_name and user_id

NSMutableArray *mutearr = [NSMutableArray arrayWithArray:array];
here array is NSArray and mutarr is NSMutablearray

You can use addObjectsFromArray: from NSMutableArray class
NSArray *animal = [[NSArray alloc]initWithObjects:#"Lion",#"Tiger",#"Dog",#"Cat",#"Sheep",#"Wolf", nil];
NSArray *color = [[NSArray alloc]initWithObjects:#"Blue",#"Red",#"Yellow",#"Green",#"Black", nil];
NSMutableArray *all = [[NSMutableArray alloc] init];
[all addObjectsFromArray:animal];
[all addObjectsFromArray:color];

var displayNames = [String]()
var userIds = [Int]()
let responseArray = (yourArray as? [NSDictionary]) ?? []
for ar in responseArray{
let display_name = ar.value(forKey: "display_name") as? String
displayNames.append(display_name ?? "")
let user_id = ar.value(forKey: "user_id") as? Int
userIds.append(user_id ?? 0)
}
print(displayNames,userIds)

Related

How to sort an array of dictionaries according to time in swift

I am trying sort an array of dictionaries according to a key which has date. So, the sorting actually has to be done on the basis of dates. But I don't know why sortedArrayUsingComparator is not sorting the array and I am getting the older array back itself. The values are all coming correctly in the objects (obj1, obj2) though.
Here is the filter array:
(
{
age = 26;
"already_reported" = no;
"already_sync" = no;
"complete_name" = "Ghazalahhhhhhhhhhhhh Khan";
distance = "7.92353";
"first_name" = Ghazalahhhhhhhhhhhhh;
id = 288;
"last_chat_details" = (
{
"from_xmpp_id" = ghazalah41e21475158541;
id = 9614;
message = H;
"message_est_timing" = "2016-11-02 08:30:09";
timestamp = 1478075409813132;
"to_xmpp_id" = annungdg1475217973;
"xmpp_message_id" = "F67A8495-E700-485B-A0B3-49C854E4FA45";
}
);
"last_name" = Khan;
"last_played_music" = (
{
album = "Haan Kargi (Jatt.fm)";
artist = "Ammy Virk (Mr-Jatt.com)";
bpm = 0;
"created_at" = "0000-00-00 00:00:00";
duration = "3.55";
genre = "Single Tracks (Mr-Jatt.com)";
id = 69251;
"is_recently_played" = 1;
"persistent_id" = 7705218523513871878;
title = "Haan Kargi (Mr-Jatt.com)";
"updated_at" = "0000-00-00 00:00:00";
"user_id" = 288;
}
);
"matched_profile_image" = (
{
"est_date_time" = "2016-10-04 04:08:34";
id = 479;
"image_order" = 3;
"is_profile_image" = no;
"main_image_id" = 2225;
"main_image_path" = "/files/profile_images/934820161004080721.jpg";
"thumb_image_path" = "/files/profile_images/t_934820161004080721.jpg";
"user_id" = 288;
},
{
"est_date_time" = "2016-10-04 04:08:34";
id = 480;
"image_order" = 2;
"is_profile_image" = no;
"main_image_id" = 2226;
"main_image_path" = "/files/profile_images/685220161004080800.jpg";
"thumb_image_path" = "/files/profile_images/t_685220161004080800.jpg";
"user_id" = 288;
},
{
"est_date_time" = "2016-10-28 06:08:37";
id = 552;
"image_order" = 1;
"is_profile_image" = yes;
"main_image_id" = 2313;
"main_image_path" = "/files/profile_images/825720161028100832.jpg";
"thumb_image_path" = "/files/profile_images/t_825720161028100832.jpg";
"user_id" = 288;
}
);
"short_description" = Church;
username = ghazalah41e21475158541;
},
{
age = 26;
"already_reported" = no;
"already_sync" = no;
"complete_name" = "Michael Berrin";
distance = "7.92490";
"first_name" = Michael;
id = 310;
"last_chat_details" = (
{
"from_xmpp_id" = michaelgbmh1477565051;
id = 9143;
message = Hi;
"message_est_timing" = "2016-11-01 07:43:46";
timestamp = 1477986226750744;
"to_xmpp_id" = annungdg1475217973;
"xmpp_message_id" = "CD8FD64D-1370-41AD-8731-BC2A4C16EB6D";
}
);
"last_name" = Berrin;
"last_played_music" = (
{
album = "T-Series";
artist = "";
bpm = 0;
"created_at" = "0000-00-00 00:00:00";
duration = "5.04";
genre = "";
id = 72284;
"is_recently_played" = 1;
"persistent_id" = 4446247237416765338;
title = "Dheere Dheere Se Meri Zindagi Video Song (OFFICIAL) Hrithik Roshan, Sonam Kapoor | Yo Yo Honey Singh - YouTube";
"updated_at" = "0000-00-00 00:00:00";
"user_id" = 310;
}
);
"matched_profile_image" = (
{
"est_date_time" = "2016-10-27 07:04:10";
id = 543;
"image_order" = 4;
"is_profile_image" = no;
"main_image_id" = 2303;
"main_image_path" = "/files/profile_images/186120161027110402.jpg";
"thumb_image_path" = "/files/profile_images/t_186120161027110402.jpg";
"user_id" = 310;
},
{
"est_date_time" = "2016-10-27 07:12:52";
id = 544;
"image_order" = 3;
"is_profile_image" = no;
"main_image_id" = 2304;
"main_image_path" = "/files/profile_images/132120161027111225.jpg";
"thumb_image_path" = "/files/profile_images/t_132120161027111225.jpg";
"user_id" = 310;
},
{
"est_date_time" = "2016-10-27 07:12:52";
id = 545;
"image_order" = 1;
"is_profile_image" = yes;
"main_image_id" = 2305;
"main_image_path" = "/files/profile_images/270620161027111236.jpg";
"thumb_image_path" = "/files/profile_images/t_270620161027111236.jpg";
"user_id" = 310;
},
{
"est_date_time" = "2016-10-27 07:12:52";
id = 546;
"image_order" = 2;
"is_profile_image" = no;
"main_image_id" = 2306;
"main_image_path" = "/files/profile_images/920120161027111247.jpg";
"thumb_image_path" = "/files/profile_images/t_920120161027111247.jpg";
"user_id" = 310;
}
);
"short_description" = "";
username = michaelgbmh1477565051;
},
{
age = 22;
"already_reported" = no;
"already_sync" = no;
"complete_name" = "Lisa Benson";
distance = "7.92195";
"first_name" = Lisa;
id = 287;
"last_chat_details" = {
};
"last_name" = Benson;
"last_played_music" = (
{
album = "";
artist = "";
bpm = 125;
"created_at" = "0000-00-00 00:00:00";
duration = "3.1";
genre = "";
id = 76890;
"is_recently_played" = 1;
"persistent_id" = 577117298;
title = "Avicii -";
"updated_at" = "0000-00-00 00:00:00";
"user_id" = 287;
}
);
"matched_profile_image" = (
{
"est_date_time" = "2016-09-29 08:54:10";
id = 459;
"image_order" = 2;
"is_profile_image" = no;
"main_image_id" = 2200;
"main_image_path" = "/files/profile_images/858820160929125409.jpg";
"thumb_image_path" = "/files/profile_images/t_858820160929125409.jpg";
"user_id" = 287;
},
{
"est_date_time" = "2016-10-04 06:58:35";
id = 481;
"image_order" = 1;
"is_profile_image" = no;
"main_image_id" = 2227;
"main_image_path" = "/files/profile_images/292820161004105734.jpg";
"thumb_image_path" = "/files/profile_images/t_292820161004105734.jpg";
"user_id" = 287;
}
);
"short_description" = "";
username = lisavmds1475153650;
}
)
Here is my code, please help:
let tempArr: NSArray! = self.filteredArray.sortedArrayUsingComparator({
(obj1:AnyObject!, obj2:AnyObject!) -> NSComparisonResult in
let dateStr1 = (obj1["last_chat_details"] as! NSArray).firstObject!["message_est_timing"] as! String
let date1: NSDate = dateFormatter.dateFromString(dateStr1 as String)!
let dateStr2 = (obj2["last_chat_details"] as! NSArray).firstObject!["message_est_timing"] as! NSString
let date2: NSDate = dateFormatter.dateFromString(dateStr2 as String)!
return date2.compare(date1)
}
)
print("the temp msgs are\(tempArr)")
enter code here
Is this what you want?
let array = [
["message_est_timing" : Date(timeIntervalSince1970: 100)] ,
["message_est_timing" : Date(timeIntervalSince1970: 300)] ,
["message_est_timing" : Date(timeIntervalSince1970: 400)]
]
let newArr = array.sorted { (dict1, dict2) -> Bool in
let date1 = dict1["message_est_timing"]!
let date2 = dict2["message_est_timing"]!
return date1.compare(date2).rawValue == 1 ? true : false
}
newArr will be sorted correctly by date.

how to create nsmutable array with object

I want to create an array like this:
"F017A_order_detail" = (
{
id = 50158;
"is_rx_medicine" = 1;
"medicine_type_id" = 2;
name = "1-11~qwe";
"order_id" = 50128;
price = "<null>";
qty = 12;
strengh = "12 mcg";
},
{
id = 50159;
"is_rx_medicine" = 1;
"medicine_type_id" = 3;
name = "1-12~qwe";
"order_id" = 50128;
price = "<null>";
qty = 12;
strengh = "12 ng/dL";
}
);
How can I create an array with object and key value pair?
How can I add values into the object when I want to add?

Depending up on json values how we have to move view controller from one view to another view?

JSON OUTPUT:
category = (
{
"category_id" = 1;
column = 0;
description = "";
image = "images/Car Wash.jpg";
"meta_description" = "";
"meta_keyword" = "";
name = "Car Wash";
"parent_id" = "";
"sort_order" = 0;
status = enable;
subcategory = (
{
"category_id" = 6;
column = 0;
description = "";
image = "images/hatchpack.jpg";
"meta_description" = "";
"meta_keyword" = "";
name = hatchpack;
"parent_id" = 1;
"sort_order" = 0;
status = enable;
subcategory = (
);
top = "";
}
);
top = "";
},
{
"category_id" = 2;
column = 0;
description = "";
image = "images/Pest Control.png";
"meta_description" = "";
"meta_keyword" = "";
name = "Pest Control";
"parent_id" = "";
"sort_order" = 0;
status = enable;
subcategory = (
);
top = "";
},
{
"category_id" = 3;
column = 0;
description = "";
image = "images/Electrician.jpg";
"meta_description" = "";
"meta_keyword" = "";
name = Electrician;
"parent_id" = "";
"sort_order" = 0;
status = "";
subcategory = (
);
top = "";
},
{
"category_id" = 4;
column = 0;
description = "";
image = "images/Washing Machine.jpg";
"meta_description" = "";
"meta_keyword" = "";
name = "Washing Machine";
"parent_id" = "";
"sort_order" = 0;
status = "";
subcategory = (
);
top = "";
},
{
"category_id" = 5;
column = 0;
description = "";
image = "images/TV Repair.png";
"meta_description" = "";
"meta_keyword" = "";
name = "TV Repair";
"parent_id" = "";
"sort_order" = 0;
status = enable;
subcategory = (
);
top = "";
},
{
"category_id" = 6;
column = 0;
description = "";
image = "images/hatchpack.jpg";
"meta_description" = "";
"meta_keyword" = "";
name = hatchpack;
"parent_id" = 1;
"sort_order" = 0;
status = enable;
subcategory = (
);
top = "";
}
);
success = 1;
}
MY CODE:
for(NSDictionary *DicHoleCategories in ArrCategory)
{
[DicAllValues setObject:[DicHoleCategories objectForKey:#"subcategory"] forKey:#"subcategory"];
[ArrName addObject:DicAllValues];
}
//did select row at indexpath method//
subCategory = [[ArrName objectAtIndex:indexPath.row] objectForKey:#"subcategory"];
if(subCategory==nil||[subCategory isEqual:#""])
{
productScreen *screen = [[productScreen alloc]init];
screen = [self.storyboard instantiateViewControllerWithIdentifier:#"product"];
[self presentViewController:screen animated:YES completion:nil];
}
else
{
CarDetailsVC *car = [[CarDetailsVC alloc]init];
car=[self.storyboard instantiateViewControllerWithIdentifier:#"category"];
[self presentViewController:car animated:YES completion:nil];
}
Here I have mentioned my code properly,getting the subcategory values has been done and I have comparing the values is null or the string is empty,but only else part is working what I have to do please any one help me.
for(NSDictionary *DicHoleCategories in ArrCategory)
{
[DicAllValues setObject:[DicHoleCategories objectForKey:#"subcategory"] forKey:#"subcategory"];
[ArrName addObject:DicAllValues];
}
//did select row at indexpath method//
NSArray *subCategory=[[ArrName objectAtIndex:indexPath.row] objectForKey:#"subcategory"];
if(subCategory.count ==0)//subcategory is an array
{
productScreen *screen=[[productScreen alloc]init];
screen =[self.storyboard instantiateViewControllerWithIdentifier:#"product"];
[self presentViewController:screen animated:YES completion:nil];
}
else
{
CarDetailsVC *car=[[CarDetailsVC alloc]init];
car=[self.storyboard instantiateViewControllerWithIdentifier:#"category"];
[self presentViewController:car animated:YES completion:nil];
}
You have given && condition inside if statement. Both conditions can't be true simultaneously. Moreover, check your string with isEqualToString method.
So put your if condition as given below -
if([subcategory length] ==0){
// Your code goes here.
}

remove Array object content dictionary with null value iOS

I have Json object like this :
{
CO = 1;
CUABR = "<null>";
CUNO = "<null>";
DIV = 2;
DLDAT = 1020923;
SALID = "<null>";
"form_number" = MM23000;
type = VHNO;
},
{
CO = 1;
CUABR = "<null>";
CUNO = "<null>";
DIV = 2;
DLDAT = 1020923;
SALID = "<null>";
"form_number" = MM23001;
type = VHNO;
},
{
CO = 1;
CUABR = "<null>";
CUNO = "<null>";
DIV = 2;
DLDAT = 1020924;
SALID = "<null>";
"form_number" = MM23002;
type = VHNO;
},
{
CO = 1;
CUABR = "\U53f0\U5316\U65b0\U6e2f";
CUNO = FCFC3;
DIV = 2;
DLDAT = 1020924;
SALID = M;
"form_number" = MM23003;
type = VHNO;
}
}
then I need to remove json array content null dictionary key value to sort like this:
{
CO = 1;
CUABR = "\U53f0\U5316\U65b0\U6e2f";
CUNO = FCFC3;
DIV = 2;
DLDAT = 1020924;
SALID = M;
"form_number" = MM23003;
type = VHNO;
}
}
How should I do?
I would like to know the proper way, should I use nest loop or block?
Thanks for helping.
use this may help you..
NSMutableArray *newarray=[NSMutableArray array];
for (NSDictionary *dictionary in parseArrray)
{
if (dictionary[key] != [NSNull null]) {
[newarray addObject:dictionary];
}
}

How to create a sub NSArray that includes key-value pairs?

I have the following NsArray and I want to sort it and create a sub array that include specific key-value pairs.
{
scores = (
{
category = 2;
"league_code" = epl;
"team_away" = Everton;
"team_home" = Liverpool;
},
{
category = 2;
"league_code" = epl;
"team_away" = Fulham;
"team_home" = "Swansea City";
},
{
category = 3;
"league_code" = ita;
"team_away" = Torino;
"team_home" = Milan;
},
{
category = 3;
"league_code" = ita;
"team_away" = Lazio;
"team_home" = Juve;
}
}
}
and I would like to create a new array or just modify array to include only objects with "league_code" = epl key.
So I want to get
{
scores = (
{
category = 2;
"league_code" = epl;
"team_away" = Everton;
"team_home" = Liverpool;
},
{
category = 2;
"league_code" = epl;
"team_away" = Fulham;
"team_home" = "Swansea City";
}
}
}
I tried to convert it to use dictionaryWithObjects:forKeys but it didn't work.
NSMutableArray* newArray = [NSMutableArray array];
NSArray* oldArray = outerDictionary[#"scores"];
for (NSDictionary* dictEntry in oldArray) {
NSString* leagueCode = dictEntry[#"league_code"];
if ([leagueCode isEqualToString #"epl"]) {
[newArray addObject:dictEntry];
}
}
Was that all that hard?
Loop Through and the if it matches your conditions add it to a dictionary. I recommend the for-loop but any loop while do.

Resources