Two hidden features of iOS 5

Source: Internet
Author: User

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:

 
 
  1. {"taken": "2011/07/13"
  2. "width": "3072",
  3. "height": "2304",
  4. "latitude": "39.52",
  5. "longitude": "-106.05",
  6. "url": "http://mypictures.com/12345.png"

The parsing is simple. Sample Code:

 
 
  1. NSError *error = nil;
  2. NSData *data = [NSData dataWithContentsOfURL:webServiceURL];
  3. NSDictionary *photo = [NSJSONSerialization
  4. JSONObjectWithData:data
  5. options:NSJSONReadingMutableLeaves
  6. error:&error];
  7. NSNumber *width = [photo objectForKey:@"width"];
  8. 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:

 
 
  1. NSError *error = nil;
  2. NSStream *stream = [self getMyStream];
  3. NSDictionary *photo = [NSJSONSerialization
  4. JSONObjectWithStream:stream
  5. options:NSJSONReadingMutableLeaves
  6. error:&error];
  7. NSNumber *width = [photo objectForKey:@"width"];
  8. 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:

 
 
  1. NSDictionary *values =
  2.         [NSDictionary dictionaryWithObject:@"object" forKey:@"key"];
  3.  dictionary = (CFDictionaryRef)values;
  4. 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:

 
 
  1.   NSDictionary *values =
  2.         [NSDictionary dictionaryWithObject:@"object" forKey:@"key"];
  3. CFDictionaryRef dictionary = (__bridge CFDictionaryRef)values;
  4. SomeFunctionCallThatNeesACFDictionary(dictionary);
  5. NSDictionary *values =
  6.         [NSDictionary dictionaryWithObject:@"object" forKey:@"key"];
  7. CFDictionaryRef dictionary = (__bridge CFDictionaryRef)values;
  8. SomeFunctionCallThatNeesACFDictionary(dictionary); 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.