Swiftpack.co - benspratling4/WorkableSwiftUI as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by benspratling4.
benspratling4/WorkableSwiftUI 0.0.3
All the stuff you need to make SwiftUI actually usable
⭐️ 0
🕓 47 weeks ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/benspratling4/WorkableSwiftUI.git", from: "0.0.3")

WorkableSwiftUI

SwiftUI is awesome, in theory. In practice, it's another story. Here's what you need to make it actually usable.

Identified

So you changed your data model to structs, and now they dont have unique id's. But what if you need your type to have Identifiable conformance, but there's no 'id' in your serialized data model?

For instance, ever want to ForEach over an array of items which are uniquely identified by their position in the array? (and isn't that like the #1 thing you actually want to do with a ForEach?) Apple's all like "that's deprecated".

WorkableSwiftUI introduces Identified, a type which quietly inserts a unique stable id into the data model but won't need to seriliza it out, nor will it demand that it have been in the data model all along.

Now, of course, the auto-created id's aren't written out to disk... that's the whole point, so they aren't stable across serializations / deserializaitons (or app launches), but they are stable in ram.

Let's see an example, suppose you have a SampleStruct

struct SampleStruct : Codable {
	var name:String
	var height:Double
}

And your data model is

struct ContainerStruct : Codable {
	var samples:[SampleStruct]
}

Where are you going to insert the id's? They aren't in the json.

{
	"samples": [
		{
			"name":"Title"
			"height":13.976
		}
	]
}

The fields you have aren't unique id's. Perhaps someone could edit them and what forces themt o be unique in the array? Are you going to write awful manual serialization / deserialization code to insert them and then avoid them when serializing? yuck. Serialization / deserialization ought to be codeless nowadays.

Just use Identified instead.

struct ContainerStruct : Codable {
	var samples:[Identified<SampleStruct>]
}

It provides conditional Encodable and Decodable conformances which don't require any changes to your file format, but do provide the unique id's when in ram (using UUID).

It also allows you to work with the array as though it is just a [SampleStruct] in many cases.

GitHub

link
Stars: 0
Last commit: 47 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

Initial release
2 years ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics