[sword-devel] Objective-C/Cocoa example code

Nathan Youngman sword-devel@crosswire.org
Sat, 30 Mar 2002 13:14:09 -0700


For anyone interested, this is an Objective-C code snippet (Mac OS X) 
to populate a drop down with a list of modules installed. It's really 
not that complicated - square brackets are for method calls, @ 
symbols indicate NSString's (vs. normal C strings).

Because Cocoa uses a bunch of its own data types (NSString is a 
Unicode string object used throughout the API), it seems I will be 
writing several wrappers for the Sword API. The swordModulesForType 
function below would be some of that wrapper code, using a mix of 
Objective-C and C++. The code in my AppController could then be pure 
Objective-C.

- n8


/* Startup code
  */
- (void)awakeFromNib
{
	[modulePopUp removeAllItems];
	[modulePopUp addItemsWithTitles: [self swordModulesForType: 
@"biblical texts"]];
}

/* swordModulesForType:
    Retrieve list of installed modules as an array, where type is:
	@"Biblical Texts"
	@"Commentaries"
	...
	@"ALL"
  */
- (NSMutableArray *)swordModulesForType:(NSString *)type
{
	SWMgr manager;
	SWModule *module;
	ModMap::iterator it;
	NSMutableArray *moduleList = [[NSMutableArray alloc] init];

	for (it = manager.Modules.begin(); it != manager.Modules.end(); it++)
	{
		module = it->second;
		if( [type caseInsensitiveCompare: @"ALL"] ||[type 
caseInsensitiveCompare:
			[NSString stringWithCString:module->Type()]] )
		{
			[moduleList addObject: [NSString 
stringWithCString:module->Name()]];
		}
	}

	[moduleList autorelease];	// release memory later
	return moduleList;
}


-- 
Nathan Youngman
E-mail: sword at nathany.com
Web: http://nathany.com