nsdictionary

How to remove a null value from NSDictionary

折月煮酒 提交于 2019-12-18 11:49:28
问题 I have a JSON Feed: { "count1" = 2; "count2" = 2; idval = 40; level = "<null>"; "logo_url" = "/assets/logos/default_logo_medium.png"; name = "Golf Club"; "role_in_club" = Admin; } The problem is the "<null>" that I cannot figure out how to remove from the NSDictionary before saving it to NSUserDefaults. Please help me solve this issue. Thankyou! 回答1: Iterate through the dictionary and look for any null entries and remove them. NSMutableDictionary *prunedDictionary = [NSMutableDictionary

Filtering NSArray of NSDictionary objects using NSPredicate

删除回忆录丶 提交于 2019-12-18 10:31:51
问题 I have an NSArray of NSDictionary objects. I want to filter the array based on keys of the dictionaries using NSPredicate. I have been doing something like this: NSString *predicateString = [NSString stringWithFormat:@"%@ == '%@'", key, value]; NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString]; NSArray *filteredResults = [allResultsArray filteredArrayUsingPredicate:predicate]; This works fine if they key passed in is one-word: Color, Name, Age. But it doesn't work if

Is there a way to iterate over a dictionary?

坚强是说给别人听的谎言 提交于 2019-12-18 09:55:21
问题 I know NSDictionaries as something where you need a key in order to get a value . But how can I iterate over all keys and values in a NSDictionary , so that I know what keys there are, and what values there are? I know there is something called a for-in-loop in JavaScript . Is there something similar in Objective-C ? 回答1: Yes, NSDictionary supports fast enumeration. With Objective-C 2.0, you can do this: // To print out all key-value pairs in the NSDictionary myDict for(id key in myDict)

parsing NSJSONReadingAllowFragments

蓝咒 提交于 2019-12-18 08:51:35
问题 I am receiving some json data in my app: NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonResponse options:NSJSONReadingAllowFragments error:nil]; NSLog(@"json :%@", json); which logs: json :{ "email" : "/apex/emailAttachment?documentId=00PZ0000000zAgSMAU&recipientId=003Z000000XzHmJIAV&relatedObjectId=a09Z00000036kc8IAA&subject=Pricing+Comparison" } This is exactly what I want. However, when I go to read the value of the email by doing [json objectForKey:@"email"] I

AFNetworking框架使用

最后都变了- 提交于 2019-12-18 06:23:46
本文是由 iOS Tutorial 小组成员 Scott Sherwood 撰写,他是一个基于位置 动态加载( Dynamically Loaded )的 软件公司(专业的混合定位)的共同创办人。 网络 — 你的程序离开了它就不能生存下去!苹果的Foundation framework中的NSURLConnection又非常难以理解, 不过这里有一个可以使用的替代品: AFNetworking . AFNetworking 非常受开发者欢迎 – 它赢得了我们读者的青睐:2012年最佳的iOS Library奖( 2012 Best iOS Library Award .) 所以现在我就写这篇文章来向你介绍如何在程序中有效的使用它。 AFNetworking 包括了所有你需要与在线资源交互的内容,从web services到文件下载。当你的程序在下载一个大文件期间,AFNetworking还能确保你的UI是可以响应的。 本文将介绍AFNetworking框架主要的组成部分。一路上,你将使用 World Weather Online 提供的咨询(Feeds)来创建一个天气(Weather)程序。刚开始使用的天气数据是静态的,不过在学完本文内容之后,程序将连接到实时的天气咨询。 今日预计:一个很酷的开发者学习所有关于AFNetworking知识,并在他的程序中使用AFNetworking

Grouping NSArray of NSDictionary based on a key in NSDictionay

若如初见. 提交于 2019-12-18 06:06:25
问题 I am trying to filter out a NSArray of NSDictionaries. With my below example, I want dict1, dict2 & dict4 grouped in one array, dict3 & dict5 grouped in second array and dict6 in third array. I am getting this data in NSArray, so essentially the "orig" array below is my input and I know that I need to do grouping based on "Name" key. Instead of looping through the NSArray I though of using valueForKeyPath to return me array based on the key path but this does not work (crashes with logs -

Check if key exists in NSDictionary is null or not

不羁岁月 提交于 2019-12-18 05:40:13
问题 I did search on how to check if NSDictionary key exists or not and came up with the solution. But still it throws me an error saying adding null value to the key. I am not sure if my code is correct or not. If anyone has any idea about this can help me. NSDictionary *result; id myImageURL = [result objectForKey:@"url"]; if ((NSNull *)myImageURL == [NSNull null]) myImageURL = @""; id myImage = [result objectForKey:@"image"]; if ((NSNull *)myImage == [NSNull null]) myImage = @""; Check if null

AFNetworking速成教程

馋奶兔 提交于 2019-12-18 01:23:43
转:http://palmsky.net/?p=4138 本文是由 iOS Tutorial 小组成员 Scott Sherwood 撰写,他是一个基于位置 动态加载( Dynamically Loaded )的 软件公司(专业的混合定位)的共同创办人。 网络 — 你的程序离开了它就不能生存下去!苹果的Foundation framework中的NSURLConnection又非常难以理解, 不过这里有一个可以使用的替代品: AFNetworking . AFNetworking 非常受开发者欢迎 – 它赢得了我们读者的青睐:2012年最佳的iOS Library奖( 2012 Best iOS Library Award .) 所以现在我就写这篇文章来向你介绍如何在程序中有效的使用它。 AFNetworking 包括了所有你需要与在线资源交互的内容,从web services到文件下载。当你的程序在下载一个大文件期间,AFNetworking还能确保你的UI是可以响应的。 本文将介绍AFNetworking框架主要的组成部分。一路上,你将使用 World Weather Online 提供的咨询(Feeds)来创建一个天气(Weather)程序。刚开始使用的天气数据是静态的,不过在学完本文内容之后,程序将连接到实时的天气咨询。 今日预计

How to add to an NSDictionary

强颜欢笑 提交于 2019-12-17 22:29:31
问题 I was using a NSMutableArray and realized that using a dictionary is a lot simpler for what I am trying to achieve. I want to save a key as a NSString and a value as an int in the dictionary. How is this done? Secondly, what is the difference between mutable and a normal dictionary? 回答1: A mutable dictionary can be changed, i.e. you can add and remove objects. An immutable is fixed once it is created. create and add: NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10]

creating JSON format in Objective C

给你一囗甜甜゛ 提交于 2019-12-17 19:48:34
问题 For an application i want to create a JSON in format as mentioned below, "Students" : { "results": { "Grade1": { "studentresult": "pass", "marksheet": "provided" }, "ID": 01, "Name": "Student1", } } I am using the following code to create the same, NSMutableDictionary *gradedetails = [[NSMutableDictionary alloc] init]; [gradedetails setObject:@"pass" forKey:@"studentresult"]; [gradedetails setObject:@"provided" forKey:@"marksheet"]; NSMutableDictionary *sdetails = [[NSMutableDictionary alloc]