Difficulty: Easy
Correct Answer: NSJSONSerialization
Explanation:
Introduction / Context:
JSON JavaScript Object Notation is a very common data format for communication between mobile apps and web services. In iOS development, Apple introduced a native API to handle JSON without relying on third party libraries. Knowing the correct framework class to use is basic knowledge for any iOS developer. This question asks you to identify that built in JSON framework class.
Given Data / Assumptions:
Concept / Approach:
In the Foundation framework, Apple provides NSJSONSerialization as the main class for converting between JSON data and Foundation objects such as NSDictionary and NSArray. This class offers methods like JSONObjectWithData:options:error: to parse JSON into Objective C objects, and dataWithJSONObject:options:error: to create JSON data from objects. NSXMLParser handles XML, NSPropertyListSerialization handles property lists, and NSURLConnection historically handled network connections, not JSON itself. Therefore NSJSONSerialization is the correct answer.
Step-by-Step Solution:
Step 1: Recall that the native JSON API introduced by Apple is part of the Foundation framework.
Step 2: Identify the class that has methods specifically for JSON serialisation and deserialisation.
Step 3: Confirm that NSJSONSerialization fits this role and is used throughout example code and documentation.
Step 4: Compare other classes: NSXMLParser focuses on XML, NSPropertyListSerialization on plist formats, and NSURLConnection on HTTP networking.
Step 5: Select NSJSONSerialization as the correct built in JSON framework class.
Verification / Alternative check:
You can verify by checking Apple documentation or by remembering typical code patterns in an iOS project. For example, after receiving data from a server, developers often write code that calls [NSJSONSerialization JSONObjectWithData:data options:0 error:&error] to parse it. They do not use NSXMLParser for JSON, since that parser expects XML structure. This confirms the practical usage of NSJSONSerialization for JSON tasks.
Why Other Options Are Wrong:
Option b, NSXMLParser, is used for parsing XML documents element by element, which is a different format. Option c, NSPropertyListSerialization, reads and writes property lists in formats like XML and binary plist, not general JSON. Option d, NSURLConnection, establishes HTTP connections and transfers data, but does not itself parse or generate JSON; it is usually combined with NSJSONSerialization in older code bases.
Common Pitfalls:
A common mistake is to install third party JSON libraries when the native class already provides all required functionality on modern iOS versions. Another pitfall is ignoring error handling; NSJSONSerialization can return nil and an NSError when JSON is invalid, so robust code must check the result. Understanding which Foundation class is responsible for which data format is key for clean, maintainable iOS code.
Final Answer:
The JSON framework class supported by iOS is NSJSONSerialization.
Discussion & Comments