Post

Replies

Boosts

Views

Activity

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?
12
1
9.7k
Mar ’25
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
461
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
5.0k
Oct ’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.3k
Jun ’23
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
2.2k
Apr ’22
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?
Replies
12
Boosts
1
Views
9.7k
Activity
Mar ’25
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
Replies
0
Boosts
0
Views
461
Activity
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.
Replies
5
Boosts
4
Views
5.0k
Activity
Oct ’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?
Replies
1
Boosts
0
Views
1.3k
Activity
Jun ’23
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?
Replies
1
Boosts
0
Views
2.2k
Activity
Apr ’22