Declaring with alloc

Coding Questions, IDE related or anything else

Declaring with alloc

Postby udo.killermann » Fri Apr 23, 2010 7:11 pm

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
--------------
MacBook (2GHz, 4 GByte) - Lion (10.7.3)
started Objective Basic coding in April 2010 - still enjoy it ;-)
udo.killermann
 
Posts: 247
Joined: Thu Apr 08, 2010 6:46 am
Location: Hannover, Germany

Re: Declaring with alloc

Postby berndnoetscher » Fri Apr 23, 2010 7:41 pm

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?
berndnoetscher
Site Admin
 
Posts: 280
Joined: Fri Feb 20, 2009 11:58 am

Re: Declaring with alloc

Postby udo.killermann » Sat Apr 24, 2010 9:06 am

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
--------------
MacBook (2GHz, 4 GByte) - Lion (10.7.3)
started Objective Basic coding in April 2010 - still enjoy it ;-)
udo.killermann
 
Posts: 247
Joined: Thu Apr 08, 2010 6:46 am
Location: Hannover, Germany

Re: Declaring with alloc

Postby berndnoetscher » Sat Apr 24, 2010 6:13 pm

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.
berndnoetscher
Site Admin
 
Posts: 280
Joined: Fri Feb 20, 2009 11:58 am


Return to Objective-Basic (Xcode 3)

Who is online

Users browsing this forum: No registered users and 1 guest

cron