master

SwiftyJSON/SwiftyJSON

Last updated at: 29/12/2023 09:35

ViewController.swift

TLDR

This file is a view controller class in the Demo Projects project. It is responsible for displaying JSON data in a table view. The file also includes methods for configuring the table view data source and handling navigation to other view controllers.

Methods

viewDidLoad

This method is called after the view controller has loaded its view. It sets the title of the view controller to "SwiftyJSON" followed by the type of the JSON data stored in the json property.

tableView(_:numberOfRowsInSection:)

This method is called when the table view is requesting the number of rows in a section. It returns the count of the JSON data stored in the json property, depending on its type. If the JSON data is an array or dictionary, it returns the count of elements. Otherwise, it returns 1.

tableView(_:cellForRowAt:)

This method is called when the table view is requesting a cell for a specific row. It dequeues a reusable cell with the identifier "JSONCell" and configures it based on the JSON data stored in the json property. If the JSON data is an array, it displays the index of the row and the array value. If the JSON data is a dictionary, it displays the key and the corresponding value. Otherwise, it displays an empty text label and the JSON description.

prepare(for:sender:)

This method is called before a segue is performed to prepare the next view controller. It sets the json property of the next view controller based on the selected row and the JSON data stored in the json property. The next view controller is of the same type as the current view controller.

Classes

ViewController

This class is a subclass of UITableViewController. It implements the data source methods of the table view and provides methods for configuring the table view data source and handling navigation to other view controllers. The class also has a json property that stores the JSON data to be displayed in the table view.

//  ViewController.swift
//
//  Copyright (c) 2014 - 2016 Pinglin Tang
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.

import UIKit
import SwiftyJSON

class ViewController: UITableViewController {

    var json: JSON = JSON.null

    // MARK: - Table view data source

	override func viewDidLoad() {
		title = "SwiftyJSON(\(json.type))"
	}

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch self.json.type {
        case .array, .dictionary:
            return self.json.count
        default:
            return 1
        }
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "JSONCell", for: indexPath) as UITableViewCell

        let row = indexPath.row

        switch self.json.type {
        case .array:
            cell.textLabel?.text = "\(row)"
            cell.detailTextLabel?.text = self.json.arrayValue.description
        case .dictionary:
            let key: Any = Array(self.json.dictionaryValue.keys)[row]
            let value = self.json[key as! String]
            cell.textLabel?.text = "\(key)"
            cell.detailTextLabel?.text = value.description
        default:
            cell.textLabel?.text = ""
            cell.detailTextLabel?.text = self.json.description
        }

        return cell
    }

    // MARK: - Navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any!) {

        var nextController: UIViewController?
        nextController = segue.destination

        if let indexPath = self.tableView.indexPathForSelectedRow {
            let row = indexPath.row
            var nextJson: JSON = JSON.null
            switch self.json.type {
            case .array:
                nextJson = self.json[row]
            case .dictionary where row < self.json.dictionaryValue.count:
                let key = Array(self.json.dictionaryValue.keys)[row]
                if let value = self.json.dictionary?[key] {
                    nextJson = value
                }
            default:
                print("")
            }
            (nextController as! ViewController).json = nextJson
            print(nextJson)
        }
    }
}