Platform.Darwin.swift 1005 B

123456789101112131415161718192021222324252627282930313233343536
  1. //
  2. // Platform.Darwin.swift
  3. // Platform
  4. //
  5. // Created by Krunoslav Zaher on 12/29/15.
  6. // Copyright © 2015 Krunoslav Zaher. All rights reserved.
  7. //
  8. #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
  9. import Darwin
  10. import class Foundation.Thread
  11. import protocol Foundation.NSCopying
  12. extension Thread {
  13. static func setThreadLocalStorageValue<T: AnyObject>(_ value: T?, forKey key: NSCopying) {
  14. let currentThread = Thread.current
  15. let threadDictionary = currentThread.threadDictionary
  16. if let newValue = value {
  17. threadDictionary[key] = newValue
  18. }
  19. else {
  20. threadDictionary[key] = nil
  21. }
  22. }
  23. static func getThreadLocalStorageValueForKey<T>(_ key: NSCopying) -> T? {
  24. let currentThread = Thread.current
  25. let threadDictionary = currentThread.threadDictionary
  26. return threadDictionary[key] as? T
  27. }
  28. }
  29. #endif