nsdictionary

Parsing a JSON array into a NSDictionary

旧时模样 提交于 2019-11-29 02:02:26
I'm working with the Weather Underground API to make an app and I've hit a snag while parsing the block relating to severe alerts. The JSON uses key-value pairs that have sub key value pairs -- which haven't been a problem for me, as I can make subsequent NSDictionaries out of those -- but the entry for severe alerts has proven problematic. See below: "alerts": [ { "type": "WAT", "description": "Flash Flood Watch", "date": "3:13 PM EDT on April 28, 2012", "date_epoch": "1335640380", "expires": "8:00 AM EDT on April 29, 2012", "expires_epoch": "1335700800", "message": "\u000A...Flash Flood

From array of dictionaries, make array containing values of one key

杀马特。学长 韩版系。学妹 提交于 2019-11-29 01:49:12
I have an array of dictionaries. I would like to extract an array with all the elements of one particular key of the dictionaries in the original array. Can this be done without enumeration? Yes, use the NSArray -valueForKey: method. NSArray *extracted = [sourceArray valueForKey:@"a key"]; Yes, just use Key-Value Coding to ask for the values of the key: NSArray* names = [NSArray arrayWithObjects: [NSDictionary dictionaryWithObjectsAndKeys: @"Joe",@"firstname", @"Bloggs",@"surname", nil], [NSDictionary dictionaryWithObjectsAndKeys: @"Simon",@"firstname", @"Templar",@"surname", nil],

Send Nested JSON using AFNetworking

99封情书 提交于 2019-11-29 01:36:53
To send registration data to server, I am using JSON is in following form: {"regData": { "City":"Some City", "Country":"Some Country", "Email_Id":"abc@gmail.com", "MobileNumber":"+00xxxxxxxxxx", "UserName":"Name Of user" } } Here is how am sending. NSURL * url = [[NSURL alloc] initWithString:registerUrlString]; AFHTTPClient * httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; httpClient.parameterEncoding = AFJSONParameterEncoding; [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]; NSDictionary * params = @{@"regData": @{ @"City": self.cityField.text, @"Country": self

Using valueForKeyPath on NSDictionary if a key starts the @ symbol?

谁说我不能喝 提交于 2019-11-29 01:21:31
I want to use valueForKeyPath on my NSDictionary , but the problem is that one of the keys is a string that starts with the @ symbol. I have no control over the naming of the key. I'm having problems trying to create the key path as I'm getting a format exception, even when trying to escape the @ symbol: This works fine: [[[dict objectForKey:@"key1"] objectForKey:@"@specialKey"] objectForKey:@"key3"] However none of these work: [dict valueForKeyPath:@"key1.@specialKey.key3"] [dict valueForKeyPath:@"key1.@@specialKey.key3"] Any ideas? Thanks, Mike pxl you shouldn't be using @ signs with your

What's the difference between a dictionary and an array?

假装没事ソ 提交于 2019-11-28 21:26:30
What is the difference between a dictionary and an array, especially when working with PLIST files? What are the advantages of using one over the other? Thanks! Both NSDictionary and NSArray are collection classes, i.e. the group together other objects. An NSArray is an 'ordered collection' - every item in the collection has an integer index, so there is an explicit order to the items. If you swap the order of items in the collection then the collection is no longer the 'same' as the order is different. An object may appear more than once in the collection. An NSSet is an 'unordered collection

json解包与json封包

人走茶凉 提交于 2019-11-28 21:21:21
首先,对两个名词进行简单的说明: 1.NSData 用来存储二进制的数据类型。NSData类提供了一种简单的方式,它用来设置缓冲区、将文件的内容读入缓冲区,或将缓冲区的内容写到一个文件。不变缓冲区(NSData类),也可定义可变的缓冲区(NSMutableData类)。 2.json 作为一种轻量级的数据交换格式,正在逐步取代XML,成为网络数据的通用格式。 小结:我们只需要明白NSData类型是用来存储二进制数据的,json是一种数据格式,注意是格式。 接着,先用一段文字简单描述一下json解包和json封包: iOS5.0以后,苹果SDK推出了自带的json解决方案NSJSONSerialization,这是一个非常好用的json生成和解析工具,效率也是比其他第三方开源项目的高很多。 NSJSONSerialization提供了json数据封包、Json数据解包。 NSJSONSerialization提供了将json数据转换为NSDictionary或NSArray的解包方法,也提供了将 NSDictionary、NSArray对象转换为json数据(可以通过调用isValidJSONObject来判断 NSDictionary、NSArray对象是否可以转换为json数据)的封包方法。 然后,用一张图试着说明两件事。其一是,利用 NSJSONSerialization

Convert NSDictionary to Swift Dictionary

天涯浪子 提交于 2019-11-28 21:03:04
Now I know that when swift compiles it just makes a NSDictionary, but the NSDictionary and Swift dictionaries have different syntax. Is there a way (through a loop or something) to convert a NSDictionary to a swift dictionary of the same type for <key, value> ? OR Is there a way to convert this to a Swift dictionary instead of NSDictionary? let jsonDict = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: &error) as NSDictionary use: let jsonDic = NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers, error: &error) as Dictionary

Writing swift dictionary to file

天大地大妈咪最大 提交于 2019-11-28 20:44:26
There are limitations with writing NSDictionaries into files in swift. Based on what I have learned from api docs and this stackoverflow answer , key types should be NSString, and value types also should be NSx type, and Int, String, and other swift types might not work. The question is that if I have a dictionary like: Dictionary<Int, Dictionary<Int, MyOwnType>> , how can I write/read it to/from a plist file in swift? rintaro Anyway, when you want to store MyOwnType to file, MyOwnType must be a subclass of NSObject and conforms to NSCoding protocol. like this: class MyOwnType: NSObject,

Keep blocks inside a dictionary

吃可爱长大的小学妹 提交于 2019-11-28 20:42:34
I have my own method that takes a block as an argument. I want to keep track of that block inside an NSDictionary. What is the best way to add the block to the dictionary? I tried this code but after executing the line below (setObject...) the dictionary is still empty. I presume that is because the block is not of type NSObject. But what is the right way to do this? - (void)startSomething:(NSURLRequest*)request block:(void (^)(NSURLResponse*, NSData*, NSError*))handler { NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; [pendingRequests setObject

get values of particular key in nsdictionary

元气小坏坏 提交于 2019-11-28 20:16:34
问题 I saved the json parsing result in a dictionary which look like: { "statusCode":"200", "body":[ { "status":"success", "remarks":null } ], "data":[ "abcd":[ { "category":"a", "title":"b", "id":"24" }, { "category":"c", "title":"crd", "id":"65" }, { "category":"ds", "title":"sd", "id":"18" } ] }, { "efgh":[ { "category":"ds", "title":"sd", "id":"18" }, { "category":"sd", "title":"sd", "id":"1" } ] }, { "ijkl":[ { "category":"ds", "title":"sd", "id":"18" }, { "category":"sd", "title":"sd", "id":