progrium/darwinkit

Translate NSString arguments to Go string

Closed this issue · 5 comments

I was looking at NSUserDefaults SetBoolForKey() function. It takes an NSStringRef as an argument. This is ok for Objective-C, but I was hoping these Go wrapped functions would use Go types like string.

I ask that all Go wrapped methods translate NSString and NSStringRef arguments to Go's string type. MacDriver was made so we could use Go code to work with Apple's frameworks. It only makes sense to be able to use Go types as well.

Return values that use NSString should also be translated into Go's string type.

tmc commented

I strongly agree -- we should map as many basic types between objc and go at the function call layer as we can.
This should be a fairly straightforward thing to tackle.

I did run into one issue. What do we do with the NSString class methods? If someone is using NSString's Alloc() method, that person probably wants an NSString object. But methods like ContainsString() could be useful with a Go string as an argument. I don't think I should just convert every method that uses a NSString to be able to return a Go string. But how do I decide which methods should not translate their NSString return value to Go's string type? Which methods should not have their NSString argument translated to a Go string argument?

I'm thinking we might need some rules to decide this. One could be any NSString method that begins with init* will return a NSString. If you are using one of NSString's init* methods then you probably expect to use an NSString argument.

Any thoughts or help would be great.

Strings, arrays, and others you may want access to their methods to do Objective-C specific things from Go. Plus if things return Go strings, we'd have to take Go strings everywhere, which means a LOT of conversions. I think the best thing to do for now is to focus on keeping the APIs using Objective-C types, and just add some standard helpers to convert.

I made a patch implements this feature. See #145.

Since #145 was merged and darwinkit also does this, we get to close this issue I think.