How to access data in Class globally in any ViewController in the project

前端 未结 2 1084
误落风尘
误落风尘 2020-12-02 03:07

I am a newbie in Swift.

Objective: Access the data globally

How to use Swift to implement a global Temporary storage using below class to store data

<
相关标签:
2条回答
  • 2020-12-02 03:41

    You can follow the step. How to set shared variable in Swift 3

    1. Create a model file "TimetableModel.swift"

      import Foundation
      class TimetableModel {
          static let sharedInstance = TimetableModel()
          let categories:[Day]        
          var user_id = Int()
          // define any variable here
      }
      
    2. After that create any controller

      import UIKit
      class TimetableVC: UIViewController {
          override func viewDidLoad() { // now you can access shared variable  
              TimetableModel.sharedInstance.categories[section].name    TimetableModel.sharedInstance.user_id
          }
      }
      
    0 讨论(0)
  • 2020-12-02 03:44

    If your objective is to access the data globally for temporary storage, just create a struct for your transaction:

    struct Transaction {
        let date: Date
        let quantity: Int
        let price: Double
        let code: String
    }
    

    Then just need to create a singleton and a property to store your transactions:

    class Shared {
        private init() { }
        static var instance = Shared()
        var transactions: [Transaction] = []
    }
    

    usage:

    Shared.instance.transactions.append(Transaction(date: Date(), quantity: 2, price: 0.99, code: "item01"))
    Shared.instance.transactions.append(Transaction(date: Date(), quantity: 3, price: 2.99, code: "item02"))
    
    Shared.instance.transactions  // [{date "Jan 10, 2017, 2:00 AM", quantity 2, price 0.99, code "item01"},
                                  //  {date "Jan 10, 2017, 2:00 AM", quantity 3, price 2.99, code "item02"}]
    

    looping through your transactions and editing it:

    for (index, transaction) in Shared.instance.transactions.enumerated() {
        print(transaction)
        if transaction.code == "item02" {
            Shared.instance.transactions[index] = Transaction(date: transaction.date, quantity: transaction.quantity, price: transaction.price, code: "NEW CODE")
        }
    }
    
    Shared.instance.transactions   // [{date "Jan 10, 2017, 2:42 AM", quantity 2, price 0.99, code "item01"}, {date "Jan 10, 2017, 2:42 AM", quantity 3, price 2.99, code "NEW CODE"}]
    

    If you want to have just a single transaction just add an optional variable to your Shared struct instead of the transactions array:

    class Shared {
        private init() { }
        static var instance = Shared()
        var transaction: Transaction?
    }
    

    and usage:

    Shared.instance.transaction = Transaction(date: Date(), quantity: 5, price: 5.0, code: "P200")
    
    print(Shared.instance.transaction?.code ?? "")   // "P200\n"
    
    if let transaction = Shared.instance.transaction {
        Shared.instance.transaction = Transaction(date: transaction.date, quantity: transaction.quantity, price: transaction.price, code: "P300")
        print(Shared.instance.transaction!)   // "Transaction(date: 2017-01-10 05:13:58 +0000, quantity: 5, price: 5.0, code: "P300")\n"
    
    }
    
    0 讨论(0)
提交回复
热议问题