Created
January 12, 2018 08:35
-
-
Save StanislavK/4008ab98e0c2b4a1c556807ba0f50f3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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