Created
November 17, 2017 09:30
-
-
Save d-date/81982e69343b2ce976ced8b936093b3c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public struct Identifier { | |
fileprivate let _rawValue: RawValue | |
public typealias RawValue = Int | |
public init(_ rawValue: RawValue) { | |
self._rawValue = rawValue | |
} | |
public var rawValue: RawValue { | |
return _rawValue | |
} | |
} | |
extension Identifier: Equatable { | |
public static func == (lhs: Identifier, rhs: Identifier) -> Bool { | |
return lhs._rawValue == rhs._rawValue | |
} | |
} | |
extension Identifier: ExpressibleByIntegerLiteral { | |
public init(integerLiteral value: Int) { | |
self._rawValue = value | |
} | |
} | |
extension Identifier: CustomDebugStringConvertible { | |
public var debugDescription: String { | |
return "\(_rawValue)" | |
} | |
} | |
extension Identifier { | |
public static func array(forKey key: String, storage: UserDefaults = .standard) -> [Identifier] { | |
guard let array = storage.array(forKey: key) as? [RawValue] else { | |
return [] | |
} | |
return array.map({ Identifier($0) }) | |
} | |
public static func value(forKey key: String, storage: UserDefaults = .standard) -> Identifier? { | |
guard let value = storage.value(forKey: key) as? RawValue else { | |
return nil | |
} | |
return Identifier(value) | |
} | |
public func append(forKey key: String, storage: UserDefaults = .standard) { | |
var array = Identifier.array(forKey: key) | |
array.append(self) | |
array.save(forKey: key) | |
} | |
public func isContains(forKey key: String, storage: UserDefaults = .standard) -> Bool { | |
return Identifier.array(forKey: key).contains(self) | |
} | |
public func save(forKey key: String, storage: UserDefaults = .standard) { | |
storage.set(self._rawValue, forKey: key) | |
} | |
public func remove(fromKey key: String, storage: UserDefaults = .standard) { | |
Identifier.array(forKey: key) | |
.filter({ $0 != self }) | |
.save(forKey: key) | |
} | |
} | |
public extension Optional where Wrapped == Identifier { | |
public var rawValue: Identifier.RawValue? { | |
switch self { | |
case .some(let wrapped): | |
return wrapped._rawValue | |
case .none: | |
return nil | |
} | |
} | |
public func save(forKey key: String, storage: UserDefaults = .standard) { | |
storage.set(self, forKey: key) | |
} | |
} | |
public extension Array where Element == Identifier { | |
var rawValues: [Identifier.RawValue] { | |
return self.map({ $0._rawValue }) | |
} | |
public func save(forKey key: String, storage: UserDefaults = .standard) { | |
storage.set(self.rawValues.unique, forKey: key) //saveするときはrawValueを保存 | |
} | |
} | |
public extension Array where Element: Equatable { | |
var unique: [Element] { | |
return self.reduce(into: [], { | |
if !$0.contains($1) { $0.append($1) } | |
}) | |
} | |
} | |
/* Usage */ | |
//initialize identifier | |
let identifier = Identifier(1) | |
let identifierFromIntegerLiteral: Identifier = 2 | |
//get single value | |
let storedValue = Identifier.value(forKey: "storedValueKey") | |
//get list | |
var storedList = Identifier.array(forKey: "storedListKey") | |
// append element | |
storedList.append(newElement) | |
// remove element from stored list | |
storedValue.remove(fromKey: "storedListKey") | |
// check Identifier | |
//save | |
storedList.save(forKey: "storedListKey") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment