68727

optional variable in class definition

Question:

<strong>optionals in class definition</strong>

I have a 'mastermodel' from which most of my models inherit so they can have the configuration constants

class MasterModel { static let apiKey = (drop.config["app","thinx-api-key"]?.string)! static let baseURL = (drop.config["app","base-URL"]?.string )! }

Notice the force unwraps :( In this case it's not really a huge problem as the program won't start without these constants but I'd like to clean this up anyway.

guard statements are only allowed in functions, not in the class definition. What is the proper way to define those constants with error trapping

Answer1:

You could assign them with a computed closure to detect the configuration error

class MasterModel { static let apiKey:String = { if let result = drop.config["app","thinx-api-key"]?.string { return result } print("MasterModel.apiKey error, missing app/thinx-api-key") return "" }() // the () here makes the closure execute and return the value // ... }

Answer2:

If you like your program to crash only if you actually use the property, you could use a computed/lazy one:

class MasterModel { static var apiKey: String { get { return drop.config["app","thinx-api-key"]?.string)! } } ... }

This might be useful in case the static initialiser is called before drop has been successfully initialised.

Recommend

  • java.io.EOFException: End of input at line 1 column 1
  • how to do a GET request using retrofit2?
  • Retrofit 2 error: NetworkOnMainThreadException
  • RxJava,Retrofit Error :Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
  • Base Internationalization and “Could not find a storyboard named […]”
  • UITableView takes much longer to load when numberOfRows returns a large number
  • In matplotlib, how do you change the fontsize of a single figure?
  • Spring: No transaction manager has been configured
  • Memory error in python- how to use more memory
  • accepts_nested_attributes_for practical form use for in Rails 3
  • Differences in dis-assembled C code of GCC and Borland?
  • Object and struct member access and address offset calculation
  • How to attach a node.js readable stream to a Sendgrid email?
  • Algorithm for a smudge tool?
  • Why value captured by reference in lambda is broken? [duplicate]
  • Join two tables and save into third-sql
  • How to model a transition system with SPIN
  • Javascript + PHP Encryption with pidCrypt
  • Hazelcast - OperationTimeoutException
  • ORA-29908: missing primary invocation for ancillary operator
  • Apache 2.4 - remove | delete | uninstall
  • RestKit - RKRequestDelegate does not exist
  • AT Commands to Send SMS not working in Windows 8.1
  • Circular dependency while pushing http interceptor
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Revoking OAuth Access Token Results in 404 Not Found
  • AngularJs get employee from factory
  • python regex in pyparsing
  • How to set the response of a form post action to a iframe source?
  • Why joiner is not used after Sequence generator or Update statergy
  • Change div Background jquery
  • Android Google Maps API OnLocationChanged only called once
  • apache spark aggregate function using min value
  • Checking variable from a different class in C#
  • Recursive/Hierarchical Query Using Postgres
  • Sorting a 2D array using the second column C++
  • UserPrincipal.Current returns apppool on IIS
  • Converting MP3 duration time
  • How can I use threading to 'tick' a timer to be accessed by other threads?