Passing by reference

Hi all,

Does anyone know how to implement this line of Cocoa in ASOC?

[[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&dir];

Please note it is the &dir I am interested in. How do you implement this in ASOC.

I tried " a reference to" without success.

Thanks

–Terry

FWIW, I’ve had no luck doing it either.

The &dir is the memory address of a variable. I think I see where you got this from (in XCode documentation), but you don’t have to use &dir, you can just put a regular boolean.

Here’s the actual declaration:

  • (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory

could you do:
[[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:YES];
?

the expression

(BOOL *)isDirectory

is no regular boolean, *isDirectory is a pointer to a memory location, which will be returned with the address operator &

The “regular” boolean to be treated as input would be

(BOOL )isDirectory

PS: this might be an alternative

NSString *myDir = @"/Library/Preferences"; BOOL isDir = [[[NSFileManager defaultManager] attributesOfItemAtPath:myDir error:NULL] objectForKey:NSFileType] == NSFileTypeDirectory; NSLog(@"%i", isDir); // 1

Thanks StefanK this seems to work.

For others I have prepared a small example script which logs the info returned. Please see the console.

This needs to be attached to a button to try it.

	on checkDir_(sender)
		set tPanel to NSOpenPanel's openPanel()
		tPanel's setCanChooseDirectories_(true)
		tPanel's setCanChooseFiles_(false)
		tPanel's setAllowsMultipleSelection_(false)
		
		tPanel's setAllowedFileTypes_(missing value)
		
		tPanel's runModal()
		
		set tMyDir to tPanel's filenames()'s objectAtIndex_(0)
		log tMyDir
		
		set isDir to false
		
		set tDefaultManager to NSFileManager's defaultManager()
		set tAttribs to tDefaultManager's attributesOfItemAtPath_error_(tMyDir, missing value)
		log tAttribs
		
		set tFileType to tAttribs's objectForKey_("NSFileType") as string
		log tFileType
		
		if tFileType = "NSFileTypeDirectory" then
			set isDir to true
			beep
		end if
	end checkDir_

All the best

–Terry