On Monday, 5 February 2018 at 19:44:37 UTC, Jacob Carlborg wrote:
Note that this applies to all classes, not just NSString.

Ah yes, I will make sure it works for all the NSObject types.

class NSStringRef {
public:
     this(string s) {
        str_ = NSString.alloc.initWithBytes(cast(immutable(ubyte)*) s.ptr,
                                             s.length,
NSStringEncoding.NSUTF8StringEncoding);
     }

     ~this() {
         str_.release();
     }

Note that this is not deterministic. There's not even a guarantee that a destructor for a class will be run at all.

Oh I simply tested this by running millions of allocations and it seemed to work but I will have to use a struct then instead I guess.

     NSStringRef s = new NSStringRef("Hello");
     NSLog(s.str);

You can add an "alias this" [1] to avoid calling "str" explicitly.

Ah that's neat!

Currently the only correct way would be to wrap the class in a struct. There as been some talk to extend the language to support for reference counted classes [2].

Ok that sounds good. However, I'm mostly interested in how to make it work with the tools that are available now :)


Reply via email to