Fix availability checks for CocoaPods

This commit is contained in:
Sindre Sorhus 2019-10-30 19:47:54 +07:00
parent 9ce53eee2e
commit a82d6728e7
3 changed files with 27 additions and 27 deletions

View File

@ -5,12 +5,12 @@ public final class Defaults {
public class Keys { public class Keys {
public typealias Key = Defaults.Key public typealias Key = Defaults.Key
@available(iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
public typealias NSSecureCodingKey = Defaults.NSSecureCodingKey public typealias NSSecureCodingKey = Defaults.NSSecureCodingKey
public typealias OptionalKey = Defaults.OptionalKey public typealias OptionalKey = Defaults.OptionalKey
@available(iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
public typealias NSSecureCodingOptionalKey = Defaults.NSSecureCodingOptionalKey public typealias NSSecureCodingOptionalKey = Defaults.NSSecureCodingOptionalKey
fileprivate init() {} fileprivate init() {}
@ -38,7 +38,7 @@ public final class Defaults {
} }
} }
@available(iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
public final class NSSecureCodingKey<T: NSSecureCoding>: Keys { public final class NSSecureCodingKey<T: NSSecureCoding>: Keys {
public let name: String public let name: String
public let defaultValue: T public let defaultValue: T
@ -72,7 +72,7 @@ public final class Defaults {
} }
} }
@available(iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
public final class NSSecureCodingOptionalKey<T: NSSecureCoding>: Keys { public final class NSSecureCodingOptionalKey<T: NSSecureCoding>: Keys {
public let name: String public let name: String
public let suite: UserDefaults public let suite: UserDefaults
@ -95,7 +95,7 @@ public final class Defaults {
} }
/// Access a defaults value using a `Defaults.NSSecureCodingKey`. /// Access a defaults value using a `Defaults.NSSecureCodingKey`.
@available(iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
public static subscript<T: NSSecureCoding>(key: NSSecureCodingKey<T>) -> T { public static subscript<T: NSSecureCoding>(key: NSSecureCodingKey<T>) -> T {
get { key.suite[key] } get { key.suite[key] }
set { set {
@ -112,7 +112,7 @@ public final class Defaults {
} }
/// Access a defaults value using a `Defaults.OptionalKey`. /// Access a defaults value using a `Defaults.OptionalKey`.
@available(iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
public static subscript<T: NSSecureCoding>(key: NSSecureCodingOptionalKey<T>) -> T? { public static subscript<T: NSSecureCoding>(key: NSSecureCodingOptionalKey<T>) -> T? {
get { key.suite[key] } get { key.suite[key] }
set { set {
@ -150,7 +150,7 @@ public final class Defaults {
- Parameter keys: Keys to reset. - Parameter keys: Keys to reset.
- Parameter suite: `UserDefaults` suite. - Parameter suite: `UserDefaults` suite.
*/ */
@available(iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
public static func reset<T: NSSecureCoding>(_ keys: NSSecureCodingKey<T>..., suite: UserDefaults = .standard) { public static func reset<T: NSSecureCoding>(_ keys: NSSecureCodingKey<T>..., suite: UserDefaults = .standard) {
reset(keys, suite: suite) reset(keys, suite: suite)
} }
@ -187,7 +187,7 @@ public final class Defaults {
- Parameter keys: Keys to reset. - Parameter keys: Keys to reset.
- Parameter suite: `UserDefaults` suite. - Parameter suite: `UserDefaults` suite.
*/ */
@available(iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
public static func reset<T: NSSecureCoding>(_ keys: [NSSecureCodingKey<T>], suite: UserDefaults = .standard) { public static func reset<T: NSSecureCoding>(_ keys: [NSSecureCodingKey<T>], suite: UserDefaults = .standard) {
for key in keys { for key in keys {
key.suite[key] = key.defaultValue key.suite[key] = key.defaultValue
@ -224,7 +224,7 @@ public final class Defaults {
- Parameter suite: `UserDefaults` suite. - Parameter suite: `UserDefaults` suite.
``` ```
*/ */
@available(iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
public static func reset<T: NSSecureCoding>(_ keys: NSSecureCodingOptionalKey<T>..., suite: UserDefaults = .standard) { public static func reset<T: NSSecureCoding>(_ keys: NSSecureCodingOptionalKey<T>..., suite: UserDefaults = .standard) {
reset(keys, suite: suite) reset(keys, suite: suite)
} }
@ -260,7 +260,7 @@ public final class Defaults {
- Parameter keys: Keys to reset. - Parameter keys: Keys to reset.
- Parameter suite: `UserDefaults` suite. - Parameter suite: `UserDefaults` suite.
*/ */
@available(iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
public static func reset<T: NSSecureCoding>(_ keys: [NSSecureCodingOptionalKey<T>], suite: UserDefaults = .standard) { public static func reset<T: NSSecureCoding>(_ keys: [NSSecureCodingOptionalKey<T>], suite: UserDefaults = .standard) {
for key in keys { for key in keys {
key.suite[key] = nil key.suite[key] = nil
@ -299,7 +299,7 @@ extension UserDefaults {
return nil return nil
} }
@available(iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
private func _get<T: NSSecureCoding>(_ key: String) -> T? { private func _get<T: NSSecureCoding>(_ key: String) -> T? {
if UserDefaults.isNativelySupportedType(T.self) { if UserDefaults.isNativelySupportedType(T.self) {
return object(forKey: key) as? T return object(forKey: key) as? T
@ -342,7 +342,7 @@ extension UserDefaults {
set(_encode(value), forKey: key) set(_encode(value), forKey: key)
} }
@available(iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
private func _set<T: NSSecureCoding>(_ key: String, to value: T) { private func _set<T: NSSecureCoding>(_ key: String, to value: T) {
if UserDefaults.isNativelySupportedType(T.self) { if UserDefaults.isNativelySupportedType(T.self) {
set(value, forKey: key) set(value, forKey: key)
@ -359,7 +359,7 @@ extension UserDefaults {
} }
} }
@available(iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
public subscript<T: NSSecureCoding>(key: Defaults.NSSecureCodingKey<T>) -> T { public subscript<T: NSSecureCoding>(key: Defaults.NSSecureCodingKey<T>) -> T {
get { _get(key.name) ?? key.defaultValue } get { _get(key.name) ?? key.defaultValue }
set { set {
@ -379,7 +379,7 @@ extension UserDefaults {
} }
} }
@available(iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
public subscript<T: NSSecureCoding>(key: Defaults.NSSecureCodingOptionalKey<T>) -> T? { public subscript<T: NSSecureCoding>(key: Defaults.NSSecureCodingOptionalKey<T>) -> T? {
get { _get(key.name) } get { _get(key.name) }
set { set {

View File

@ -43,7 +43,7 @@ extension Defaults {
return [T].init(jsonString: "\([value])")?.first return [T].init(jsonString: "\([value])")?.first
} }
@available(iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
private static func deserialize<T: NSSecureCoding>(_ value: Any?, to type: T.Type) -> T? { private static func deserialize<T: NSSecureCoding>(_ value: Any?, to type: T.Type) -> T? {
guard guard
let value = value, let value = value,
@ -96,7 +96,7 @@ extension Defaults {
} }
} }
@available(iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
public struct NSSecureCodingKeyChange<T: NSSecureCoding> { public struct NSSecureCodingKeyChange<T: NSSecureCoding> {
public let kind: NSKeyValueChange public let kind: NSKeyValueChange
public let indexes: IndexSet? public let indexes: IndexSet?
@ -129,7 +129,7 @@ extension Defaults {
} }
} }
@available(iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
public struct NSSecureCodingOptionalKeyChange<T: NSSecureCoding> { public struct NSSecureCodingOptionalKeyChange<T: NSSecureCoding> {
public let kind: NSKeyValueChange public let kind: NSKeyValueChange
public let indexes: IndexSet? public let indexes: IndexSet?
@ -240,7 +240,7 @@ extension Defaults {
/** /**
Observe a defaults key. Observe a defaults key.
*/ */
@available(iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
public static func observe<T: NSSecureCoding>( public static func observe<T: NSSecureCoding>(
_ key: Defaults.NSSecureCodingKey<T>, _ key: Defaults.NSSecureCodingKey<T>,
options: NSKeyValueObservingOptions = [.initial, .old, .new], options: NSKeyValueObservingOptions = [.initial, .old, .new],
@ -286,7 +286,7 @@ extension Defaults {
/** /**
Observe an optional defaults key. Observe an optional defaults key.
*/ */
@available(iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
public static func observe<T: NSSecureCoding>( public static func observe<T: NSSecureCoding>(
_ key: Defaults.NSSecureCodingOptionalKey<T>, _ key: Defaults.NSSecureCodingOptionalKey<T>,
options: NSKeyValueObservingOptions = [.initial, .old, .new], options: NSKeyValueObservingOptions = [.initial, .old, .new],

View File

@ -14,7 +14,7 @@ enum FixtureEnum: String, Codable {
let fixtureDate = Date() let fixtureDate = Date()
@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
final class ExamplePersistentHistory: NSPersistentHistoryToken { final class ExamplePersistentHistory: NSPersistentHistoryToken {
let value: String let value: String
@ -46,9 +46,9 @@ extension Defaults.Keys {
static let date = Key<Date>("date", default: fixtureDate) static let date = Key<Date>("date", default: fixtureDate)
// NSSecureCoding // NSSecureCoding
@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
static let persistentHistoryValue = ExamplePersistentHistory(value: "ExampleToken") static let persistentHistoryValue = ExamplePersistentHistory(value: "ExampleToken")
@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
static let persistentHistory = NSSecureCodingKey<ExamplePersistentHistory>("persistentHistory", default: persistentHistoryValue) static let persistentHistory = NSSecureCodingKey<ExamplePersistentHistory>("persistentHistory", default: persistentHistoryValue)
} }
@ -106,7 +106,7 @@ final class DefaultsTests: XCTestCase {
XCTAssertTrue(Defaults[.key]) XCTAssertTrue(Defaults[.key])
} }
@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
func testNSSecureCodingKeys() { func testNSSecureCodingKeys() {
XCTAssertEqual(Defaults.Keys.persistentHistoryValue.value, Defaults[.persistentHistory].value) XCTAssertEqual(Defaults.Keys.persistentHistoryValue.value, Defaults[.persistentHistory].value)
let newPersistentHistory = ExamplePersistentHistory(value: "NewValue") let newPersistentHistory = ExamplePersistentHistory(value: "NewValue")
@ -182,7 +182,7 @@ final class DefaultsTests: XCTestCase {
waitForExpectations(timeout: 10) waitForExpectations(timeout: 10)
} }
@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
func testObserveNSSecureCodingKey() { func testObserveNSSecureCodingKey() {
let key = Defaults.NSSecureCodingKey<ExamplePersistentHistory>("observeNSSecureCodingKey", default: ExamplePersistentHistory(value: "TestValue")) let key = Defaults.NSSecureCodingKey<ExamplePersistentHistory>("observeNSSecureCodingKey", default: ExamplePersistentHistory(value: "TestValue"))
let expect = expectation(description: "Observation closure being called") let expect = expectation(description: "Observation closure being called")
@ -217,7 +217,7 @@ final class DefaultsTests: XCTestCase {
waitForExpectations(timeout: 10) waitForExpectations(timeout: 10)
} }
@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *) @available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *)
func testObserveNSSecureCodingOptionalKey() { func testObserveNSSecureCodingOptionalKey() {
let key = Defaults.NSSecureCodingOptionalKey<ExamplePersistentHistory>("observeNSSecureCodingOptionalKey") let key = Defaults.NSSecureCodingOptionalKey<ExamplePersistentHistory>("observeNSSecureCodingOptionalKey")
let expect = expectation(description: "Observation closure being called") let expect = expectation(description: "Observation closure being called")
@ -286,7 +286,7 @@ final class DefaultsTests: XCTestCase {
XCTAssertEqual(Defaults[key1], defaultString1) XCTAssertEqual(Defaults[key1], defaultString1)
XCTAssertEqual(Defaults[key2], newString2) XCTAssertEqual(Defaults[key2], newString2)
if #available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *) { if #available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) {
let key3 = Defaults.NSSecureCodingKey<ExamplePersistentHistory>("key3", default: ExamplePersistentHistory(value: defaultString3)) let key3 = Defaults.NSSecureCodingKey<ExamplePersistentHistory>("key3", default: ExamplePersistentHistory(value: defaultString3))
Defaults[key3] = ExamplePersistentHistory(value: newString3) Defaults[key3] = ExamplePersistentHistory(value: newString3)
Defaults.reset(key3) Defaults.reset(key3)
@ -326,7 +326,7 @@ final class DefaultsTests: XCTestCase {
XCTAssertEqual(Defaults[key1], nil) XCTAssertEqual(Defaults[key1], nil)
XCTAssertEqual(Defaults[key2], newString2) XCTAssertEqual(Defaults[key2], newString2)
if #available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *) { if #available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, iOSApplicationExtension 11.0, macOSApplicationExtension 10.13, tvOSApplicationExtension 11.0, watchOSApplicationExtension 4.0, *) {
let key3 = Defaults.NSSecureCodingOptionalKey<ExamplePersistentHistory>("optionalKey3") let key3 = Defaults.NSSecureCodingOptionalKey<ExamplePersistentHistory>("optionalKey3")
Defaults[key3] = ExamplePersistentHistory(value: newString3) Defaults[key3] = ExamplePersistentHistory(value: newString3)
Defaults.reset(key3) Defaults.reset(key3)