Best temporary storage measure

送分小仙女□ 提交于 2019-12-11 12:19:52

问题


I am working on an app where i needed to move between view controllers a lot of time mostly making a loop but now i have to store some data(simple variable's and array's).

I am currently storing them in the app delegate but i don't know if this is a great idea. i have looked online but i couldn't really tell what is the best solution for me.
This is how i have it set up: Appdelegate:

var aantalSpelers: Int!
var namenSpelers = [String]()

var youself = KaartenSpeler()
var player2 = KaartenSpeler()
var player3 = KaartenSpeler()
var player4 = KaartenSpeler()
var player5 = KaartenSpeler()
var player6 = KaartenSpeler()

var Vragen = [[0,0,0,0,0,5]]
var VragenOnbekend = [[6,0,0,0,0,0]]
var VragenInformatie = [[["Spelletjeskamer",""],["Keuken",""],["Garage",""],["Binnenplaats",""],["Zitkamer",""],["Slaapkamer",""],["Studeerkamer",""],["Eetkamer",""],["Badkamer",""]], [["De Wit",""],["Pimpel",""],["Blaauw van Draet",""],["Roodhart",""],["Groenewoud",""],["Van Geelen",""]], [["Loden pijp",""],["Pistool",""],["Engelse sleutel",""],["Dolk",""],["Touw",""],["Kandelaar",""]]]
var EersteKeerMainScreen = true

and in the VC:

func Appdelegate() -> AppDelegate {
    return UIApplication.sharedApplication().delegate as! AppDelegate
}

let sections = ["Locaties","Personages","Wapens"]

var aantalSpelers: Int!
var namenSpelers = [String]()
var eersteKaarten = [[Int(),Int()]]

var youself: KaartenSpeler!
var player2: KaartenSpeler!
var player3: KaartenSpeler!
var player4: KaartenSpeler!
var player5: KaartenSpeler!
var player6: KaartenSpeler!

//vraag is: [numberVrager,numerGevraagde,numerLocatie,numerPersonage,numerWapen,0=lietgeenkaartzien - 1=locatie, - 2=personage - 3=wapen - 4=onbekend]
var Vragen = [[]]
var VragenOnbekend = [[]]
var VragenInformatie = []

var EersteKeerMainScreen = false

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    //Get information back from appdelegate
    aantalSpelers = Appdelegate().aantalSpelers
    namenSpelers = Appdelegate().namenSpelers

    youself = Appdelegate().youself
    player2 = Appdelegate().player2
    player3 = Appdelegate().player3
    player4 = Appdelegate().player4
    player5 = Appdelegate().player5
    player6 = Appdelegate().player6

    Vragen = Appdelegate().Vragen
    VragenOnbekend = Appdelegate().VragenOnbekend
    VragenInformatie = Appdelegate().VragenInformatie
    EersteKeerMainScreen = Appdelegate().EersteKeerMainScreen

And is this actually a viable option?

P.S. sorry for my bad english


回答1:


The approach I would use (may not be the best) in this case is to pass the variables with you using segues through the use of self.performSegueWithIdentifier("segueTitle", sender).

Then you can in a prepareForSegue actually handle the objects you moving from this VC to another VC.

e.g.

/*Global Variable is numbersArray*/
var numbersArray: [String] = []

and whenever you finish writing your logic for the view controller, use:

self.performSegueWithIdentifier("segueTitle", sender: self)

Now to handle what you want to pass to the next VC, add this:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if (segue.identifier == "segueTitle") {
            // pass data to next view
            let destinationVC = segue.destinationViewController as! YourNextViewController
            destinationVC.numbersArray = self.numbersArray;

    }
}

Things to keep in mind is that you must have the same global variable in next VC as you can see we are assigning current to next in the above example. Also make sure that segue is created and connected via Storyboard and replace segueTitle with title given to it.

Update: E.g. TransferClass

class TranferClass {
   var numbersArray:[String] = []

}

then instantiate this class and send it to next VC.Eg.:

in VC 1:

/*in global scope*/
var transferObject = TransferClass()

in the same VC, set the array for example:

self.transferObject.numbersArray[0] = "Hello"

then trigger the segue:

self.performSegueWithIdentifier(...)

then handle the passing:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
            if (segue.identifier == "segueTitle") {
                // pass data to next view
                let destinationVC = segue.destinationViewController as! YourNextViewController
                destinationVC.transferObject = self.transferObject;

        }
    }



回答2:


Your initial thought in that you probably shouldn't store these in the AppDelegate. I recommend either creating a new class or a struct that can hold these values.

But the other thing I noticed is that you have a separate class already for KaartenSpeler so you definitely want to look out for retain cycles and make sure that these classes don't hold strong references to one another.



来源:https://stackoverflow.com/questions/35004043/best-temporary-storage-measure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!