NSlog and NSMutableArray with Strings and floats

Hi all
I am still learning about Objective C,

In one of the scripts i am writing to learn, I have created a NSMutableArray.

And added to it various objects of different classes. ( NSString, NSMutableString, NSArray, NSMutableDictionary, NSURL, NSProcessInfo, Objects from NSArray,) using different methods for add and insert.

This all works well and has helped me in my learning curve.

If the objects or contents of the objects added are all of one class i.e NSString, integer or floats,
then I can use %@ , %i, @f respectively with NSLog.

But I wondered if there was a way to print using NSlog, when the items of an array has a mix of Strings and floats or integers in it.

Many thanks for any answers.


Hi Mark,

as all items of NSArray are objects, you can print all with %@.
For example a float or integer is a NSNumber object. You can also log the array itself with %@

NSArray *array = [NSArray arrayWithObjects:[NSNumber numberWithInt:10], [NSNumber numberWithFloat:25.96], @"Hello World", nil]; for (id obj in array) NSLog(@"%@", obj);

Thanks Stefan,
Thats what I thought,

But this does not work:

NSProcessInfo *newProcessInfo = [NSProcessInfo processInfo];
	NSString *newProcessName = [newProcessInfo processName];
	float newProcessID = [newProcessInfo processIdentifier];
	NSString *newHostName = [newProcessInfo hostName];
	float newProcessorCount = [newProcessInfo processorCount];
	NSArray *arrayNorm = [NSArray arrayWithObjects: newProcessName, newProcessID, newHostName, newProcessorCount, nil];
	for (id obj in arrayNorm)
		NSLog(@" %@", obj);

What am I missing??

an item of NSArray can’t be a primitive like float or integer, it must be an object (like NSNumber)

read the documentation of NSProcessInfo, the result of newProcessID is int ,
the result of newProcessID is NSUInteger

[code]NSProcessInfo *newProcessInfo = [NSProcessInfo processInfo];

NSString *newProcessName = [newProcessInfo processName];

int newProcessID = [newProcessInfo processIdentifier];
NSString *newHostName = [newProcessInfo hostName];
NSUInteger newProcessorCount = [newProcessInfo processorCount];

NSArray *arrayNorm = [NSArray arrayWithObjects: newProcessName, [NSNumber numberWithInt:newProcessID], newHostName, [NSNumber numberWithUnsignedInteger:newProcessorCount], nil];

for (id obj in arrayNorm)
    NSLog(@" %@", obj);[/code]


I think that was partly me not paying attention… I did read NSProcessInfo, and see what was should be returned, but for some reason I changed my type cast (if thats what you call it) along the way…??