Instruction

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

What is JSON?

JSON, or JavaScript Object Notation, is a lightweight data-interchange format that’s easy for humans to read and write, and easy for machines to parse and generate. It’s a text-based format that represents data in key-value pairs and arrays, providing a simple and flexible way to structure and exchange information.

{
  "tasks": [
    {
      "id": 1,
      "title": "Complete Project Proposal",
      "dueDate": "2024-03-01",
      "priority": "high",
      "completed": false
    },
    {
      "id": 2,
      "title": "Review Code Changes",
      "dueDate": "2024-02-28",
      "priority": "medium",
      "completed": true
    }
  ]
}

Advantages of Using JSON in iOS

JSON is an ideal choice for iOS development for several reasons:

Where to Put JSON Files? Understanding the Sandbox

In iOS development, understanding the sandbox is crucial for securely managing your app’s files and data. The sandbox is a protected environment that confines an app’s access to its own directories, ensuring data isolation and security.

How to Locate the Document Directory on your Simulator

cd ~/Library/Developer/CoreSimulator/Devices/{YOUR_SIMULATOR_UDID}/data/Containers/Data/Application/{APP_CONTAINER_UDID}/Documents

When to Write Data to the Document Directory

Deciding when to persist changes to the JSON file in the Document directory involves a thoughtful consideration of various factors and use case requirements. Below are considerations for the two approaches:

Writing Data into the Document Directory

The following Swift code illustrates the process of writing data into the Document directory in JSON format:

// 1
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

// 2
let fileURL = documentsDirectory.appendingPathComponent("data.json")

// 3
if FileManager.default.fileExists(atPath: fileURL.path) {
  try FileManager.default.removeItem(at: fileURL)
}

// 4
try JSONEncoder().encode(data).write(to: fileURL)

Reading Data from the Document Directory

The Swift code snippet below demonstrates the process of reading data from the Document directory in JSON format:

// 1
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

// 2
let jsonFilePath = documentsDirectory.appendingPathComponent("data.json")

// 3
if FileManager.default.fileExists(atPath: jsonFilePath.path) {
  // 4
  let data = try Data(contentsOf: jsonFilePath)
  
  // 5
  let decodedData = try JSONDecoder().decode([Joke].self, from: data)
  print("Data read successfully.")
  return decodedData
} else {
  print("No data found at path:", jsonFilePath.path)
  return nil
}
See forum comments
Download course materials from Github
Previous: Introduction Next: Demo