# Documentation for iOS¶

## Usage Sample¶

The following code shows typical usage of the parser library.

#import <ScanditParser/ScanditParser.h>

...

NSError *err;
// Replace SBSDataFormatHIBC with the data format of your choice.
// picker: SBSBarcodePicker object from Scandit SDK
SBSParser *parser = [picker parserForFormat:SBSParserDataFormatHIBC error:&err];
if (!parser) {
}

SBSParserResult *result = [parser parseString:@"+A99912345/52001510X33" error:&err];
if (!result) {
}

// We can either access the data from the raw JSON string...
NSLog(@"%@", result.jsonString);

// ... or from the fields dictionary...
NSDictionary<NSString*, SBSField*> *fieldsDictionary = result.fieldsDict;
for (NSString *key in fieldsDictionary.allKeys) {
SBSField *field = [fieldsDictionary objectForKey:key];
NSLog(@"%@ name: %@", key, field.name);
NSLog(@"%@ raw: %@", key, field.rawString);
NSLog(@"%@ parsed: %@", key, field.parsed);
}

// ... or from the fields array
for (SBSField* field in result.fieldsArray) {
NSLog(@"%@ raw: %@", field.name, field.rawString);
NSLog(@"%@ parsed: %@", field.name, field.parsed);
}


Some data formats allow for special parsing options which modify the behaviour of the parser. These can be set in the following way:

NSError *err;
NSDictionary *opts = @{
};
[parser setOptions:opts error:&err];


## iOS API¶

class SBSParser

Defines the interface for a data string parser. Parsers are capable of parsing one particular data format, which is passed to them during construction.

parseString:(NSString *)string error:(NSError **)outError;

Parses the data string and returns the contained fields in the result object. In case the result could not be parsed, the error message is accessible as part of the outError parameter.

Returns: The result object. Before accessing the fields of the result, you must ensure that the string was correctly parsed, that is, outError has not been set. string – The string to parse. Must not be nil. outError – Upon failure will be set to an instance of NSError containing details on why the data could not be parsed. Can be nil. SBSParserResult*
parseRawData:(NSString *)rawData error:(NSError **)outError;

Parses the raw data of the code and returns the contained fields in the result object. In case the result could not be parsed, the error message is accessible as part of the outError parameter.

Returns: The result object. Before accessing the fields of the result, you must ensure that the string was correctly parsed, that is, outError has not been set. rawData – The raw data of the string to parse. Must not be nil. outError – Upon failure will be set to an instance of NSError containing details on why the data could not be parsed. Can be nil. SBSParserResult*
setOptions:(NSDictionary *)opts error:(NSError **)outError;

Apply the option map to the parser, allowing the user to fine-tune the behaviour of the parser. Available options depend on the data format and are specified in the respective documentation. If the case that the options object is invalid and the operation fails, the error message is accessible as part of the outError parameter.

Parameters: opts – The options dictionary. Must not be nil. outError – Upon failure will be set to an instance of NSError containing details on why the operation failed. Can be nil. void
class SBSParserResult

The SBSParserResult holds the result of a successfully parsed data string. The result is a collection of fields that can either be accessed as an sequential array (fieldsArray), or a dictionary (fieldsDict) which maps field name onto values.

json

The JSON string of the result content.

Type: NSString*
fieldsDict

Dictionary of SBSParserField objects with key being the field name.

Type: NSDictionary*
fieldsArray

Array of SBSParserField objects with key being the field name. Use this accessor if the order of the fields in the string matter, if you are only interested in a particular field, using the SBSParserField.fieldsDict is simpler.

Type: NSArray*
class SBSParserField

A particular field of the parsed result.

name

The name of the field.

Type: NSString*

This attribute corresponds to the JSON “name” field.

rawString

Raw substring of the original code containing the field data. For fields that are inferred and do not have a direct correspondence to a particular part of the string, the string is set to an empty string. An example of such a field is the Metadata field field of HIBC codes.

Type: NSString*

This attribute corresponds to the JSON “rawString” field.

parsed

The parsed representation of the data contained in this field. If no parsed representation is available for the field, this property is nil. Use SBSParserField.rawString to retrieve the data for these fields.

Type: id. Depending on the field, this may be an instance of NSNumber, NSDictionary, NSArray, or NSString.

This attribute corresponds to the JSON “parsed” field.