Comments (9)
So far I haven't come across too many APIs that take an NSArray that will be written to, but I suspect that pattern is not working here because of the copying going on for converting from slices. If you were to use objc.Call
to call InstantiateWithOwnerTopLevelObjects
passing a constructed NSArray (maybe foundation.Array_Array()
) it might work and I could look into how we might handle that in generated bindings.
from macdriver.
Your suggestion worked! I saw the correct output for the first time.
Here is the updated program with your suggestion implemented:
// File: main.go
// Date: 11/29/23
// Description: Uses a NIB file to display the interface
// Run directions: go run main.go
package main
import (
"fmt"
"os"
"github.com/progrium/macdriver/macos/appkit"
f "github.com/progrium/macdriver/macos/foundation"
"github.com/progrium/macdriver/objc"
)
func main() {
// Setup the application
app := appkit.Application_SharedApplication()
app.SetActivationPolicy(appkit.ApplicationActivationPolicyRegular)
app.ActivateIgnoringOtherApps(true)
// Get the NIB file's data
godata, err := os.ReadFile("MainMenu.nib")
if err != nil {
fmt.Println("Failed to load nib file:", err)
return
}
fmt.Println("godata length:", len(godata))
// How the Nib would be obtained in Objective-C.
// Can't use this method because InstantiateWithOwnerTopLevelObjects
// does not accept (NS)Data object.
nsdata := f.Data_DataWithContentsOfFile("MainMenu.nib")
fmt.Println("nsdata length:", nsdata.Length())
myNib := appkit.NewNibWithNibDataBundle(godata, nil)
myObjects := f.Array_Array()
fmt.Printf("address of myObjects before: %p\n", &myObjects)
//status := myNib.InstantiateWithOwnerTopLevelObjects(nil, myObjects)
status := objc.Call[bool](myNib, objc.Sel("instantiateWithOwner:topLevelObjects:"), nil, &myObjects)
if status == false {
fmt.Println("Error: failed to instantiate nib file")
return
}
fmt.Printf("address of myObjects after: %p\n", &myObjects)
fmt.Println("successful NIB instantiation:", status)
fmt.Println("count:", myObjects.Count())
// print out all the objects found in the NIB file
var i uint
for i = 0; i < myObjects.Count(); i++ {
fmt.Printf("Index %d: %s\n", i, myObjects.ObjectAtIndex(i).Description())
}
}
I do still like the idea of using a Go slice in place of a (NS)Array type. It more Go-friendly.
from macdriver.
Yes that would be nice. I'm not even sure it's possible here. We convert/copy the Go slice to an NSArray and then because it's just passed by reference we have no way to know when to convert/copy it back to our slice, which we would have to keep track of and further complicate an already tricky memory management situation.
Glad it worked!
from macdriver.
Could we leave this issue open until the fix is in the repo?
from macdriver.
What solution would make you want to close it?
from macdriver.
I think adding the rule to the bindings generator that prevents translating from an Objective-c type to a Go type when dealing with a pointer to a pointer in a parameter would fix the problem. One reason to use a pointer to a pointer is to return an object thru a parameter like an NSError.
Here are some of the methods that are probably affected by this issue:
- NSFileManager's contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error;
- NSKeyedUnarchiver's unarchivedObjectOfClass:fromData:error:
- NSString's writeToFile:atomically:encoding:error:
from macdriver.
That is a great suggestion that I think should work well but can we make it a separate issue?
from macdriver.
I think that is a good idea. This issue probably affects more than just NSNib's InstantiateWithOwnerTopLevelObjects().
from macdriver.
Created issue #234 for the generator issue.
from macdriver.
Related Issues (20)
- SetDefaultApplicationAtURLToOpenContentTypeCompletionHandler error
- ComboBox `SetDelegate` uses `TextField`'s `SetDelegte` resulting in error HOT 1
- module github.com/progrium/macdriver@latest found (v0.4.0), but does not contain package github.com/progrium/macdriver/keycode HOT 2
- Can I use Network Extension HOT 2
- Framework Request: ScreenCaptureKit
- DarwinKit: Rendering Problem with OutlineView
- SetDuration Can not set duration for amination
- Generator change: keep Objective-C type when parameter is a pointer to a pointer HOT 1
- Framework Request: StoreKit HOT 2
- enumexport error with call to s.Parse
- Generating appkit problem with enumtypes.gen.go and aliastypes.gen.go
- APFS (Case-sensitive) filesystem protocols.gen.m:3:9: fatal error:: 'Appkit/Appkit.h' file not found
- Can macdriver be used to implement macOS widgets and install them directly into the macOS notification center or desktop?
- corelocation API giving panic HOT 6
- Dictionary's DictionaryWithObjectsAndKeys() throws NSInvalidArgumentException HOT 6
- An error occurs when running the layout example. HOT 2
- Notification example doesnโt work. HOT 1
- user-agent? HOT 1
- Can not create Event Taps HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from macdriver.