xcode / Obj-C write to file

I need a small script to write a file with in a ‘.m’ Object-C class.

Basically I want to write and save a txt doc (or even to a local MySQL db) using Obj-C. If anyone has such code then this would be great.

The following code is used to addDevicetoList for the bluetooth class:

-(void)addDeviceToList:(IOBluetoothDevice*)inDevice
{
    id								newButton;
	const BluetoothDeviceAddress*	addressPtr			= [inDevice getAddress];
    id 								buttonIcon			= NULL;
	BluetoothDeviceClassMajor		deviceClassMajor 	= [inDevice getDeviceClassMajor];
	BluetoothDeviceClassMinor		deviceClassMinor 	= [inDevice getDeviceClassMinor];
    NSString*						deviceCODString		= nil;
    NSString*						deviceAddressString	= nil;
	NSString*						deviceNameString	= [inDevice getName];
	NSString*						connectionInfo		= nil;

	// Get the device address and deal with the name.

	if( addressPtr )
	{
		deviceAddressString = [NSString stringWithFormat:@"%02x-%02x-%02x-%02x-%02x-%02x",	addressPtr->data[0],
																							addressPtr->data[1],
																							addressPtr->data[2],
																							addressPtr->data[3],
																							addressPtr->data[4],
																							addressPtr->data[5]];
	}
	
	if( !deviceNameString )
	{
		deviceNameString = @"<Name not yet known>";
	}
	
	// Make sure we don't already have this device in the list.
	
	if( ![self saveNewDeviceIfAcceptable] )
	{
		// Already have seen it. Bail.
	
		return;
	}
	
	// Create COD info string.

	deviceCODString = [NSString stringWithFormat:@"Major Class: %@ (0x%02x)\nMinor Class: %@ (0x%02x)",
													GetStringForMajorCOD( [inDevice getDeviceClassMajor] ),
													[inDevice getDeviceClassMajor],
													GetStringForMinorCOD( [inDevice getDeviceClassMajor], [inDevice getDeviceClassMinor] ),
													[inDevice getDeviceClassMinor]];
	
	// If there is a connection in place shows the connection handle:
	if ( [inDevice isConnected] )
	{
		connectionInfo =  [NSString stringWithFormat:@"Connected, Connection Handle 0x%04x", [inDevice getConnectionHandle]];
	}
	else
	{
		connectionInfo =  [NSString stringWithFormat:@"Not Connected"];
	}

	
	// load up an image for the class of device which we have found.
	
	[self getIconForClassOfDevice:&buttonIcon
			majorClass:deviceClassMajor
			minorClass:deviceClassMinor];
	[buttonIcon setScalesWhenResized:TRUE];
	[buttonIcon setSize:gCellImageSize];

	// Make space for and get a button for the new device.
	
	[_buttonMatrix addRow];	
	newButton = [_buttonMatrix cellAtRow:[_buttonMatrix numberOfRows]-1 column:0];
	if( !newButton ) return;

	// Set the button's attributes.

	[newButton setImage:buttonIcon];
	[newButton setTitle:[NSString stringWithFormat:@"%@ / %@\n%@\n%@", [deviceAddressString uppercaseString], deviceNameString, deviceCODString, connectionInfo]];
	[newButton setTag:(int)inDevice];

	// make it the right type of button and update the display.
	
	[_buttonMatrix sizeToCells];
	[_matrixView setNeedsDisplay:TRUE];
}

The write to .txt doc will need to go in here somewhere probably at the end, so if you do now a way to do it then please let me know.

Many Thanks si:)

Model: iBookG4 and a G5
AppleScript: Xcode 2.4.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

You’ll find more help for something like this on the Cocoa-dev mailing list. Check out lists.apple.com.

At any rate, NSString has methods to write strings to disk:

http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/writeToFile:atomically:encoding:error:
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/writeToURL:atomically:encoding:error:

If you decide to store your data in an NSDictionary, for organizational purposes, that class also has very convenient read and write methods.

thanks for the reply Mikey-San.

Will look up thous links.

I am after it to write toa file/db then be able to recall the information in from another application Flash etc from a search field for device name. If possible might consider using applescript for my searching and display program. Thats another matter thanks matey.