swift2 如何在项目的任何ViewController中全局访问Class中的数据

yrwegjxp  于 2022-11-06  发布在  Swift
关注(0)|答案(2)|浏览(148)

我是斯威夫特的新手。
目标:全局访问数据
如何使用Swift实现一个全局临时存储使用下面的类来存储数据
在C#中,
1)我使用具有静态属性的类

Class TransactionData
    {
        public static DateTime dateTime { get; set; }
        public static int    Qty_Purchase { get; set; }
        public static double  ItemPrice { get; set; }
        public static string  Item_Code { get; set; }

   }

2)我可以全局访问和设置或使用数据
交易数据.项目代码=“P200.XXX”;
在斯威夫特,
a)在swift中如何实现此类?
要设计一个类,使用CocoaTouch类还是Swift类?
CocoaTouch类(UIViewControlelr的子类)和Swift类之间有什么不同
a)Swift中是否有一个静态类

public static Class TransactionData
    {
        public static DateTime dateTime { get; set; }
        public static int    Qty_Purchase { get; set; }
        public static double  ItemPrice { get; set; }
        public static string  Item_Code { get; set; }

   }

谢谢

z4iuyo4d

z4iuyo4d1#

如果您的目标是全局访问数据以进行临时存储,则只需为事务创建一个结构:

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

然后只需要创建一个singleton和一个属性来存储你的事务:

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

使用方法:

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"}]

循环处理事务并对其进行编辑:

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"}]

如果您只想拥有一个事务,只需向Shared结构体添加一个可选变量,而不是事务数组:

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

和用法:

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"

}
kqhtkvqz

kqhtkvqz2#

您可以按照步骤。如何在Swift 3中设置共享变量
1.创建模型文件“TimetableModel.swift”

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

1.然后创建任何控制器

import UIKit
class TimetableVC: UIViewController {
    override func viewDidLoad() { // now you can access shared variable  
        TimetableModel.sharedInstance.categories[section].name    TimetableModel.sharedInstance.user_id
    }
}

相关问题