Data.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // Data.swift
  3. // Haneke
  4. //
  5. // Created by Hermes Pique on 9/19/14.
  6. // Copyright (c) 2014 Haneke. All rights reserved.
  7. //
  8. import UIKit
  9. // See: http://stackoverflow.com/questions/25922152/not-identical-to-self
  10. public protocol DataConvertible {
  11. associatedtype Result
  12. static func convertFromData(_ data:Data) -> Result?
  13. }
  14. public protocol DataRepresentable {
  15. func asData() -> Data!
  16. }
  17. private let imageSync = NSLock()
  18. extension UIImage : DataConvertible, DataRepresentable {
  19. public typealias Result = UIImage
  20. // HACK: UIImage data initializer is no longer thread safe. See: https://github.com/AFNetworking/AFNetworking/issues/2572#issuecomment-115854482
  21. static func safeImageWithData(_ data:Data) -> Result? {
  22. imageSync.lock()
  23. let image = UIImage(data:data, scale: scale)
  24. imageSync.unlock()
  25. return image
  26. }
  27. public class func convertFromData(_ data: Data) -> Result? {
  28. let image = UIImage.safeImageWithData(data)
  29. return image
  30. }
  31. public func asData() -> Data! {
  32. return self.hnk_data() as Data?
  33. }
  34. fileprivate static let scale = UIScreen.main.scale
  35. }
  36. extension String : DataConvertible, DataRepresentable {
  37. public typealias Result = String
  38. public static func convertFromData(_ data: Data) -> Result? {
  39. let string = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
  40. return string as Result?
  41. }
  42. public func asData() -> Data! {
  43. return self.data(using: String.Encoding.utf8)
  44. }
  45. }
  46. extension Data : DataConvertible, DataRepresentable {
  47. public typealias Result = Data
  48. public static func convertFromData(_ data: Data) -> Result? {
  49. return data
  50. }
  51. public func asData() -> Data! {
  52. return self
  53. }
  54. }
  55. public enum JSONV : DataConvertible, DataRepresentable {
  56. public typealias Result = JSONV
  57. case Dictionary([String:AnyObject])
  58. case Array([AnyObject])
  59. public static func convertFromData(_ data: Data) -> Result? {
  60. do {
  61. let object : Any = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions())
  62. switch (object) {
  63. case let dictionary as [String:AnyObject]:
  64. return JSONV.Dictionary(dictionary)
  65. case let array as [AnyObject]:
  66. return JSONV.Array(array)
  67. default:
  68. return nil
  69. }
  70. } catch {
  71. Log.error(message: "Invalid JSON data", error: error)
  72. return nil
  73. }
  74. }
  75. public func asData() -> Data! {
  76. switch (self) {
  77. case .Dictionary(let dictionary):
  78. return try? JSONSerialization.data(withJSONObject: dictionary, options: JSONSerialization.WritingOptions())
  79. case .Array(let array):
  80. return try? JSONSerialization.data(withJSONObject: array, options: JSONSerialization.WritingOptions())
  81. }
  82. }
  83. public var array : [AnyObject]! {
  84. switch (self) {
  85. case .Dictionary(_):
  86. return nil
  87. case .Array(let array):
  88. return array
  89. }
  90. }
  91. public var dictionary : [String:AnyObject]! {
  92. switch (self) {
  93. case .Dictionary(let dictionary):
  94. return dictionary
  95. case .Array(_):
  96. return nil
  97. }
  98. }
  99. }