I found this code in our project and it just feels like the wrong way to do what it seems to be doing
id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
for (symbol in results) {
// this just selects the first symbol in the results
}
Presumably it actually doesn't select the first symbol - instead it selects the last one!
But there must be a better way. I don't quite know what
[info objectForKey: ZBarReaderControllerResults]
returns, but I'm not too bothered either. Is there a way that I can do the above code without having to loop through all possible results?
1 Answer 1
ZBarSDK is the only place I've ever seen NSFastEnumeration
. I don't know a whole lot about it, nor do I know exactly why ZBarSDK was designed to be used in this way.
NSFastEnumeration
isn't any faster than using a forin
loop. It is faster than a regular for
or while
(or do-while
) loop, but not faster than a forin
.
In a forin
loop, we're using regular Objective-C collections, and NSArray
has a firstObject
and lastObject
.
But if the real question is (and should be) how do I improve this code (more than just specifically grabbing a single object out of an NSFastEnumeration
), then the answer is to use AVCaptureMetadataOutput (official documentation), which was introduced in iOS7.
ZBarSDK has an iOS7 memory leak. Also, I don't know about compiled on-device size, but when I removed it from my project, it saved about 1.4mb from the project size.
Meanwhile, AVCaptureMetadataOutput
has many advantages over ZBarSDK
.
- It's slightly easier to use... and it's simple to use if you're already familiar with video capture in iOS.
- It leaves a smaller footprint in terms of storage space your app takes up on a device.
- Even without ZBarSDK's memory leak,
AVCaptureMetadataOutput
has a smaller memory footprint. AVCaptureMetadataOutput
seems to scan barcodes faster and a farther distances than what I could manage withZBarSDK
.- ZBarSDK doesn't support 64-bit processors at all.
AVCaptureMetadataOutput
however does.
-
\$\begingroup\$ For an example on using
AVCaptureMetadataOutput
: codereview.stackexchange.com/questions/42252/… \$\endgroup\$nhgrif– nhgrif2014年02月20日 12:24:42 +00:00Commented Feb 20, 2014 at 12:24
break;
command in the loop, then it selects the first symbol. \$\endgroup\$