Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

lopoz

macrumors regular
Original poster
May 10, 2005
134
9
I'm writing a basic image viewer with image metadata support, but I'm having a little trouble when specific metadata can't be found.

I've extracted the metadata from the image and now I'm creating an array to hold the specific data. BUT, when some data isn't available, the return value will be nil, thus terminating my array, which gives problems for me later on.

How can I make the return value an empty string? (e.g. @"")

Code:
CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef) url, NULL);
NSDictionary *metadata = (NSDictionary *)CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);

metadataList = [NSArray arrayWithObjects:
					[NSString stringWithFormat:@"%@ x %@", [metadata objectForKey:(id)kCGImagePropertyPixelWidth],[metadata objectForKey:(id)kCGImagePropertyPixelHeight]],
					[metadata objectForKey:(id)kCGImagePropertyProfileName], etc.etc..

If any more info is needed, please let me know..

Thanks in advance!
 
Code:
[metadata objectForKey:(id)kCGImagePropertyPixelWidth]?[metadata objectForKey:(id)kCGImagePropertyPixelWidth]:@""
In the line where you're building up the array, you could do this for each property.

Or you could create a NSMutableDictionary instead, loop through the keys you want to use in building your new array, and if nil is returned, alter the value to @"".

There are many other permutations, but hopefully that gives you some ideas.

-Lee
 
Code:
[metadata objectForKey:(id)kCGImagePropertyPixelWidth]?[metadata objectForKey:(id)kCGImagePropertyPixelWidth]:@""
In the line where you're building up the array, you could do this for each property.

Or you could create a NSMutableDictionary instead, loop through the keys you want to use in building your new array, and if nil is returned, alter the value to @"".

There are many other permutations, but hopefully that gives you some ideas.

-Lee
Thanks! The question mark approach is working quite nicely! Could you please explain what this does? Or point me to some book/documentation where I could have found this?
 
Thanks! The question mark approach is working quite nicely! Could you please explain what this does? Or point me to some book/documentation where I could have found this?

It's called a ternary operator. It's basically an inline if/else statement:

Code:
myvar = (a < b) ? c : d;

is the same as:

Code:
if ( a < b ) {
    myvar = c;
} 
else {
    myvar = d;
}
 
Thanks! The question mark approach is working quite nicely! Could you please explain what this does? Or point me to some book/documentation where I could have found this?

What about any book that explains programming in C / C++ or Java? It is just the ?: operator, nothing more. For an (almost) definitive source on C programming, type "n1124.pdf" into Google. And remember that Objective C is a superset of C.
 
What about any book that explains programming in C / C++ or Java? It is just the ?: operator, nothing more. For an (almost) definitive source on C programming, type "n1124.pdf" into Google. And remember that Objective C is a superset of C.
Thanks for that. Could've done without the condescending tone, but whatever..

@splitpea: Thanks for the info!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.