lukaskollmer / objc

🔮 NodeJS ↔ Objective-C bridge (experimental)
MIT License
98 stars 20 forks source link

Checking for null #9

Closed erikjalevik closed 6 years ago

erikjalevik commented 6 years ago

This is a follow-up question to my previous one, and again I'm not sure if it's a bug or I'm doing something wrong.

I'm trying to check that the errorOut variable is null in the following code:

    const trashedFileUrlOut = objc.allocRef();
    const errorOut = objc.allocRef();
    const fm = NSFileManager.defaultManager();
    const success = fm.trashItemAtURL_resultingItemURL_error_(
      nsFileUrl,
      trashedFileUrlOut,
      errorOut
    );

    // Fails because errorOut seems to be an empty object in JS
    expect(errorOut).toBeNull();

    // js function fails with 'Unable to find method isKindOfClass_ on object (null)'
    expect(objc.js(errorOut)).toBeNull();

But I seem to need some other way of converting a null NSError* into JS land.

erikjalevik commented 6 years ago

Furthermore:

    // Fails with 'Unable to find method asymmetricMatch_ on object (null)'
    expect(errorOut).toEqual({});
lukaskollmer commented 6 years ago

Hi, sorry for the late response i only just saw the issue.

Due to how JavaScript passes objects when calling a function, it's impossible to set the parameter to null. Therefore, a simple errorOut === null check won't work.

However, I added a helper function (objc.isNull) to check whether an object is nil (ie whether an objc.Instance object is holding a nil reference).

Example

const error = objc.allocRef();
NSFileManager.defaultManager().trashItemAtURL_resultingItemURL_error_(nsUrl, null, error);

console.log(objc.isNull(error)); // -> prints "true"

(You'll have to update to v0.17)