Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

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

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

In C#,

1) I use a class with static properties

 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) I can access and set or use the data globally

TransactionData.Item_Code = "P200.XXX";

In Swift,

a) How to implement such class in swift?

To design a class, use CocoaTouch class or Swift Class ?

What is the different between CocoaTouch class ( Where it subclass to UIViewControlelr) and Swift Class

a) is there a static class in Swift like

 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; }

   }

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
"

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")
"

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...