-
-
Notifications
You must be signed in to change notification settings - Fork 160
Issues with NSError and other arguments using pointer to pointer types
In MacDriver v0.5.0 before commit 6a98f82 method parameters that used a pointer to a
pointer type did not work. After this commit support for using
pointer to pointer types can work. There is still one issue. All the
methods in all the classes were generated using the old code. So a
lot of methods like those that returned an NSError thru an argument
do not work.
What commit 6a98f82 does
When generating Go wrapper code if a method's parameter is a pointer
to pointer type (e.g. error:(NSError * _Nullable *)error) the
parameter's type is set to unsafe.Pointer. Code that would use this
parameter would declare a variable (e.g. var myError
foundation.Error) and then pass the variable to the method by
wrapping it in an unsafe.Pointer call (e.g
unsafe.Pointer(&myError)).
Examples
Function signature generated before commit 6a98f82:
func NewXMLDocumentWithXMLStringOptionsError(string_ string, mask XMLNodeOptions, error IError) XMLDocument
Function signature generated after commit 6a98f82:
func NewXMLDocumentWithXMLStringOptionsError(string_ string, mask XMLNodeOptions, error unsafe.Pointer) XMLDocument
Original Objective-C function signature:
- (instancetype)initWithXMLString:(NSString *)string options:(NSXMLNodeOptions)mask error:(NSError * _Nullable *)error;
Error message when trying to compile a program using an outdated
function signature:
./xmltest.go:34:63: cannot use unsafe.Pointer(&myError) (value of type unsafe.Pointer) as foundation.IError value in argument to f.NewXMLDocumentWithXMLStringOptionsError: unsafe.Pointer does not implement foundation.IError (missing method Autorelease)
These steps will help fix the problem. It assumes you are using a
git repo and it is at commit 6a98f82 or newer. If you are not then
simply download the most recent version of MacDriver or run 'git
pull'. Then follow these directions:
- cd to your MacDriver folder.
- Run this command: make generate/symbols.zip
- Run this command: go generate ./macos/<framework name>
Example: go generate ./macos/foundation
Now you should be able to use all methods in the framework that use a pointer to pointer type. If everything is working this test program should pass both tests.
How to run this program
- Save this program to a file called xmltest.go.
- In the same folder as this file run: go mod init program
- Open the go.mod file and add these lines at the end of the
file:
require github.com/progrium/macdriver v0.5.0
replace github.com/progrium/macdriver => <path to your macdriver folder> - To run this program use: go run main.go
// Description: test generating a NSError using the XMLDocument class
package main
import "fmt"
import f "github.com/progrium/macdriver/macos/foundation"
import "unsafe"
func main() {
testWithProblem()
testWithoutProblem()
}
// Use a bad string to try to make a XML document. It should fail to create one.
func testWithProblem() {
inputStr := "BUG<root><element>Some text</element></root>"
options := f.XMLNodePreserveWhitespace
var myError f.Error
f.NewXMLDocumentWithXMLStringOptionsError(inputStr, options, unsafe.Pointer(&myError))
if myError.IsNil() == false {
fmt.Println("Testing with problem string...pass")
//fmt.Printf("Error code: %d\t domain:%s\t userInfo:%s\n", myError.Code, myError.Domain, myError.UserInfo)
} else {
fmt.Println("Testing with problem string...fail")
}
}
// Use a good string to try to make a XML document. It should succeed
func testWithoutProblem() {
inputStr := "<root><element>Some text</element></root>"
options := f.XMLNodePreserveWhitespace
var myError f.Error
f.NewXMLDocumentWithXMLStringOptionsError(inputStr, options, unsafe.Pointer(&myError))
if myError.IsNil() == true {
fmt.Println("Testing with good string...pass")
} else {
fmt.Println("Testing with good string...fail")
}
}