mirror of
https://github.com/hyperoslo/Cache.git
synced 2026-04-07 19:17:36 +00:00
32 lines
614 B
Swift
32 lines
614 B
Swift
import Foundation
|
|
|
|
// MARK: - Cachable
|
|
|
|
/**
|
|
Implementation of Cachable protocol.
|
|
*/
|
|
extension String: Cachable {
|
|
public typealias CacheType = String
|
|
|
|
/**
|
|
Creates a string from Data.
|
|
- Parameter data: Data to decode from
|
|
- Returns: An optional CacheType
|
|
*/
|
|
public static func decode(_ data: Data) -> CacheType? {
|
|
guard let string = String(data: data, encoding: String.Encoding.utf8) else {
|
|
return nil
|
|
}
|
|
|
|
return string
|
|
}
|
|
|
|
/**
|
|
Encodes a string to Data.
|
|
- Returns: Optional Data
|
|
*/
|
|
public func encode() -> Data? {
|
|
return data(using: String.Encoding.utf8)
|
|
}
|
|
}
|