nsdictionary

Creating a constant dictionary object

两盒软妹~` 提交于 2019-12-01 17:28:12
I would like to accomplish something like what is being done in this post: Constants in Objective-C however, i would like to construct an NSDictionary. if i do something like: constants.h extern NSArray *const mFooKeys; extern NSArray *const mFooObjects; extern NSDictionary *const mFooDictionary; constants.m NSArray *const mFooKeys = [[NSArray alloc] initWithObjects: @"Foo", @"Bar", @"Baz", nil]; NSArray *const mFooObjects = [[NSArray alloc] initWithObjects: @"1", @"2", @"3", nil]; NSDictionary *const mFooDictionary = [[NSDictionary alloc] dictionaryWithObjects:mFooObjects forKeys:mFooKeys];

iOS开发-NSCache

坚强是说给别人听的谎言 提交于 2019-12-01 16:11:41
开发时,经常会遇到一个问题,从网络下载的图片应该如何来缓存?难道每次请求都要去下载一次?流量不要钱哇?所以比较好的方法就是把图片资源下载下来,保存到本地,然后下次直接从本地取出图片就可以了,但是用什么来保存这个图片呢?有些人可能说用NSDictionary(应该是NSMutableDictionary),因为这个类在开发中非常非常常用。但是其实可能有一个更好的选择,是专门为缓存来设计的,没错,就是 NSCache 。非常牛逼的AFN、SDWebImage都是用它来进行图片缓存的。 就像上边说到的,NSCache和NSDictionary很类似,用key-value键值对来存储,但是相对于NSDictionary,他有几点优势: 1.NSCache结合了各种自动删除的策略,用来确保不会占用过多的系统内存。 2.NSCache是线程安全的,在多线程情况下,不需要考虑加锁来保证数据的安全。 3.不像NSMutableDictionary,NSCache不会copy对象,而是保留它。 NSCache 框架 NSCache的类中内容很少,从Xcode进去看也就只有43行,接下来我们来看一下他都提供了哪些方法和属性。 NSCache 的属性 1 @property NSUInteger countLimit; countLimit 用来限制缓存最多维护的对象个数,默认为0,0表示不限制数量。

Pesky new lines and whitespace in XML reader class

亡梦爱人 提交于 2019-12-01 15:23:52
I'm using a class written by a blogger ( http://troybrant.net/blog/ ) that takes an XML string and spits out a NSDictionary. It's beautiful...works perfectly, except I end up with a weird configuration of newlines and white space at the beginning of many element values. I haven't been able to figure out why. I'm posting the class here so I can get a second pair of eyes...see if anyone notices anything suspect in the method he is using. // // XMLReader.m // #import "XMLReader.h" NSString *const kXMLReaderTextNodeKey = @"text"; @interface XMLReader (Internal) - (id)initWithError:(NSError **

Parse JSON array

戏子无情 提交于 2019-12-01 14:19:58
I fetch a JSON array from a web service with touchJSON. Which looks like this: [{"icecream": {"title": "Banana"}}, {"icecream": {"title": "Strawberry"}}] I'm not able to parse this into a NSDictionary, because touchJSON doesn't support JSON arrays. How do I get my JSON array into a NSDicitionary? Regards Have you consider trying another framework? This one seems to support JSON arrays. Maybe you could use another of the many JSON implementations listed on the JSON homepage. You can chekc out the JSON webpage , where they provide links to parsing code in dozens of languages. However, at first

Parse JSON array

女生的网名这么多〃 提交于 2019-12-01 13:07:28
问题 I fetch a JSON array from a web service with touchJSON. Which looks like this: [{"icecream": {"title": "Banana"}}, {"icecream": {"title": "Strawberry"}}] I'm not able to parse this into a NSDictionary, because touchJSON doesn't support JSON arrays. How do I get my JSON array into a NSDicitionary? Regards 回答1: Have you consider trying another framework? This one seems to support JSON arrays. 回答2: Maybe you could use another of the many JSON implementations listed on the JSON homepage. 回答3: You

Array of structs: How to save in coredata?

限于喜欢 提交于 2019-12-01 13:06:21
I´m trying to save an array of structs into coredata. I did a lot of research, but i cannot find the solution. Here´s what i´ve got: import Cocoa import CoreData class ViewController: NSViewController { struct StudentsStruct { let firstName: String let lastName: String let age: Int } let Studentsdata: [StudentsStruct] = [StudentsStruct(firstName: "Albert", lastName: "Miller", age: 24), StudentsStruct(firstName: "Susan", lastName: "Gordon", age: 24), StudentsStruct(firstName: "Henry", lastName: "Colbert", age: 24)] override func viewDidLoad() { super.viewDidLoad() let student: Student =

NSDictionaries and Nested JSON

落花浮王杯 提交于 2019-12-01 11:53:09
I am able to create a NSDictionary from a JSON file no problem, what I need to know is how to do nested JSON strings?? See Example: { "data": [ { "id": "270639882984792_306265986160413", "from": { "category": "Non-profit organization", "name": "My Facebook Page", "id": "270639882984792" }, So this is part of a massive JSON file, but how do I set it up, so that I can call the "name" key from the "from" key - I know how to call the "id" key from the "data" key, but the one I want is a level deeper. Thanks in advance:-) EDIT - here is some iOS code I started working on: NSDictionary *items =

How to create JSON from a dictionary in Swift 4?

99封情书 提交于 2019-12-01 05:47:58
Edit: I have read the other answers on SO for the same issue , however Im unable to get the desired output. I have tried many variations as suggested in other questions but its not working. I have a JSON snipped which needs to be added as the body when I open a websocket. sender: "system1@example.com", recipients:"system2@example.com", data: { text: "Test Message" }, So using Swift I did the following, var messageDictionary : [String: Any] = [ "sender": "system1@example.com", "recipients":"system2@example.com", "data": [ "text": "Test Message" ], ] do { let jsonData = try JSONSerialization

Description of NSDictionary - why are some key names with quotes?

你。 提交于 2019-12-01 05:06:58
I used a simple NSLog on a dictionary: NSLog(@"dict %@", dictionary); the result was: ... "first_name" = Peter; gender = male; id = 1171548848; "last_name" = Lapisu; ... Why are some key names in "quotes" and some not? Apurv When the string has characters apart from alphabets + numerics, it will quote the string. This is the basic methodology of the description function. 来源: https://stackoverflow.com/questions/11538371/description-of-nsdictionary-why-are-some-key-names-with-quotes

Sort NSDictionary keys as NSDate

自古美人都是妖i 提交于 2019-12-01 04:32:32
问题 I'm developing an iOS 4 application with latest SDK and XCode 4.2. I have a NSMutableDictionary where keys are NSDate and values are NSMutableString . When I have filled up the NSMutableDictionary I want to sort its keys but I don't know how can I do it. I want first January dates, then February and so on. The NSDate key format is dd/mm/yyyy . How can I sort those keys when NSMutableDictionary has all its keys and values? (I'm not going to add more). 回答1: That's a pretty standard thing,