nsdictionary

Swift: looping into array of dictionaries (Error: Any is not convertible to NSDictionary)

旧城冷巷雨未停 提交于 2019-12-02 09:08:29
I'm trying to run for loop in an Array of dictionaries. But I'm getting this error: Any is not convertible to NSDictionary Here is my implementation: let content:NSArray = json .object(forKey: "books") as! NSArray for contentDic:NSDictionary in content { print(contentDic) } How can I cast contentDic to Dictionary or NSDictionary without having this error? I'll really appreciate your help. If you can guarantee that your content is an array of NSDictionary , you can: for contentDic:NSDictionary in content as! [NSDictionary] { print(contentDic) } Consider the native Swift way: if let content =

How to set Dictionary in NSUserDefault in swift?

房东的猫 提交于 2019-12-02 08:16:30
问题 I've a mutable dictionary (in form of [Int:Int]) and want that to save it. I would use NSUserDefaults like that: var myDic: NSMutableDictionary = [:] myDic = [1:2] NSUserDefaults.standardUserDefaults().setObject(myDic, forKey: "myDic") but with that I get an error: Thread 1: signal SIGABRT I have no idea why. 回答1: setObject(_:forKey:) can’t accept Dictionary with a key which is integer type. The method requires property-list objects, but myDic = [1:2] is not property-list object. There are

Remove duplicates in NSdictionary

空扰寡人 提交于 2019-12-02 08:10:52
问题 Is there a way to remove duplicate (key-value) pairs from NSDictionary ? EDIT: My description was misleading, I have duplicate pairs e.g. key1-value1 key1-value1 key2-value2 key1-value1 etc.. 回答1: reversing key-value is not good idea because not all values can be keys. You can do it with: // dict is original dictionary, newDict new dictionary withot duplicates. NSMutableDictionary * newDict = [NSMutableDictionary dictionaryWithCapacity:[dict count]]; for(id item in [dict allValues]){ NSArray

Sort NSDictionary

六月ゝ 毕业季﹏ 提交于 2019-12-02 08:01:42
I got NSDictionary with pairs like : Key -> Value: @"home" -> @"blue" @"family" -> @"green" @"work" -> @"red" Updated Internet said no one can sort NSDictionary :c This code helped me with values. sortedArray = [anArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; But i lose relation between keys and values. If anybody can write function, which take NSDictionary and make two NSArray, which related and one of those arrays sorted by values of NSDictionary like this: (unsorted) Key -> Value: @"work" -> @"red" @"home" -> @"blue" @"family" -> @"green" convert to: Arrays,

NSNull crashes my initWithDictionary

我的梦境 提交于 2019-12-02 07:53:50
I am parsing a JSON file. After getting the NSDictionary, I parse the objects in the dictionary into an array of objects. However, for certain JSON files, I get NULL, which should be fine, but it crashes my app for those place where I am expecting something but getting null: - (id)initWithDictionary:(NSDictionary *)boxDictionary { if ([self init]) { // ... int numberOfBoxes = [[boxDictionary valueForKey:@"box_count"] intValue]; int numberOfItemsInBoxes = [[boxDictionary valueForKey:@"box_items_count"] intValue]; // .. } return self; } The basic problem seems to be that there is no intValue

Remove duplicates in NSdictionary

旧巷老猫 提交于 2019-12-02 06:22:25
Is there a way to remove duplicate (key-value) pairs from NSDictionary ? EDIT: My description was misleading, I have duplicate pairs e.g. key1-value1 key1-value1 key2-value2 key1-value1 etc.. Vladimir reversing key-value is not good idea because not all values can be keys. You can do it with: // dict is original dictionary, newDict new dictionary withot duplicates. NSMutableDictionary * newDict = [NSMutableDictionary dictionaryWithCapacity:[dict count]]; for(id item in [dict allValues]){ NSArray * keys = [dict allKeysForObject:item]; [newDict setObject:item forKey:[keys objectAtIndex:0]]; }

How to save a NSMutableDictionary into a file in documents?

为君一笑 提交于 2019-12-02 05:59:50
I would like to save the content of a NSMutableDictionary object to a file. How do I do this ? I already know how to do this task with a NSDictionary object but I don't know how to convert/copy this NSMutableDictionary to a NSDictionary...unless there's a method to write directly the content of NSMutableDictionary to a file...I stress that the NSMutableDictionary object contains objects of NSDictionary type. Thx for helping, Stephane NSMutableDictionary is a subclass of NSDictionary : you can use it anywhere you'd use NSDictionary . Literally, just pass the object through to the same code you

NSPredicate on nested array with NSDictionary as object

回眸只為那壹抹淺笑 提交于 2019-12-02 04:46:46
i have a NSDictionary like: { "2017-05-02" = ( { "always_valid" = 0; date = "2017-05-02"; from = "12:00"; to = "13:00"; }, { "always_valid" = 0; date = "2017-05-02"; from = "12:00"; to = "12:00"; }, { "always_valid" = 0; date = "2017-05-02"; from = "14:00"; "hourly_rate" = 12; to = "15:00"; } ); "2017-05-03" = ( { "always_valid" = 0; date = "2017-05-03"; from = "12:00"; to = "13:00"; } ); "2017-05-18" = ( { "always_valid" = 1; date = "2017-05-18"; from = "12:00"; to = "12:00"; } ); } i'm trying to apply NSPredicate *filter = [NSPredicate predicateWithFormat:@"always_valid = \"1\""]; NSArray

write into plist file using NSDictionary object

▼魔方 西西 提交于 2019-12-02 04:27:13
问题 Sorry I saw similar questions but they don't seem to have some full answers for me. And i try to put it in order so that people will not hate me or my poor english. I am working with Xcode 4.2 with storyboard and ARC I can read from my plist file. My task is simply to write back the updated value(s) to my plist file. My plist is contain in "supporting files" sub folder of the main folder (where story-board is things goes). the file is call Global.plist and GlobalValue2 is a element of the

How to set Dictionary in NSUserDefault in swift?

随声附和 提交于 2019-12-02 04:24:05
I've a mutable dictionary (in form of [Int:Int]) and want that to save it. I would use NSUserDefaults like that: var myDic: NSMutableDictionary = [:] myDic = [1:2] NSUserDefaults.standardUserDefaults().setObject(myDic, forKey: "myDic") but with that I get an error: Thread 1: signal SIGABRT I have no idea why. setObject(_:forKey:) can’t accept Dictionary with a key which is integer type. The method requires property-list objects, but myDic = [1:2] is not property-list object. There are two documents about it. setObject(_:forKey:) of NSUserDefaults The value parameter can be only property list