Page 1 of 1

Declaring with alloc

PostPosted: Fri Apr 23, 2010 7:11 pm
by udo.killermann
Hi Forum,

I would like to share an insight I had over the last few days. Looking at the networking classes of Cocoa I found NSSocketPort which gives you either access to local or remote sockets for TCP communication. In the class reference I only found methods to initialize the instances but no class method to allocate(!) the instance. I ended up with these declares:
Code: Select all
Declare Class "NSSocketPort"
Declare Function "NSSocketPort" - (id)initRemoteWithTCPPort:(NSInteger)port host:(NSString *)hostName
Declare Function "NSSocketPort" - (id)initWithTCPPort:(NSInteger)port
Declare Function "NSSocketPort" - (id)init
Declare Function "NSSocketPort" - (id)address ' was (NSData *)


Now came the research stuff and several crashes, trying to use these methods. Here is an excerpt:

Code: Select all
Dim mySocketPort As NSSocketPort
mySocketPort.initRemoteWithTCPPort(80, host:="www.heise.de")
' -> results in a crash sometimes caught by the IDE sometimes by OSX


To make a long story short, I was missing the alloc method, which is a class method for each and every Cocoa class inherited from NSObject. So I needed to add the following Declare:

Code: Select all
Declare Function "NSSocketPort" + (id)alloc ' didn't think of this before!

This method allocates the memory needed by NSSocketClass' instances. Now all the initializers can work as expected.

The code becomes
Code: Select all
Dim mySocketPort As NSSocketPort

mySocket=NSSocketPort.alloc() ' here the magic happens ;-)
mySocketPort.initRemoteWithTCPPort(80, host:="www.heise.de")
' -> now it compiles and works like a charm


This should work for every class within the Cocoa environmet you should meet - happy coding.

Regards
Udo

BTW: If asked by customs "Something to declare?", what would the OB-Coder answer?
"Cocoa only!"

Here comes the complete code as a working example:
Code: Select all
Declare Class "NSSocketPort"
Declare Function "NSSocketPort" - (id)initRemoteWithTCPPort:(NSInteger)port host:(NSString *)hostName
Declare Function "NSSocketPort" - (id)initWithTCPPort:(NSInteger)port
Declare Function "NSSocketPort" - (id)init
Declare Function "NSSocketPort" - (id)address ' was (NSData *)
Declare Function "NSSocketPort" + (id)alloc


Event AwakeFromNib()
  Dim mySocketPort As NSSocketPort
  Dim myData As NSData
   
  mySocketPort=NSSocketPort.alloc()
 
  Log(ClassName(mySocketPort))
 
 ' mySocketPort.initRemoteWithTCPPort(80, host:="www.heise.de")
  mySocketPort.initRemoteWithTCPPort(80, host:="localhost") ' HTTP Server running on localhost

  ' mySocketPort.initWithTCPPort(48000) ' opens local port 48000 (non privileged port)
  Log("mySocketPort")
  Log(mySocketPort)

  ' now we can use the SocketPort for something useful

  myData = mySocketPort.address()
  Log("myData")
  Log(myData)

End Event

Re: Declaring with alloc

PostPosted: Fri Apr 23, 2010 7:41 pm
by berndnoetscher
Hi Udo,

thanks for your example. I will add it to the official examples, if you don't mind.
Maybe alloc and init should be automatically supported for every cocoa class. What do you think of it?

Re: Declaring with alloc

PostPosted: Sat Apr 24, 2010 9:06 am
by udo.killermann
Hello Bernd,

thanks for including my code in the examples. What do you think of grouping the examples you have? Otherwise the list gets very long by the time.
I think it would be very helpful for the beginner and also the experienced to have alloc and init available in every Declared Cocoa class.
What should be done with release? Do you autorelease the allocated instances within the runtime?

Kind regards
Udo

Re: Declaring with alloc

PostPosted: Sat Apr 24, 2010 6:13 pm
by berndnoetscher
Hi Udo,

all ObjB apps run with the built-in garbage collector of the Objective-C runtime. So there is no need for release/autorelease.
Will group the example list. Thanks.