Files
hyperoslo-Cache/Source/Shared/Extensions/String+Cache.swift
T
2016-05-23 10:11:43 +02:00

52 lines
989 B
Swift

import Foundation
// MARK: - Cachable
/**
Implementation of Cachable protocol.
*/
extension String: Cachable {
public typealias CacheType = String
/**
Creates a string from NSData
- Parameter data: Data to decode from
- Returns: An optional CacheType
*/
public static func decode(data: NSData) -> CacheType? {
guard let string = String(data: data, encoding: NSUTF8StringEncoding) else {
return nil
}
return string
}
/**
Encodes a string to NSData
- Returns: Optional NSData
*/
public func encode() -> NSData? {
return dataUsingEncoding(NSUTF8StringEncoding)
}
}
// MARK: - Helpers
/**
Helper String extension.
*/
extension String {
/**
Creates base64 string
- Returns: A base64 encoded string
*/
func base64() -> String {
guard let data = self.dataUsingEncoding(NSUTF8StringEncoding) else { return self }
return data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
}
}