On Sunday, February 2, 2003, at 02:28 PM, Dan Mills wrote:
No, alloc is a class method that creates a new instance. Init and friends are instance methods that you call to initialize the new instance.On Sunday, February 2, 2003, at 01:19 PM, Sherm Pendley wrote:my $dict = NSDictionary->alloc->init;Interesting, so there is already a global instance of NSDictionary?
I say "and friends" because there are variations. For example, to create an NSDictionary from the contents of a file:
my $dict = NSDictionary->alloc->initWithContentsOfFile("/tmp/foo");
Or, to create an NSMutableDictionary with an initial capacity for five objects:
my $mdict = NSMutableDictionary->alloc->initWithCapacity(5);
The basic rule is, whenever you send an ObjC message with colons in the name, replace them with underscores. If there is a trailing underscore, remove it.$dict->writeToFile_Atomically("/tmp/foo", 1);I forgot to try with underscores :)
For example, here's a bit of Objective-C that creates an NSMutableDictionary and adds an object and key to it:
NSMutableDictionary *myDict;
myDict = [[NSMutableDictionary alloc] initWithCapacity: 1];
[myDict setObject:anObject forKey: aKey];
The equivalent in Perl would be:
my $myDict = NSMutableDictionary->alloc->initWithCapacity(1);
$myDict->setObject_forKey($anObject, $aKey);
At the moment, you'll need to manually create a Cocoa dictionary object, populate it, and pass that. A future version of CB will include "toll-free" bridging of collections.They're intended to provide access to a Perl hash/array, using the NSDictionary/NSArray interface. They're mainly there for ObjC programmers, but if you wanted to use Cocoa methods to work with a Perl hash or array from Perl, I suppose that would work too.What I wanted to do was to get an ObjC object that contained the data in my perl hash without having to manually iterate over the contents of my hash and fill it in.
sherm--
"I have no special gift, I am only passionately curious." - Albert Einstein