-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
130 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
version: 1 | ||
builder: | ||
configs: | ||
- documentation_targets: [spm-template] | ||
- documentation_targets: [iCloudStorage] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// swift-tools-version: 5.8.0 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "iCloudStorage", | ||
platforms: [ | ||
.iOS(.v15), | ||
.macOS(.v12), | ||
.watchOS(.v8), | ||
.tvOS(.v15), | ||
.visionOs(.v1) | ||
], | ||
products: [ | ||
// Products define the executables and libraries a package produces, making them visible to other packages. | ||
.library( | ||
name: "iCloudStorage", | ||
targets: ["iCloudStorage"] | ||
) | ||
], | ||
targets: [ | ||
// Targets are the basic building blocks of a package, defining a module or a test suite. | ||
// Targets can depend on other targets in this package and products from dependencies. | ||
.target( | ||
name: "iCloudStorage"), | ||
.testTarget( | ||
name: "iCloudStorageTests", | ||
dependencies: ["iCloudStorage"] | ||
) | ||
] | ||
) |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// iCloudStorage.swift | ||
// iCloudStorage | ||
// | ||
// Created by Wesley de Groot on 2025-01-24. | ||
// https://wesleydegroot.nl | ||
// | ||
// https://github.com/0xWDG/iCloudStorage | ||
// MIT License | ||
// | ||
|
||
#if canImport(SwiftUI) | ||
import Foundation | ||
import SwiftUI | ||
|
||
/// A property wrapper that reads and writes to iCloud. | ||
/// | ||
/// Example: | ||
/// ``` | ||
/// @iCloudStorage("key") var value: String = "default" | ||
/// ``` | ||
@propertyWrapper | ||
public struct iCloudStorage<T>: DynamicProperty { | ||
// swiftlint:disable:previous type_name | ||
/// The key to read and write to. | ||
let key: String | ||
|
||
/// The default value to use if the value is not set yet. | ||
let defaultValue: T | ||
|
||
/// Creates an `iCloudStorage` property. | ||
/// | ||
/// - Parameter wrappedValue: The default value. | ||
/// - Parameter key: The key to read and write to. | ||
public init(wrappedValue: T, _ key: String) { | ||
self.key = key | ||
self.defaultValue = wrappedValue | ||
} | ||
|
||
/// The value of the key in iCloud. | ||
public var wrappedValue: T { | ||
get { | ||
return NSUbiquitousKeyValueStore.default.object(forKey: key) as? T ?? defaultValue | ||
} | ||
// This needs to be nonmutating because we're setting a property on a struct. | ||
nonmutating set { | ||
NSUbiquitousKeyValueStore.default.set(newValue, forKey: key) | ||
} | ||
} | ||
|
||
/// A binding to the value of the key in iCloud. | ||
public var projectedValue: Binding<T> { | ||
Binding { | ||
return self.wrappedValue | ||
} set: { newValue in | ||
self.wrappedValue = newValue | ||
} | ||
} | ||
} | ||
#endif |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// iCloudStorageTests.swift | ||
// iCloudStorage | ||
// | ||
// Created by Wesley de Groot on 2025-01-24. | ||
// https://wesleydegroot.nl | ||
// | ||
// https://github.com/0xWDG/iCloudStorage | ||
// MIT License | ||
// | ||
import Testing | ||
@testable import iCloudStorage | ||
|
||
@Test func example() async throws { | ||
// Write your test here and use APIs like `#expect(...)` to check expected conditions. | ||
} |