[futurebasic] Re: [FB] CFArray & CFDictionary create

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : December 2009 : Group Archive : Group : All Groups

From: Brian Stevens <bstevens33@...>
Date: Tue, 1 Dec 2009 22:36:19 -0700
On Dec 1, 2009, at 9:00 AM, Bernie wrote:

> NSArray *array = [NSArray arrayWithObjects:@"zero", @"one", @"two",  
> @"three", @"four", nil];


For those not familiar with C or Objective-C, some of the above may  
look strange. Bernie is teaching us generally about mutable/immutable  
arrays ( immutable is typically the default and mutable inherits from  
immutable - but I digress ), the use of Dictionaries ( which are  
ordered key/value pairs stored in xml files ) and contrasting how this  
is done in Objective-C and FB. Before plunging into the overall  
concepts of Bernie's good example, maybe a short discussion of the  
above syntax would help. If this is too basic, please let me know.

In C and Objective-C it is possible to define a variable and assign a  
value to it in one statement. The 'array' variable above reflects this  
syntax. In FB we must define the variable and then assign a value to it:

dim as ^long	aVar
aVar = @someOtherVar


The Objective-C statement above can be written in C in two statements  
just like we do in FB5:

NSArray *array;   // this defines the variable array as a pointer to  
the class NSArray
array = [NSArray arrayWithObjects:@"zero", @"one", @"two", @"three",  
@"four", nil]; // calls the NSArray class method arrayWithObjects and  
the returned pointer is stored in array

The syntax @"zero" specifies a constant( immutable ) of type NSString  
( which is the same as a CFString - they are "toll-free bridged" is  
Apple's terminology ). There is also CFMutableString but that is  
another topic. FB note: This compact syntax for creating a CFString  
has been adopted by FB5. Anywhere fn CFSTR("some pascal string" ) is  
used, the @"mystring" syntax may be used. CFStrings/NSStrings are  
important because ( unlike C and pascal strings ) they automatically  
handle Unicode for us with no additional work. Using CFStrings in FB  
has the advantage of being portable to Cocoa's NSStrings.

The brackets ( i.e. [ ] ) enclose the method call. If there are  
multiple sets of brackets ( the example only has one set ), they are  
evaluated from the inner-most pair to the outer-most very much the  
same way parenthetical nesting of function calls would work in FB5.  
The general format of the call is: [ object method ] where object is  
NSArray and method is arrayWithObjects. A method is similar to a  
function call in C or FB. Objective-C folks say they are sending a  
message to a receiver. The receiver is the object and the message is  
the method invocation plus its parameter(s). The colon ":" after the  
method name indicates the method has a parameter. Typically, more  
colons indicate more parameters passed. The above method call is  
different because it passes the full array in one parameter specified  
as a list of NSString constants delimited at the end by nil. The list  
of NSString constants is variable. Some method calls have variable  
list of parameters but most have a fixed number of parameters.

Hope this introduction was interesting. It isn't complete or  
exhaustive by any means, but maybe piques some interest in newer  
methods.


Brian S