Declaring with alloc
Posted: 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:
Now came the research stuff and several crashes, trying to use these methods. Here is an excerpt:
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:
This method allocates the memory needed by NSSocketClass' instances. Now all the initializers can work as expected.
The code becomes
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:
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