nsdictionary

What is an NSCFDictionary?

这一生的挚爱 提交于 2019-11-27 05:27:11
I'm getting an NSCFDictionary returned to me and I can't figure out how to use it. I know it's of type NSCFDictionary because I printed the class and it came out as __NCSFDictionary. I can't figure out how to do anything with it. I'm just trying to hold onto it for now but can't even get that to work: NSDictionary *dict = [[NSURLCredentialStorage sharedCredentialStorage] allCredentials]; for(NSURLProtectionSpace key in [dict keyEnumerator]) { NSCFDictionary *value = [dict objectForKey:key]; } The class reference for allCredentials says its supposed to return a dictionary whose values are also

Obj-C easy method to convert from NSObject with properties to NSDictionary?

不打扰是莪最后的温柔 提交于 2019-11-27 05:14:35
问题 I ran across something that I eventually figured out, but think that there's probably a much more efficient way to accomplish it. I had an object (an NSObject which adopted the MKAnnotation protocol) that had a number of properties (title, subtitle,latitude,longitude, info, etc.). I needed to be able to pass this object to another object, which wanted to extract info from it using objectForKey methods, as an NSDictionary (because that's what it was getting from another view controller). What

NSDictionary keys sorted by value numerically

旧城冷巷雨未停 提交于 2019-11-27 03:55:41
问题 I store names as keys and scores as values into an NSDictionary for saving in NSUserDefaults . I then want to get back the keys sorted by score, but I can't seem to sort them numerically, only by string. The list of scores 100, 50, 300, 200, 500, for example, gives me 100, 200, 300, 50, 500. Can this be done or do I need to go about this differently? NSString *defaultNames[] = {@"Matt", @"Terry",@"Jessica",@"Sean",nil}; NSNumber *defaultScores[] = {@"600", @"500",@"100",@"50", nil};

Serialize and Deserialize Objective-C objects into JSON

馋奶兔 提交于 2019-11-27 03:39:46
I need to serialize and deserialize objective-c objects into JSON to store in CouchDB. Do people have any example code for best practice for a general solution? I looked at a few JSON framework and they are stopped at the NSDictionary/NSArray level. i.e. A lot of framework will serialize and deserialize NSDictionary/NSArray into JSON. But I still have to do the work to convert NSDictionary into Objective-C objects. To make things more complex, my Object A can have reference to an NSArray/NSDictionary of Object Bs. My question is very similar to this question with addition of the collection

Replace all NSNull objects in an NSDictionary

≯℡__Kan透↙ 提交于 2019-11-27 03:28:42
I'm curious, I currently have an NSDictionary where some values are set to an NSNull object thanks to the help of json-framework. The aim is to strip all NSNull values and replace it with an empty string. I'm sure someone has done this somewhere? No doubt it is probably a four liner and is simple, I am just far too burnt out to figure this out on my own. Thanks! Really simple: @interface NSDictionary (JRAdditions) - (NSDictionary *)dictionaryByReplacingNullsWithStrings; @end @implementation NSDictionary (JRAdditions) - (NSDictionary *)dictionaryByReplacingNullsWithStrings { const

NSPredicate to match “any entry in an NSDatabase with value that contains a string”

烈酒焚心 提交于 2019-11-27 02:31:19
问题 I have an array of dictionaries, similar to the following: ( { Black = "?"; Date = "????.??.??"; Result = "*"; SourceDate = "2007.10.24"; White = "Mating pattern #1"; }, { Black = "?"; Date = "????.??.??"; Result = "*"; SourceDate = "2008.10.24"; White = "About this Publication"; } ) I want to offer the user the ability to search for text either within just the "White" and "Black" fields, or within any field. I've got an NSPredicate for doing just the specific fields: predicate = [NSPredicate

Swift equivalent to `[NSDictionary initWithObjects: forKeys:]`

℡╲_俬逩灬. 提交于 2019-11-27 02:01:21
Is there an equivalent for Swift's native Dictionary to [NSDictionary initWithObjects: forKeys:] ? Say I have two arrays with keys and values and want to put them in a dictionary. In Objective-C I'd do it like this: NSArray *keys = @[@"one", @"two", @"three"]; NSArray *values = @[@1, @2, @3]; NSDictionary *dict = [[NSDictionary alloc] initWithObjects: values forKeys: keys]; Of course I can iterate with a counter through both arrays, use a var dict: [String:Int] and add stuff step by step. But that doesn't seem to be a good solution. Using zip and enumerate are probably better ways of iterating

Are keys and values in an NSDictionary ordered?

落爺英雄遲暮 提交于 2019-11-27 01:45:50
I mean: Is the order of keys and values in an NSDictionary always the same like how they were specified when initializing the NSDictionary? Or should I better maintain a seperate NSArray if I really need to know the order of keys? No, they are not ordered. As long as you don't add or remove any elements from the dictionary, they will remain in the same order, but as soon as you add or remove an element, the new order will be completely different. If you need the keys/values to be ordered, use an NSArray (or some other ordered data structure) instead. NSDictionary keys & values are not ordered.

Best way to save to nsuserdefaults for custom class?

谁说我不能喝 提交于 2019-11-27 01:08:48
问题 If I have a custom class Person which has three variables (which are propertized and synthesized): NSString* theName; float* theHeight; int theAge; Person instances are stored in an NSArray 'Group'. There is only one Group. What is the best way of storing and loading the Group in NSUserDefaults? (bearing in mind that float and int are not legal for NSUserDefaults) 回答1: @Brad Smith's answer is not complete, and even is incorrect in some sense. We have to use NSKeyedArchiver and

Sorting NSDictionary

﹥>﹥吖頭↗ 提交于 2019-11-26 23:10:40
问题 I was wondering if someone can show me how to sort an NSDictionary ; I want to read it starting from the last entry, since the key is Date + Time and I want to be able to append it to an NSMutableString . I was able to read it using an enumerator but I don't get the results I want. Thanks 回答1: For your requirements the easiest way is to create a new array from the keys, sort that, then use the array to reference items from the original dictionary. (Note myComparison is your own method that