[futurebasic] Re: [FB] Non-legacy method for setting a file type and creator

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : April 2012 : Group Archive : Group : All Groups

From: Max Taylor <maxclass@...>
Date: Mon, 23 Apr 2012 09:27:21 -0700
On Apr 23, 2012, at 01:26 AM, Bernie wrote:

> 
> Deep asked:
> 
>> Also, how would you Get (rather than Set) the Type and Creator info?
> 
> '---------------
> compile as "Objective-C"
> include "ConsoleWindow"
> 
> local fn SetTypeCreator( url as CFURLRef, type as OSType, creator as OSType )
> '~'1
> BeginCCode
> NSString *path = [(NSURL *)url path];
> NSNumber *typeNum = [NSNumber numberWithUnsignedLong:type];
> NSNumber *creatorNum = [NSNumber numberWithUnsignedLong:creator];
> NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:typeNum, NSFileHFSTypeCode, creatorNum, NSFileHFSCreatorCode, nil];
> [[NSFileManager defaultManager] setAttributes:attributes ofItemAtPath:path error:nil];
> [[NSWorkspace sharedWorkspace] noteFileSystemChanged:path];// force Finder update (seems to work fine here without this line)
> EndC
> end fn
> 
> local fn GetTypeCreator( url as CFURLRef, type as ^OSType, creator as ^OSType )
> '~'1
> BeginCCode
> NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[(NSURL *)url path] error:nil];
> *type = [[attributes objectForKey:NSFileHFSTypeCode] unsignedLongValue];
> *creator = [[attributes objectForKey:NSFileHFSCreatorCode] unsignedLongValue];
> EndC
> end fn
> 
> local fn MyChooseFileHandler( reply as ^NavReplyRecord, userData as pointer )
> '~'1
> dim as CFURLRef   url
> dim as OSType     type, creator
> 
> NavDialog_CopyItemCFURLRef( #reply, 1, @url )
> fn GetTypeCreator( url, @type, @creator )
> print mki$(type),mki$(creator)
> CFRelease( url )
> end fn
> 
> fn NavDialog( _kNavDialogChooseFile, "Choose file", "", @fn MyChooseFileHandler, 0 )

Forgive me if I'm sounding a little picky but if we are trying to mimic what Objective-C does inside of wrappers. That's cool.

For the sake of this argument, function = method and method = function, although in design they are very different.

So, for the sake of following Objective-C's naming conventions, and getting folks used to them (in a teaching kind of way), I feel that doing so would be a positive thing.

So, in Objective-C:

1. All methods begin with a lower case letter.

2. All "setters" begin with the word "set" followed by the method name ( i.e. "setTypeCreator", as in the above example).

3. All "getters" for (as in #2 above) only have the method name ( i.e. "typeCreator" ) also beginning with a lower case letter.

4. All setter/getter methods are typically designed to set or get only one object instance value.

Re: #3 - Thus, "setTypeCreator" is the setter and "typeCreator" is the getter in the above example. These are typically used to "Set" and "Get" instance variables values within a given object.

Re: #4 - Following Objective-C conventions the above example would actually have four methods.

type & setType

creator & setCreator

// - - - - - - -

To start off learning and using these conventions now means that we will not have to unlearn anything that we will be up against in the future.

A good start and good practice as I see it. Thanks for your indulgence.

Max
