Post

Replies

Boosts

Views

Activity

Reply to Why would you use @State at the application level, or anywhere outside SwiftUI?
The answers you are looking for are linked from the documentation you provided a link to, here: https://developer.apple.com/documentation/swiftui/migrating-from-the-observable-object-protocol-to-the-observable-macro?language=objc The Observable macro is new and most of the Internet hasn't caught up yet. If you can use Observable, you can use @State instead of @StateObject, even for classes and structs. The library lives outside the App struct. Lexically, it appears to be inside the struct, but SwiftUI manages its lifetime because of that @State macro. You can put a breakpoint inside Library's init to see when it called.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’23
Reply to Build failed on Xcode 15
how long is it since you last built or linked your project? Is it an app, a library or a framework? What version of Xcode and did it last succeed on? It sounds like a tool has changed, and what used to look like a flag now looks like a file name to the build tools.
Dec ’23
Reply to What are command line tools?
use your favorite search engine to look for "what is in mac command line tools", and you might find this link, for example, which will tell you more https://mac.install.guide/commandlinetools/index.html You ask is it safe to install the tools - yes. Is it safe to use the tools - well, it depends on what you do with them. Why are they so large - they do a lot more than set the visibility attribute of files. The SetFile tool itself is 168KB. hope this helps, good luck
Topic: App & System Services SubTopic: Drivers Tags:
Dec ’23
Reply to SwiftUI - Infinite Loop - onDisappear without "."
with the dot, you're calling a function called onDisappear on the result of the function .onAppear. without the dot, you're calling a function called onDisappear on the view enclosing the one you are applying modifiers to. The latter is probably never what you want to do, but the compiler doesn't know that. All it knows is that the code compiles. I think that the Swift compiler should take a look at what you write, and question the intent (even if the code compiles). There are rumors that Apple is doing big things with AI, maybe improved code review in Xcode could be one of those things? You can use Feedback Assistant to request such improvements.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’23
Reply to What does this error mean when using .searchable()
In a template app running on a simulated iPhone 15, I don't see the error you describe. There must be more to your code than what you show in your example Content(), or it only shows up on a real device, or it only occurs on something other than iOS? I'm using Xcode 15.1 on macOS 14.1.2, simulating iOS 17.2 Did you try setting the CG_NUMERICS_SHOW_BACKTRACE environment variable, and if so, did it give you any further insight?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’23
Reply to How to prevent singleton base class getting re-initialised
I can reproduce your problem. Thank you for posting all your code, which made this possible. BLEManager is supposed to a singleton, right? So you want exactly one of these in your program. To ensure this, you should make the initializer private and access the BLEManager only through shared. But in the ContentView, you create a BLEManager without going through shared, so now you have two - one created in the App's body, and one created by your ContentView. If I make the BLEManager private, the compiler complains about the initializer for DeviceModel - "'super.init' isn't called on all paths before returning from initializer". Which is fair enough, but I'm not allowed to call super.init() - I made it private to ensure the all accesses to the singleton BLEManager go through its shared accessor. I'm puzzled as to why a DeviceModel is a BLEManager. Just the names tell me that they are different objects; a Device may need access to a BLEManager, but it shouldn't be a BLEManager. Also, you expect an array of DeviceModels - how does that work if they are all the same object (BLEManager.shared?). It can work if they all use the same object.
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’23
Reply to Understanding the swift language and its documentation
Hi. I'm glad you asked this because I'm constantly tripping over SwiftUI syntax myself. There are a bunch of ways to express the contents of your NavigationSplitView. Note that SwiftUI is not Swift. It looks like Swift, it is written in Swift, but it is a domain specific language for creating descriptions of user interfaces. It extends the Swift language. If you look at the documentation for NavigationSplitView in Xcode's Documentation viewer, you'll find struct NavigationSplitView<Sidebar, Content, Detail> where Sidebar : View, Content : View, Detail: View and if you click on View, you'll find that it is a protocol. The initializer you are using is the one which creates a two-column view, with no control over the visibility of the columns public init(@ViewBuilder sidebar: () -> Sidebar, @ViewBuilder detail: () -> Detail) where Content == EmptyView The parameters (labelled sidebar and detail here) are closures - functions returning structs conforming to the View protocol. my SidebarView and DetailView are Views defined like this struct SidebarView: View { var body: some View { Text("sidebar") } } struct DetailView: View { var body: some View { Text("detail") } } And here are functions which return these structs func DetailViewFunc() -> DetailView { return DetailView() } func SidebarViewFunc() -> SidebarView { return SidebarView() } we can use these functions as parameter values: struct ContentView: View { var body: some View { NavigationSplitView ( sidebar: SidebarViewFunc , detail: DetailViewFunc) } } that's a bit verbose, but it does look like a regular function call with a parameter list. More succinctly, you can just write the functions directly in the parameter list, without names. You can omit the return keyword because of the use of @ViewBuilder in the declaration of the NavigationSplitView initializer. You don't need SidebarViewFunc or DetailViewFunc any more. Usually, separate closures go on separate lines: struct ContentView: View { var body: some View { NavigationSplitView ( sidebar: { SidebarView() }, detail: { DetailView() } ) } } This works fine, but most code examples won't look like this. The Swift developers were very proud of how much typing you don't have to do. If the last parameter to a function is a closure, you can omit its name and move it outside the parentheses: struct ContentView: View { var body: some View { NavigationSplitView ( sidebar: { SidebarView() } ) { DetailView() } } } this works too. However SwiftUI does quite a lot with closures, often views take multiple closure parameters, so some people thought it would be cool to extend this syntax to multiple trailing closures. See https://github.com/apple/swift-evolution/blob/main/proposals/0279-multiple-trailing-closures.md for more context. The rule here is that the first trailing closure can be unnamed, while the subsequent ones must be named. Which has the effect here of removing the visible "sidebar" label and reinstating the "detail" label. But because all the parameters are closures and were moved out of the parentheses, we can omit the parentheses, grounds for rejoicing in some circles. So now the ContentView can look like this: struct ContentView: View { var body: some View { NavigationSplitView { SidebarView() } detail: { DetailView() } } } Xcode's code completion suggestions suggest the explicitly labelled parameter version, not the multiple trailing closure syntax version, which is unhelpful. And if you try to translate from Xcode's suggestion to trailing closure syntax, and forget to delete a comma, you'll see five compiler errors, none of which tell you that you have a comma which shouldn't be there. struct ContentView: View { var body: some View { NavigationSplitView { SidebarView() }, // added (and unnecessary) comma detail: { DetailView() } } } if that bothers you, I'd encourage you to file a bug. Swift error reporting has improved since the beginning, but the language has also become more and more complex. Stick at it. Eventually you'll get used to it, and know what to look for when Swift kicks out an error you cannot understand.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’23
Reply to How to access a directly attached UVC camera with AVPlayer?
take a look at the AVCam sample code. It is for iOS, but most of it translates pretty well to macOS. You build a AVCaptureSession using your AVCaptureDevice, and set up a AVCapturePreviewLayer so you can see what it is producing. There is also older sample code kicking around which is more Mac-oriented, and some third-party samples on GitHub. Try searching for "macOS AVCaptureSession sample"
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’23
Reply to Control HID USB Device from iPad?
it seems that HIDDriverKit isn't available on iPadOS. But you can write your own code to detect your custom device (using USBDriverKit) and your own routines to read and write reports, which are usually pretty simple control pipe writes and interrupt pipe reads. If your device isn't horrendously complicated, this is easier than using EA, and you don't need to deal with MFi licensing.
Topic: App & System Services SubTopic: Drivers Tags:
Dec ’23
Reply to is XPC from app to CMIOExtension possible?
@cullenp you asked "how do I access the CMIO extension in the first place" From an app, use the routines in CoreMediaIO/CMIOHardwareObject.h to iterate through the CMIO objects, starting at kCMIOObjectSystemObject until you find an object with kCMIODevicePropertyDeviceUID with a value equal to the value of the deviceID parameter used in your initializer of your CMIOExtensionDevice. You also asked "Is it possible to supply the Extension provider source with my queue? " I'm not sure what you're asking here. You find the sink stream's ID by querying the sink device you found using the code above. Using that sink stream ID, you can copy its queue using CMIOStreamCopyBufferQueue, and you use CMSimpleQueueEnqueue to put your samples onto the sink stream's queue.
Topic: App & System Services SubTopic: Drivers Tags:
Dec ’23