Post

Replies

Boosts

Views

Activity

How to edit local package referenced from package in XCode?
Situation I am working on two Swift packages, let's call them Applied and Base. Package Applied has dependency on a package Base. Both are stored on Github and the package Applied has dependecy specified as: .package(url: "https://github.com/.../Base", branch: "main"), When I work from command-line, I mark the Base package in the Applied package directory for edditing with: swift package edit --path ../Base Base This works as expected from the command-line. Problem XCode seems to be ignoring packages in edit mode. The article Organizing your code with local packages seems not to be applicable in this case, as it assumes that the Applied package is a XCode project or a workspace, which is not - it is just another plain Swift package. Both projects are plain Swift packages. What is the way to make XCode use the local package in edit mode when none of the packages is XCode Project/Workspace – they are just plain Swift packages? (using XCode beta 15.2)
0
0
486
Jan ’24
Error: Missing package product only in XCode
I am encountering a strange "Missing package product '(package name)' error that manifests only in XCode, but not in the command-line. I tried to isolate the problem into two new packages and it behaves the same with the new packages as it does with the original - I can replicate it. Having the following directory layout : SOME_ROOT/ MyTool/ Package.swift ... ProtoLib/ Package.swift Sources/ MyCore/ MyFlows/ ... The MyTool Package.swift looks like: // swift-tools-version: 5.9 import PackageDescription let package = Package( name: "MyTool", platforms: [.macOS("13"), .custom("linux", versionString: "1")], products: [ .library( name: "MyTool", targets: ["MyTool"]), ], dependencies: [ .package(path: "../ProtoLib"), ], targets: [ .target( name: "MyTool", dependencies: [ .product(name: "MyCore", package: "ProtoLib"), .product(name: "MyFlows", package: "ProtoLib"), ] ), .testTarget( name: "MyToolTests", dependencies: ["MyTool"]), ] ) The ProtoLib package: // swift-tools-version: 5.9 import PackageDescription let package = Package( name: "ProtoLib", platforms: [.macOS("13"), .custom("linux", versionString: "1")], products: [ .library( name: "MyCore", targets: ["MyCore"]), .library( name: "MyFlows", targets: ["MyFlows"]) ], targets: [ .target( name: "MyCore"), .target( name: "MyFlows", dependencies: ["MyCore"]) ] ) The ProtoLib has two libraries that are incubated for the time being under one package (multiple reasons, mostly convenience). When I build the project using command-line swift everything is fine, the project builds without any issues. When I try to build the MyTool project using XCode I am getting two errors: .../MyTool/Package.swift Missing package product 'MyCore' .../MyTool/Package.swift Missing package product 'MyFlows' What I am doing wrong? How to make the project build in XCode? Versions: XCode: Version 15.0 (15A240d) swift version (command-line): swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1) Target: arm64-apple-macosx13.0
1
0
2.2k
Sep ’23
JSONSerialization alternative (XCode 15.2 beta)
I need to read an arbitrary JSON file – structure is not known up-front. I am trying to use JSONSerialization from Foundation (system details at the end). The following code causes an error: let ser = JSONSerialization() The error: Thread 1: "*** +[NSJSONSerialization allocWithZone:]: Do not create instances of NSJSONSerialization in this release" I tried to have a look at the new Swift FoundationPreview package, however it does not seem to have JSONSerialization (as of today). It just has JSONCoder/JSONDecoder which require the values to be typed (Any can not be decodable). I would prefer to stay within official set of packages/libraries/frameworks. What is the (official) alternative to read an arbitrary JSON file? System details: OS: macOS Ventura – 13.4.1 (22F82) XCode Version 15.0 beta 2 (15A5161b)
1
0
574
Jun ’23
Inspector of multiple-object selection in SwiftUI
This is rather a convoluted problem, maybe a starter of a discussion for an application pattern. I am trying to create an application for editing dynamic objects (structure can not be expressed as a struct/class - therefore it is not known to the application developer), which can be simulated using a dictionary for now. There might be multiple collections (or filters) of such objects. In each collection an user can select one or multiple of the objects and inspect them using a third view: an inspector. Inspector can inspect and set properties of one or more objects. The distilled application idea with some concept mockups and more detailed information is here at Github. WARNING: The application DOES NOT WORK as intended, it is just there to demonstrate some issues. It might look a bit convoluted, but can not think of a simpler solution for given problem. Tried to extract the essentials. Either I have deep misunderstanding of SwiftUI (knowledge comes mostly from official docs), or there is something obscure going on. The application views are as follows: Window | +-- Content View (*>) | +-- Navigation View | | | +-- [sidebar] List of Thing Filters | | | +-- ThingListView(<*) – list of things, selectable, multiple | +-- Inspector(<*) – of selection in the above (*>) Selection – state, source of truth (<*) Selection - binding to "Visual" representation: Navigation Inspector |<----------------------->|<--------->| +----------+--------------+-----------+ | Filter 1 |**Thing 1*****| Selected: | | Filter 2 | Thing 2 | 1,3 | | .... |**Thing 3*****| | | | ... | Name: ___ | +----------+--------------+-----------+ ^ | +------- this can be 2D canvas, can be anything to present a collection of selectable objects. Note: The ThingListView might as well be some kind of Canvas, or any other view, it does not have to be just a list view. Issue 1 I am experiencing an issue where the binding set is called on no change in any of the the Inspector subviews. This has a negative side-effect on the model. Pick a Filter (Thing collection) Select multiple (more than one) items One of (any): Change selection to one item Switch to another Filter Result: All previously selected items will be changed. Expected: No change happens. What happened: set of a binding (see below) to a text filed is called on selection change, not on text field content change. The offending code location: Inspector.swift -> class Inspector -> var body -> nameBinding -> set. For the context the simplified listing is here. Note the name and nameBinding are just mockups, there might be more properties as well as different kinds of inspectors. struct Inspector: View {     @State var name: String = "" // ... more state is defined here ...     var body: some View {         let nameBinding = Binding<String>(             get: { // get one or coalesce multiple values of a dynamic property `name`             },             set: { text in // set the property in all selected objects             }         ) // ... view definition continues here } } Issue 2 This is minor, but noticed that the get of the binding is called very frequently. Question How to make SwitfUI to call set of a binding only when the value actually changed? Alternatively: if I did not get the solution approach right, what is the more paradigmatic way of solving the (Collections → Things) → Inspector application flow when the objects have dynamic properties (can not be expressed as structs/classes known to the application developer) and one can inspect a multi-object selection?
2
0
634
Nov ’21
TextEditor cells in SwiftUI Table
I need to have a proper, multi-line text editor in a table (SwiftUI Table). Requirement is not a typical spreadsheet, it is rather a table with cells of larger size, mostly for editing multi-line textual content. Expectation is a dozen or two of columns and potentially many rows. Some columns might contain single simple scalar values, those might be using for sorting, for example. Here is a simple (wrong) code snippet that demonstrates what I am trying to achieve at this moment: struct Thing: Identifiable {     var id: Int     var text: String     init(id: Int, text: String="") {         self.id = id         self.text = text     } } var data = [     Thing(id: 1, text: "one"),     Thing(id: 2, text: "two"),     Thing(id: 3, text: "three"), ] struct ContentView: View {     var body: some View {         VStack {             Text("Hello, world!")                 .padding()             Table(data) {                 TableColumn("ID") { row in Text(String(row.id)) }                 // Error: Cannot convert value of type 'String' to expected argument type 'Binding<String>'                 TableColumn("Text") { row in TextEditor(text: row.text) }             }         }     } } Question: What is the proper way of defining that the row.text is a binding in this case? Side note: I can not use key paths, since at the end the Thing will also be some dynamic structure not known at compile time - the structure will be editable by the user.
2
1
2.5k
Oct ’21