Post

Replies

Boosts

Views

Activity

Can't store Codable Size3D in SwiftData
I got a @Model class with a property of a codable "Options" struct. import SwiftData @Model final class Project { var options: Options } In this struct I have another property called "resolution" of type Size3D. import Spatial struct Options: Codable { var resolution: Size3D } I can initially launch the app and save a project with a custom resolution. Tho when I try to re-launch the app it crashes: Could not cast value of type 'Swift.Optional<Any>' (0x1fa251d80) to '__C.SPSize3D' (0x1047dcbc0). Size3D is codable, tho it does not seem to currently be supported by SwiftData. My current solution is to have a middle type that I store in my options struct like this: struct Options: Codable { private struct CodableResolution: Codable { let width: Double let height: Double let depth: Double var size: Size3D { Size3D(width: width, height: height, depth: depth) } init(_ size: Size3D) { self.width = size.width self.height = size.height self.depth = size.depth } } private var codableResolution: CodableResolution var resolution: Size3D { get { codableResolution.size } set { codableResolution = CodableResolution(newValue) } } } Note that I'm testing this on the visionOS simulator with Xcode 15.2 on macOS 14.0 Feedback: FB13543953
0
0
285
Jan ’24
How to subclass with SwiftData?
Hi, I want to subclass my model. Here is my superclass: import SwiftData @Model public class Control { @Attribute(.unique) public var frame: Frame init(frame: Frame) { self.frame = frame } } Here is my subclass: import SwiftData import CoreGraphics class SliderControl: Control { let axis: Axis var value: CGFloat = 0.0 init(axis: Axis, frame: Frame) { self.axis = axis super.init(frame: frame) } required public init(backingData: any BackingData<Control>, fromFetch: Bool = false) { // How to get `axis` and `value` from the backing data? } } I'm not sure how to use the backing data to re-create my object. My goal is to have multiple controls with unique properties.
5
4
3.7k
Jun ’23
How to create a Metal shader with layerEffect in SwiftUI
I'm trying to use the new layerEffect(_:maxSampleOffset:isEnabled:). https://developer.apple.com/documentation/swiftui/view/layereffect(_:maxsampleoffset:isenabled:) Tho I'm not sure how to define the metal shader function signature. The docs indicate that we should use SwiftUI::Layer, tho I'm not sure what to import to get access to this layer structure. [[ stitchable ]] half4 name(float2 position, SwiftUI::Layer layer, args...) My goal is to create a custom blur effect. Does anyone have any pointer on how to get started with layer effects?
1
0
1.1k
Jun ’23
Can't open package files on iPad in iCloud for document based app
I've got a multi-platform document based app, with package files (the "file" is a folder, but looks like a file for the user). I can create files on all platforms, tho I can only open files on Mac and iPhone. When I try to open files (in iCloud) on iPad, the file does not open and nothing is logged. Tho the files do open when they are stored locally on the iPad. I followed the documentation here: https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFBundles/DocumentPackages/DocumentPackages.html I've specified LSTypeIsPackage to true and added com.apple.package to UTTypeConformsTo. I've tried incrementing the build number. I'm targeting iOS 14 / macOS 11, and build with Xcode 13 beta 3. Here's my info.plist: &lt;plist version="1.0"&gt; &lt;dict&gt; &lt;key&gt;CFBundleDocumentTypes&lt;/key&gt; &lt;array&gt; &lt;dict&gt; &lt;key&gt;CFBundleTypeName&lt;/key&gt; &lt;string&gt;File Name&lt;/string&gt; &lt;key&gt;LSHandlerRank&lt;/key&gt; &lt;string&gt;Owner&lt;/string&gt; &lt;key&gt;LSItemContentTypes&lt;/key&gt; &lt;array&gt; &lt;string&gt;com.mysite.file&lt;/string&gt; &lt;/array&gt; &lt;key&gt;LSTypeIsPackage&lt;/key&gt; &lt;true/&gt; &lt;/dict&gt; &lt;/array&gt; &lt;key&gt;LSRequiresIPhoneOS&lt;/key&gt; &lt;true/&gt; &lt;key&gt;LSSupportsOpeningDocumentsInPlace&lt;/key&gt; &lt;true/&gt; &lt;key&gt;UISupportsDocumentBrowser&lt;/key&gt; &lt;true/&gt; &lt;key&gt;UIFileSharingEnabled&lt;/key&gt; &lt;true/&gt; &lt;key&gt;UTExportedTypeDeclarations&lt;/key&gt; &lt;array&gt; &lt;dict&gt; &lt;key&gt;UTTypeConformsTo&lt;/key&gt; &lt;array&gt; &lt;string&gt;com.apple.package&lt;/string&gt; &lt;string&gt;public.composite-content&lt;/string&gt; &lt;string&gt;public.data&lt;/string&gt; &lt;/array&gt; &lt;key&gt;UTTypeDescription&lt;/key&gt; &lt;string&gt;File Name&lt;/string&gt; &lt;key&gt;UTTypeIdentifier&lt;/key&gt; &lt;string&gt;com.mysite.file&lt;/string&gt; &lt;key&gt;UTTypeTagSpecification&lt;/key&gt; &lt;dict&gt; &lt;key&gt;public.filename-extension&lt;/key&gt; &lt;array&gt; &lt;string&gt;myext&lt;/string&gt; &lt;/array&gt; &lt;/dict&gt; &lt;/dict&gt; &lt;/array&gt; &lt;/dict&gt; &lt;/plist&gt; Does anyone know how to debug this?
1
0
1k
Aug ’21
Actor-Isolated Class with Different Actor Isolation
Hi I'm using this package in an app. The package builds fine. Tho I get build errors in the package when I build the app. Here's the error I get: Main actor-isolated class 'My Class' has different actor isolation from nonisolated superclass 'My Super Class' I'm not using actor or @MainActor, so I'm not sure where the error is coming from. Here's a line where the error shows up in the package. Related stackoverflow post.
1
0
1.9k
Jul ’21
Crash with OutlineGroup on macOS
I've got a Multiplatform SwiftUI app and I'm trying to create a hierarchical structure with OutlineGroup. My code works fine on iOS. Tho on macOS I get a crash. List { &#9;&#9;Section(header: Text("A")) { &#9;&#9;&#9;&#9;OutlineGroup(layers.aLayers, &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; children: \.children) { layer in &#9;&#9;&#9;&#9;&#9;&#9;LayerView(layer: layer, &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;selectedID: $layers.id) &#9;&#9;&#9;&#9;} &#9;&#9;} &#9;&#9;Section(header: Text("B")) { &#9;&#9;&#9;&#9;OutlineGroup(layers.bLayers, &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; children: \.children) { layer in &#9;&#9;&#9;&#9;&#9;&#9;LayerView(layer: layer, &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;selectedID: $layers.id) &#9;&#9;&#9;&#9;} &#9;&#9;} } .listStyle(SidebarListStyle()) Does anyone know what this means? Thread 1: Fatal error: Could not find child #1 for Optional(SwiftUI.OutlineItem(id: SwiftUI.ViewListID.Canonical(_index: 0, implicitID: 0, explicitID: nil), isGroupItem: true))
1
0
859
Aug ’20
Failed to Save in Photos Extension
I've start editing an image in Photos on macOS with my extension, and then when I try to save, I get an alert saying something went wrong when saving. func finishContentEditing(completionHandler:) In the finish content editing method I save my photo async like this: let output = PHContentEditingOutput(contentEditingInput: input) let unitCrop: UnitCrop = UnitCrop(frame: self.cropper.frame) do { &#9;&#9;let data: Data = try JSONEncoder().encode(unitCrop) &#9;&#9;output.adjustmentData = PHAdjustmentData(formatIdentifier: UnitCrop.formatIdentifier, &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; formatVersion: UnitCrop.formatVersion, &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; data: data) } catch { &#9;&#9;completionHandler(nil) &#9;&#9;return } self.cropper.save { result in &#9;&#9;switch result { &#9;&#9;case .success(let image): &#9;&#9;&#9;&#9;guard let imageData: Data = image.pngData() else { &#9;&#9;&#9;&#9;&#9;&#9;completionHandler(nil) &#9;&#9;&#9;&#9;&#9;&#9;return &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;do { &#9;&#9;&#9;&#9;&#9;&#9;try imageData.write(to: output.renderedContentURL, options: .atomic) &#9;&#9;&#9;&#9;&#9;&#9;print("success!") &#9;&#9;&#9;&#9;&#9;&#9;completionHandler(output) &#9;&#9;&#9;&#9;} catch { &#9;&#9;&#9;&#9;&#9;&#9;completionHandler(nil) &#9;&#9;&#9;&#9;&#9;&#9;return &#9;&#9;&#9;&#9;} &#9;&#9;case .failure(let error): &#9;&#9;&#9;&#9;completionHandler(nil) &#9;&#9;} } I see the success print, tho I still get an popup alert saying it failed in Photos and no edits gets saved. Do anyone know how to debug this?
2
0
918
Jul ’20
Image fails to open via Drag and Drop on iOS
I've got a SwiftUI app with the onDrop method on my View like this: .onDrop(of: [.image], delegate: myController) In an extension of my controller I conform to the DropDelegate and implement performDrop(info:) Here's my implementation: guard info.hasItemsConforming(to: [.image]) else { return false } let items: [NSItemProvider] = info.itemProviders(for: [.image]) guard let item: NSItemProvider = items.first else { return false } guard item.canLoadObject(ofClass: MPImage.self) else { return false } item.loadObject(ofClass: MPImage.self) { (reading, error) in guard error == nil else { return } guard let image: MPImage = reading as? MPImage else { return } self.didLoad(image: image) } The code compiles fine on iOS (tho not on macOS, see link) https://developer.apple.com/forums/thread/653934 So when I run the app on iOS, and drag an image from Photos and drop it on my app I get the following error: MyApp perform drop failed: Error Domain=NSItemProviderErrorDomain Code=-1000 "Cannot load representation of type public.jpeg" UserInfo={NSLocalizedDescription=Cannot load representation of type public.jpeg, NSUnderlyingError=0x283fa15f0 {Error Domain=NSCocoaErrorDomain Code=260 "The file “DAE533E7-2918-465E-9F3C-502B8DEC78BA.jpeg” couldn’t be opened because there is no such file." UserInfo={NSURL=file:///var/tmp/com.apple.DragUI.druid/.com.apple.DragUI.BDfAeP/DAE533E7-2918-465E-9F3C-502B8DEC78BA.jpeg, NSFilePath=/var/tmp/com.apple.DragUI.druid/.com.apple.DragUI.BDfAeP/DAE533E7-2918-465E-9F3C-502B8DEC78BA.jpeg, NSUnderlyingError=0x283f4b8a0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}}}
2
1
4.9k
Jul ’20
How to Drag and Drop Images in SwiftUI on macOS
I've got a Multiplatform app with a SwiftUI view and this function: .onDrop(of: [.image], delegate: myController) In an extension of my controller I conform to the DropDelegate and implement performDrop(info:) I've also typealias'ed the Image types. #if os(macOS) public typealias MPImage = NSImage #else public typealias MPImage = UIImage #endif Here's my implementation: guard info.hasItemsConforming(to: [.image]) else { return false } let items: [NSItemProvider] = info.itemProviders(for: [.image]) guard let item: NSItemProvider = items.first else { return false } guard item.canLoadObject(ofClass: MPImage.self) else { return false } item.loadObject(ofClass: MPImage.self) { (reading, error) in &#9;&#9;guard error == nil else { return } &#9;&#9;guard let image: MPImage = reading as? MPImage else { return } &#9;&#9;self.didLoad(image: image) } This compiles fine on iOS, tho on macOS I get the following errors: Instance method 'canLoadObject(ofClass:)' requires that 'MPImage' (aka 'NSImage') conform to 'ObjectiveCBridgeable' and Instance method 'loadObject(ofClass:completionHandler:)' requires that 'MPImage' (aka 'NSImage') conform to 'ObjectiveCBridgeable' Is this not the way to do drag and drop on macOS?
1
0
1.9k
Jul ’20
Package Loading - Failed to resolve dependencies
I've updated my Swift Package, PixelKit, to Swift 5.3. I tagged my commit with 1.1.1 and pushed to origin. Tho when trying to add it to another package, it failed to resolve dependencies: because PixelKit >=1.0.13 contains incompatible tools version and root depends on PixelKit 1.1.1..<2.0.0, version solving failed. Here's how I include it: .package(url: "https://github.com/hexagons/PixelKit.git", from: "1.1.1") I'm not sure what the old tag 1.0.13 has to do with this. I'm using the same swift tools version in both packages: // swift-tools-version:5.3
3
0
8.5k
Jun ’20
Swift Package with Metal
Hi, I've got a Swift Framework with a bunch of Metal files. Currently users have to manually include a Metal Lib in their bundle provided separately, to use the Swift Package. First question; Is there a way to make a Metal Lib target in a Swift Package, and just include the .metal files? (without a binary asset) Second question; If not, Swift 5.3 has resource support, how would you recommend to bundle a Metal Lib in a Swift Package?
10
1
7.5k
Jun ’20