Creating NSCalendar object

On Mavericks (10.9)

Trying to create an NSCalendar object and looking at all the objective-C examples it should work something like this:

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

which I then translated in AppleScriptOBJC to:

set v_NSCalendar to current application's NSCalendar's (alloc()'s initWithCalendarIdentifier:(current application's NSGregorianCalendar))

However, this then causes a dump with EXC_BAD_ACCESS (SIGBUS) and the crashed thread has:

I can easily create a NSCalendar object with:

set v_NSCalendar to current application's NSCalendar's currentCalendar()

and then set the calendar to gregorian:

v_NSCalendar's setLocale:(current application's NSLocale's alloc()'s initWithLocaleIdentifier:(current application's NSGregorianCalendar))

But I am just trying to understand what the initWithCalendarIdentifier does not work. If I try:

set v_NSCalendar to current application's NSCalendar's alloc()

then I get the exact same error.

Any ideas?

Hi ehendrix,

Good question. I’ve always wondered exactly what alloc() init() meant. S something to do with memory I think.

gl,
kel

What if you use something like this:

set theId to (current application's NSLocaleIdentifier)
set v_NSCalendar's setLocale:(current application's NSLocale's alloc()'s initWithLocaleIdentifier:theId)

gl,
kel

Hey,

Problem is not with that, I can set the locale on my NSCalendar once created with currentcalendar. The problem is with:

set v_NSCalendar to current application's NSCalendar's (alloc()'s initWithCalendarIdentifier:(current application's NSGregorianCalendar))

which I believe should work but it doesn’t.

It’s a bug, and a nasty one at that. Please report it.

Better to use:

set v_NSCalendar to current application's NSCalendar's currentCalendar()'s |copy|()

Otherwise you’re fiddling with the user’s calendar.

Thanks Shane,

Just submitted the problem to Apple together with a crash report. BTW, thanks for the hint as well on using the copy. :slight_smile:

One silly question, using AppleScriptOBJ the whole ApplecriptOBJ crashes on something like this. Is it possible within AppleScriptOBJ to do something so that itself does not crash? Just wondering.

Thanks!

Hi ehendrix,

You can use the error handler.

gl,
kel

Not really. It looks like it’s getting into an infinite loop, and that somehow gets detected and the whole thing killed to stop it.

Hey Shane, understood. But what in a scenario as this for example:

Application Specific Information:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[BAImmediateData intValue]: unrecognized selector sent to instance 0x60000001a940'
abort() called
terminating with uncaught exception of type NSException

Would seem to me that for this it should be able then to catch that exception and display the error instead?

AppleScriptObjC does generally seem to use a @try/catch with libraries – otherwise you’d never get error dialogs – but @try/catch doesn’t always work.