NSArray - issues passing to LCS?

Moderators: LCMark, LCfraser

Post Reply
Simon Knight
Posts: 539
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

NSArray - issues passing to LCS?

Post by Simon Knight » Tue Feb 11, 2020 7:44 pm

Hi, I have LCB code that queries an EKEventStore for all the events that fall between two dates. The data is returned to the code stored in an NSArray where each element of the array is an event object. Here is Apple's documentation:
I have tried converting this array to a string using Trevor's example but I believe that certain properties are being lost. For example while I can return the titles of events, they are being returned without any dates which is not very helpful.

I believe that I am going to have to write more foreign handlers to process the elements of the array reading the object properties that I need as I step through each element. However, I thought I would write this post to confirm that there isn't a shortcut before I start adding more foreign handlers.
best wishes
Skids

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 979
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

Re: NSArray - issues passing to LCS?

Post by trevordevore » Tue Feb 11, 2020 7:55 pm

Ah, that is an array of EKEvent objects. You have to loop through the array and extract each object. Take a look at the AppCastItemsToArray handler in my Sparkle project on Github. It creates an LCB list from an NSArray and then converts each AppCastItem object in the list to an array in LCB. Look at DidFinishLoadingAppcast to see how AppCastItemsToArray is used to create an array that is then sent to LCS.

https://github.com/trevordevore/lc-spar ... e.lcb#L575
Trevor DeVore
ScreenSteps - http://www.screensteps.com

Levure Application Framework: https://github.com/trevordevore/levure
LiveCode Resources for Developers: http://livecode.bluemangolearning.com

Simon Knight
Posts: 539
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: NSArray - issues passing to LCS?

Post by Simon Knight » Tue Feb 11, 2020 9:52 pm

Hi Trevor,

Thanks for confirming what I have to do, I have already made a start but I will study the links that you have provided. So far I have managed to get a count of the array elements - small steps...!

One thing I am unclear of is the use of objcRetainedID versus objcID when writing foreign handlers. Is there a simple explanation as to when it should be used?
best wishes
Skids

Simon Knight
Posts: 539
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: NSArray - issues passing to LCS?

Post by Simon Knight » Tue Feb 11, 2020 10:04 pm

Ha!,

So I don't need a count now I have seen how you use ListFromNSArray!

The dictionary is a little spartan on ListFromNSArray am I correct in thinking it returns a List of object IDs ?

TVM
best wishes
Skids

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 979
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

Re: NSArray - issues passing to LCS?

Post by trevordevore » Tue Feb 11, 2020 10:25 pm

An NSArray is a "A static ordered collection of objects." so yes, ListFromNSArray is going to return a List of objective-c object ids.

Regarding ObjcID and ObjcRetainedID - I don't have a great understanding of these and I always have to refresh my memory when I start coding in LCB again. But here are a couple of notes that Monte put in Gitter which provide some guidance:
Dec 16 2018: Regarding the changing of ObjcRetainedId to ObjcId the issue there is you are using class factory methods which you (ie the engine) are not responsible for releasing. In many cases the returned object is autoreleased which is the case here but in some cases it might be a singleton (like [NSApplication sharedApplication]). ObjcRetainedId tells the engine that it needs to take ownership of an already retained object and release the object when it goes out of scope. Returning ObjcAutoreleasedId or ObjcId (they are actually the same thing) tells the engine it needs to retain the object and release when it goes out of scope.
Aug 24 2018: Hi /all I figured something out yesterday that isn’t all that intuitive with objective-c FFI I thought I would share. Object init methods when they fail call [self release] on themselves before returning nil. This causes an issue if you have defined your alloc method as returning ObjcRetainedIdas there is now a reference to an already released object that will have release called on it again as the variable in LCB goes out of scope.

Given you always call alloc then an init method the way to deal with this is to have the alloc method defined as returning ObjcId and the init method defined as returning optional ObjcRetainedId. After init you check if the variable is nothing before using it.

FYI the difference between ObjcId and ObjcRetainedId is that ObjcId will be retained on creation and released on deletion in LCB while ObjcRetainedId will only be released on deletion.
Trevor DeVore
ScreenSteps - http://www.screensteps.com

Levure Application Framework: https://github.com/trevordevore/levure
LiveCode Resources for Developers: http://livecode.bluemangolearning.com

Simon Knight
Posts: 539
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: NSArray - issues passing to LCS?

Post by Simon Knight » Wed Feb 12, 2020 7:33 pm

Hi Trevor,
I don't have a great understanding of these
me neither and if you don't have a great understanding then as a tyro I have little chance. My fear is that my extension will leak memory, but it seems that the use of the retainedID is an attempt to maintain an object in memory until the LC engine is finished with it. I'll blunder on and keep my fingers crossed that all is well.

best wishes

Simon
best wishes
Skids

Post Reply

Return to “LiveCode Builder”