I have the exact same question! I've tried both in Objective-C and Swift (XCode 11.6, macOS SDK 10.15)
Apple is trying to move developers off of old APIs. Old API? Documentation for NSSavePanel:runModal doesn't warn about undesirable use or possible future depreciation. And neither the warnings. So using it seems legitimate.
Use a modern API. Look in the documentation for runModel. Are there any new methods listed near it that use completion handlers? I've tried beginWithCompletionHandler, and it does work without warning. But it is not modal, so main application window still accessible, which is not wanted.
I've tried beginSheetModalForWindow:completionHandler, but i can't get it to work.
In my MainViewController.m file, in an IBAction for a button:
NSSavePanel *savePanel = [NSSavePanel savePanel];
[savePanel beginSheetModalForWindow:[self.view window] completionHandler:^(NSModalResponse result){
		if (result == NSModalResponseOK) { ... }
		...
}];
But i've got this first:
[General] -[NSNib _initWithNibNamed:bundle:options:] could not load the nibName: MainViewController in bundle (null).
[General] (
0	 CoreFoundation											0x00007fff31307b57 __exceptionPreprocess + 250
1	 libobjc.A.dylib										 0x00007fff6a14e5bf objc_exception_throw + 48
2	 CoreFoundation											0x00007fff313079b5 +[NSException raise:format:] + 189
3	 AppKit															0x00007fff2e55f18d -[NSNib _initWithNibNamed:bundle:options:] + 607
4	 AppKit															0x00007fff2e55eee7 -[NSViewController _nibWithName:bundle:] + 174
5	 AppKit															0x00007fff2e55eaf1 -[NSViewController loadView] + 134
6	 AppKit															0x00007fff2e55e90b -[NSViewController _loadViewIfRequired] + 72
7	 AppKit															0x00007fff2e55e888 -[NSViewController view] + 23
8	 TestSavePanel01										 0x0000000100001c89 -[MainViewController ButtonPressed:] + 425
(...)
I've made some research and found some informations, so for View Controller object, inside the Attribute inspector, i've added my XIB name inside "Nib name" (MainMenu). but then i get:
[Nib Loading] Failed to connect (delegate) outlet from (MainViewController) to (AppDelegate): missing setter or instance variable
[General] -[MainViewController loadView] loaded the "MainMenu" nib but no view was set.
[General] (
0	 CoreFoundation											0x00007fff31307b57 __exceptionPreprocess + 250
1	 libobjc.A.dylib										 0x00007fff6a14e5bf objc_exception_throw + 48
2	 CoreFoundation											0x00007fff313079b5 +[NSException raise:format:] + 189
3	 AppKit															0x00007fff2e55ed4f -[NSViewController loadView] + 740
4	 AppKit															0x00007fff2e55e90b -[NSViewController _loadViewIfRequired] + 72
5	 AppKit															0x00007fff2e55e888 -[NSViewController view] + 23
6	 TestSavePanel01										 0x0000000100001c89 -[MainViewController ButtonPressed:] + 425
(...)
I've not been able to get any further.
So some more precise explanations, suggestions and maybe even a code sample would be very welcome!
I'm a complete newbie in macOS development, and Apple really doesn't help a lot, providing a documentation that looks like much like a catalog or dictionnary than a real documentation with code examples, like ones can find in other environment/langages.
Thanks in advance.
Post
Replies
Boosts
Views
Activity
Hi,
I was dealing with a similar problem than the OP, and wanted to test the proposed Timer based concrete example.
So i've created a new Swift/SwiftUI project, added a Client.swift file with code, but as soon as i try to add the following code in ContentView.swift:
@ObservedObject
var client: Client
i get a "Failed to build scheme" error in UI preview, and a "Missing argument for parameter 'client' in call" error located at the
ContentView()
line near the end.
I've managed to silence these errors with this modification:
@ObservedObject
var client = Client()
The code then seems to work has expected.
I also tried to use the original syntax, and to add a (maybe missing) init block below, but did not managed to get rid of the errors.
Sorry if this is a dumb question, but could somebody clarify what the problem is with the original code (maybe some change with newer Swift version?), and what are the solutions to correct it (and if my correction if correct)?
Xcode 14.0.1/Swift 5.7
Thanks!
Additional question: is there a reason to specify "self" in
Button(action: { self.client.start() }, label: { Text("Start") } )
.disabled(!client.state.canStart)
It seems to work also without it.
Thanks a lot for your complete and precise reply!
These are more Swift and SwiftUI questions than networking questions.
Right, but since i was trying to apply the example code provided in this thread, i thought it would be better to reply here. Sorry if it was a bad choice.
Ok! That makes sense for me now. I thought that the provided code samples were complete and usable "as is", but that was a bad assumption from me.
As a matter of style, I use self everywhere
Ok. I was pretty sure it doesn't hurt to have it or not, but wanted to be sure that i didn't miss something... like i did for the previous question!
I'm just starting at Swift development, and was also not very aware of object oriented development in C/C++, so i have a lot to learn here, and since Apple documentation is quite light on code examples, it's easy to get lost or confused, or to miss something obvious.
Thanks for your precious help. I will be able to continue to try to code my first macOS swift/swiftUI app to send commands to a serial device connected through an serial to IP converter (so using TCP connection)...