Skip to content

Instantly share code, notes, and snippets.

@StanislavK
Created January 12, 2018 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StanislavK/4008ab98e0c2b4a1c556807ba0f50f3b to your computer and use it in GitHub Desktop.
Save StanislavK/4008ab98e0c2b4a1c556807ba0f50f3b to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import UIKit
// Computed property
// NOTE: Since a computed property gets reassigned upon every access!, it can only be declared as a var and not a let.
var urlSession: URLSession {
let urlSessionConfiguration: URLSessionConfiguration = URLSessionConfiguration.default.copy() as! URLSessionConfiguration
urlSessionConfiguration.requestCachePolicy = NSURLRequest.CachePolicy.returnCacheDataElseLoad
// some further configuration
return URLSession(configuration: urlSessionConfiguration)
}
// Stored property
// Stored properties get initialized once
// This is made clear by the fact that this is actually an assignment operation (denoted by the = operator) and that the closure gets executed prior to the assignment,(The () brackets after the closure).
let urlSession1: URLSession = {
let urlSessionConfiguration: URLSessionConfiguration = URLSessionConfiguration.default.copy() as! URLSessionConfiguration
urlSessionConfiguration.requestCachePolicy = NSURLRequest.CachePolicy.returnCacheDataElseLoad
// some further configuration
return URLSession(configuration: urlSessionConfiguration)
}()
for _ in 1...10 {
print(urlSession1
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment