变量后的转义循环不为零

时间:2015-01-16 06:42:41

标签: objective-c cocoa-touch

我希望我的条形码扫描应用程序在找到结果后停止扫描。这是我的代码:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:        (AVCaptureConnection *)connection
{
CGRect highlightViewRect = CGRectZero;
AVMetadataMachineReadableCodeObject *barCodeObject;
NSString *detectionString = nil;
NSArray *barCodeTypes = @[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code,
        AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code,
        AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode];

for (AVMetadataObject *metadata in metadataObjects) {
    for (NSString *type in barCodeTypes) {
        if ([metadata.type isEqualToString:type])
        {
            barCodeObject = (AVMetadataMachineReadableCodeObject *)[_prevLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata];
            highlightViewRect = barCodeObject.bounds;
            detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
            break;
        }
    }

    if (detectionString != nil)
    {

        NSString *connect = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", @"http://example.com/index.php?b=", detectionString]] encoding:NSUTF8StringEncoding error:nil];

        _label.text = connect;

        break;
    }
    else
        _label.text = @"Scan Barcode";
}

_highlightView.frame = highlightViewRect;

}

我基本上想通过NSURL将条形码发送到我的服务器,如下所示。虽然目前它不断发送多个请求。我只希望它发送一次NSURL然后停止。

2 个答案:

答案 0 :(得分:0)

你应该将NSString *detectionString = nil;移到函数之外,这样每次调用方法时它都不会被重置,并且在for循环之前进行检查,如果你已经设置了detectString,则不要进入它/ p>

答案 1 :(得分:0)

所以把你的detectString变量放在你的函数之外。否则,每次调用该函数时,一次又一次地声明detectString。

@interface YourClassName ()
@property (strong, nonatomic) NSString *detectionString;
@end

- (void)viewDidLoad{
   self.detectionString = nil;
}