title: Plist to struct class with codable and static variables
body: <p>I have a Plist which has endpoint URLs in it. I want to read that Plist and write it to my URLs struct after app opens and I want to reach these URLs in everywhere. Therefore, variables needs to be static. However, when I do var's static I get below error;</p>
  
  <blockquote>
    <p>'static var' declaration requires an initializer expression or getter/setter specifier.</p>
  </blockquote>
  
  <pre><code>struct URLs: Codable {
          static let urlBaseURL: String
          static let urlCheckCMS: String
          static let urlJSON: String
  
          enum CodingKeys: String, CodingKey {
              case urlBaseURL = "url_base_url"
              case urlCheckCMS = "url_check_cms"
              case urlJSON = "url_json"
          }
      }
  
      // MARK: Convenience initializers
  
      extension URLs {
          init?(data: Data) {
              guard let me = try? PropertyListDecoder().decode(URLs.self, from: data) else { return nil }
              self = me
          }
  
          init?(_ plist: String, using encoding: String.Encoding = .utf8) {
              guard let data = plist.data(using: encoding) else { return nil }
              self.init(data: data)
          }
  
          init?(fromURL url: String) {
              guard let url = URL(string: url) else { return nil }
              guard let data = try? Data(contentsOf: url) else { return nil }
              self.init(data: data)
          }
  
          var plistData: Data? {
              return try? JSONEncoder().encode(self)
          }
  
          var plist: String? {
              guard let data = self.plistData else { return nil }
              return String(data: data, encoding: .utf8)
          }
  </code></pre>
  
  <p>EDIT: In addition to below answer I change data init class with below code to get rid of optional. With below code, I check plist and If return nil when creating init class app won't continue to work;</p>
  
  <pre><code>    init?(data: Data) {
      guard let me = try? PropertyListDecoder().decode(URLs.self, from: data) else { return nil }
      self = me
      URLs.instance = me
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>static var instance: URLs!
  </code></pre>
  
  <p>But I'm still not sure this will be the best approach. As It is discussed in below post;</p>
  
  <p><a href="https://codereview.stackexchange.com/questions/115119/loading-configurations-from-plist-into-singleton">Loading Configurations from plist into singleton</a></p>