GithubHelp home page GithubHelp logo

Comments (9)

progrium avatar progrium commented on June 6, 2024

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.

programmingkidx avatar programmingkidx commented on June 6, 2024

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.

progrium avatar progrium commented on June 6, 2024

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.

programmingkidx avatar programmingkidx commented on June 6, 2024

Could we leave this issue open until the fix is in the repo?

from macdriver.

progrium avatar progrium commented on June 6, 2024

What solution would make you want to close it?

from macdriver.

programmingkidx avatar programmingkidx commented on June 6, 2024

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.

progrium avatar progrium commented on June 6, 2024

That is a great suggestion that I think should work well but can we make it a separate issue?

from macdriver.

programmingkidx avatar programmingkidx commented on June 6, 2024

I think that is a good idea. This issue probably affects more than just NSNib's InstantiateWithOwnerTopLevelObjects().

from macdriver.

programmingkidx avatar programmingkidx commented on June 6, 2024

Created issue #234 for the generator issue.

from macdriver.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.