JSON serialization
The nsjsonserialization class is added to iOS 5. JSON and foundation objects can be converted to each other. Let's take a look at the code example of an image:
- {"taken": "2011/07/13"
- "width": "3072",
- "height": "2304",
- "latitude": "39.52",
- "longitude": "-106.05",
- "url": "http://mypictures.com/12345.png"
- }
The parsing is simple. Sample Code:
- NSError *error = nil;
- NSData *data = [NSData dataWithContentsOfURL:webServiceURL];
- NSDictionary *photo = [NSJSONSerialization
- JSONObjectWithData:data
- options:NSJSONReadingMutableLeaves
- error:&error];
- NSNumber *width = [photo objectForKey:@"width"];
- NSNumber *height = [photo objectForKey:@"height"];
Complete the work. Do not download the class library. In the example, jsonobjectwithdata: Options: Error: The method is to change the data parameters to nsdictionary.
Nsjsonserialization can easily parse data to the memory or stream. For big data, the advantage of using stream is more obvious, and data can be processed in blocks, in this way, the memory resources are used as few as possible. Let's look at another example. The same paragraph is different from the previous one because the jsonobjectwithstream: variant method is used this time:
- NSError *error = nil;
- NSStream *stream = [self getMyStream];
- NSDictionary *photo = [NSJSONSerialization
- JSONObjectWithStream:stream
- options:NSJSONReadingMutableLeaves
- error:&error];
- NSNumber *width = [photo objectForKey:@"width"];
- NSNumber *height = [photo objectForKey:@"height"];
IOS supports JSON and can even generate JSON data.
ARC (automatic reference count)
Many developers who are familiar with the garbage collection mechanism development language will not get used to using objective-C, and always need to perform a lot of memory management work. Apple believes that garbage collection, which seriously affects power efficiency, conflicts with the inherent real-time performance of mobile devices. However, in iOS 5, Apple introduced an automatic memory management mechanism called arc.
In order to manage the Reserved/released memory, arc has added a new concept called "zero-to-zero weak reference". The weak reference will be cleared once it is no longer referenced. So far, all weak references will not point to the hanging pointer, but will be automatically cleared.
If you use arc, you must declare to the compiler what you are doing with the core foundation object. Otherwise, an error is reported. Let's first look at an example that can result in an error:
- NSDictionary *values =
- [NSDictionary dictionaryWithObject:@"object" forKey:@"key"];
- dictionary = (CFDictionaryRef)values;
- SomeFunctionCallThatNeesACFDictionary(dictionary);
If you only want to use dictionary as the cfdictionaryref class, you only need to add a _ bridge. Let's take a look at the correct example:
- NSDictionary *values =
- [NSDictionary dictionaryWithObject:@"object" forKey:@"key"];
- CFDictionaryRef dictionary = (__bridge CFDictionaryRef)values;
- SomeFunctionCallThatNeesACFDictionary(dictionary);
- NSDictionary *values =
- [NSDictionary dictionaryWithObject:@"object" forKey:@"key"];
- CFDictionaryRef dictionary = (__bridge CFDictionaryRef)values;
- SomeFunctionCallThatNeesACFDictionary(dictionary);