Answers inlined.
>> > Decided to help you a little further:
>> Thanks - I appreciate this ...
>> I have a few more questions (inline)
>> >
>> > The problem is you can't just send("button", ...), because the "button" is
>> > not a symbol, but a NSButton's class (or something like this) instance.
>> > I'm not sure because I've never done any MacOX programming, but iOS.
>> > And in order to send a message to an instance you need to know it. So, in
>> > general, your workflow should be like this:
>> >
>> > Erlang side:
>> > %% create button
>> > send
>> > {create_instance, {string, "NSButton"}}
>> > receive
>> > {ok, Instance}
>> question 1:
>> Is Instance an integer here? (see question 3)
Yes, see below.
>> >
>> > %% set button's title
>> > send
>> > {perform, {integer, Button} {string, "setTitle:"}, {string, "click me"}}
>> Button is the integer in "Instance" above I assume
Right. Typo :(
>> > receive
>> > ok
>> >
>> >
>> > Objective-C side
>> >
>> > On receiving {create_instance, {string, Clazz}}:
>> >
>> > // note that `Class' is a reserved word
>> > id instance = [[NSClassFromString(Clazz) alloc] init];
>> > if (instance) {
>> What is instance? is this "really" a 32 bit pointer?
>> How do I serialize it to send it back to erlang?
Yes, it's a 32 or 64 bit pointer depending on the machine.
I guess you can use any method as long as serialization/deserialization
sequence gives the original value.
>> > reply {ok, instance};
>> > } else {
>> > reply {error, @"No such class"}
>> > }
>> >
>> > On receiving {perform, {integer, Instance}, {string, Selector}, {string,
>> > Object}}:
>> > SEL sel = NSSelectorFromString(Selector);
>> > if (sel) {
>> > if ([Instance respondsToSelector(sel)]) {
>> > [Instance performSelector:sel withObject:Object];
>> > reply ok;
>> > } else {
>> > reply {error, @"No such selector"};
>> > }
>> > } else {
>> > reply {error, @"Invalid selector"}
>> > }
>> Same question here. On the "wire" I'll see an encoding of
>> {perform, {integer, Instance}, ... and so on How do I make an Objective
>> C id or NSString from a sequence of bytes that I read from a socket.
>> Also do I have to worry about strings (for example) being garbage
collected.
>> For example If I say (in objective C)
>> NSString s = @"hello"
>> I create a literal string in the variable s
>> But if I receive a string "hello" somewhere in a buffer from a socket
>> I need to create a NSString and put it somewhere so it doesn't get
>> garbage collected away. Something like:
>> NSMutableArray *stringtable = [[NSMutableArray alloc] init];
>> NSString s = [[NSString alloc] initWithString: Data]
>> [stringtable addObject:s];
>> (Pardon my Objective C here - I'm a total beginner here)
For id see above, just make sure you restore the same value you got from
the `id instance = [[NSClassFromString(Clazz) alloc] init]' call.
The `instance' is just a pointer, so I'm pretty sure this will work.
Regarding the strings. You receive an array of bytes and it's possible
to build an NSString object out of them, like this:
char array[5] = {'a', 'b', 'c', 'd', '\0'};
NSString *string = [NSString stringWithCString:array
encoding:NSASCIIStringEncoding];
Also as long as you create an NSString object and pass it as a param to
a method you don't need to worry about the string being garbage collected.
>> > This was a little messy, but hopefully you will get the idea.
>> Yes - I get the idea, the problem is the nitty gritty details, not
>> the idea.
>> The idea is easy. Allocate some memory to store the stuff you
received in
>> create some dynamic objects and call some methods - problem is how.
>> Thanks - I'll try this later
>> /Joe
Hope this helps :)