简体   繁体   中英

Swift- Use data through app

I am developing a quote of the day app so I created a quote class:

import Foundation
class Quote {
  let quote: String?
  let author: String?
  let backgroundImage: UIImage?
}

Then I created a loading screen that fetches the quote of the day data from an external database and creates a new instance from class Quote with the data fecthed:

import UIKit
import Parse
class LoadingViewController: UIViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
    //
    //check for internet connection and fecth data
    //
    //assign each data to a variable of quote object 
    ///q, a and bI are variables that represents quote, author and image

    let quoteOfTheDay = Quote()
    quoteOfTheDay.quote = q
    quoteOfTheDay.author = a
    quoteOfTheDay.backgroundImage = bI

    //---perform segue to main view controller
  }

}

After that, I want to share this quote of the day instance with all my other viewControllers until my app terminates. If it quits, the user will be directed to the loading view controller again to get the data. Otherwise, this quoteOfTheDay instance needs to stay active and be used by other screens as if it was cached while the app is active. How can I do this?

Note that I don't want to keep passing this instance with the use of prepareForSegue.

Thank You!

You can use a singleton like this:

import Foundation
class Quote {
  let quote: String?
  let author: String?
  let backgroundImage: UIImage?

  class var sharedInstance: Quote {
    struct Singleton {
      static let instance = Quote()
    }
    return Singleton.instance
  }
}

Now you can just call

Quote.sharedInstance

More details about design patterns in swift here

You will find a lot of code demos for singleton in here

@rmaddy and @adamPro raise important aspects I forgot to mention:

  • You must be careful with this designer as it stays in memory for the life of your application (so no load images and videos here, and do not abuse this kind of designer too)
  • Also as the name suggest you can just create one instance of a singleton, if you think you may need to in the future you should look for a more appropriate method to use
  • Singletons does not have a very good view in most of the apple community, seems that this is changing with swift, however it is vastly used in other languages

You can set up a static property in your Quote class to store the quote of the day.

class Quote: NSObject {
     static var quoteOfTheDay: Quote?

     //Other Quote Code...
}

Then to access:

let quote = Quote.quoteOfTheDay as Quote!

Keep in mind though that if the quoteOfTheDay is nil when you try to access it using the method above, the app will crash. So you'll want to either consider accessing it in a more secure way or not storing it as an optional.

Have you considered writing the Quote object to a disk archive? Perhaps in Application Support? By using a naming convention that utilizes the date the quote is generated, a user could then retrieve it even in a later launch on the same day. You could then delete it on the next launch on a different day.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM