0

I'm creating an iphone app using swift 2. I got the app to be able to read barcodes and display the barcode. Also I learned core data and using it to import a .csv file with a bunch of product information which includes the barcode. Now when a barcode is scanned, I want it to search through the file and display the information relating to it. Here are parts of my code, how will I go about doing this? To be honest, I have programmers block right now and had for the past two days. Any help or guidance will be appreciated.

Here is the CSV Parser:

func parseCSV (contentsOfURL: NSURL, encoding: NSStringEncoding) -> [(title:String, price:String, weight:String, box_length:String, box_width:String, box_height:String, sku:String, barcodeNum:String )]? {

        // Load the CSV file and parse it
        let delimiter = ","
        var items:[(title:String, price:String, weight:String, box_length:String, box_width:String, box_height:String, sku:String, barcodeNum:String )]?

        do {
            let content = try String(contentsOfURL: contentsOfURL, encoding: encoding)
            print(content)
            items = []
            let lines:[String] = content.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet()) as [String]

            for line in lines {
                var values:[String] = []
                if line != "" {
                    // For a line with double quotes
                    // we use NSScanner to perform the parsing
                    if line.rangeOfString("\"") != nil {
                        var textToScan:String = line
                        var value:NSString?
                        var textScanner:NSScanner = NSScanner(string: textToScan)
                        while textScanner.string != "" {

                            if (textScanner.string as NSString).substringToIndex(1) == "\"" {
                                textScanner.scanLocation += 1
                                textScanner.scanUpToString("\"", intoString: &value)
                                textScanner.scanLocation += 1
                            } else {
                                textScanner.scanUpToString(delimiter, intoString: &value)
                            }

                            // Store the value into the values array
                            values.append(value as! String)

                            // Retrieve the unscanned remainder of the string
                            if textScanner.scanLocation < textScanner.string.characters.count {
                                textToScan = (textScanner.string as NSString).substringFromIndex(textScanner.scanLocation + 1)
                            } else {
                                textToScan = ""
                            }
                            textScanner = NSScanner(string: textToScan)
                        }

                        // For a line without double quotes, we can simply separate the string
                        // by using the delimiter (e.g. comma)
                    } else  {
                        values = line.componentsSeparatedByString(delimiter)
                    }

                    // Put the values into the tuple and add it to the items array
                    //(title:String, price:String, weight:String, box_length:String, box_width:String, box_height:String, sku:String, barcodeNum:String )
                    let item = (title: values[0], sku: values[1], price: values[2], weight: values[3], box_length: values[4], box_width: values[5], box_height: values[6], barcodeNum: values[7])
                    items?.append(item)
                }
            }

        } catch {
            print(error)
        }

        return items
    }

When Barcode is detected:

 func barcodeDetected(code: String) {
        print(code)
        if(barcodeDelegate != nil){
            barcodeDelegate!.barcodeFound(code)
        }

        // Let the user know we've found something.

        let alert = UIAlertController(title: "Found a Barcode!", message: code, preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "Search", style: UIAlertActionStyle.Destructive, handler: { action in

            // Remove the spaces.

            let trimmedCode = code.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())

            // EAN or UPC?
            // Check for added "0" at beginning of code.

            let trimmedCodeString = "\(trimmedCode)"
            var trimmedCodeNoZero: String

            if trimmedCodeString.hasPrefix("0") && trimmedCodeString.characters.count > 1 {
                trimmedCodeNoZero = String(trimmedCodeString.characters.dropFirst())

                // Send the doctored UPC to DataService.searchAPI()

                DataService.searchCode(trimmedCodeNoZero)
            } else {

                // Send the doctored EAN to DataService.searchAPI()

                DataService.searchCode(trimmedCodeString)
            }

            self.navigationController?.popViewControllerAnimated(true)
        }))

        self.presentViewController(alert, animated: true, completion: nil)
}

Dataservice.swift file

import Foundation

class DataService {

    static let dataService = DataService()

    static func searchCode(codeNumber: String) {

    }


}

I can post more of my project if it helps, but I feel like this is already too much information, anything will help at this point on how to tackle this.

David_A
  • 13
  • 3
  • What is your specific problem? It isn't clear. I can suggest that creating a struct or class to represent each line in your CSV and returning an array of these is a better approach than returning an array of tuples. – Paulw11 Jun 16 '16 at 04:09
  • After the barcode is detected it's being passed to a Dataservice class. So from my parseCSV function, how would I be able to get that specific tuple with the barcode and the item arrays in my dataservice class? – David_A Jun 17 '16 at 20:30
  • Since you have an array of tuples, your only option is to iterate over the array and compare each element for a potential match. you could build a dictionary that has the barcode string value as the key and the rest of the data as the value. Then you could access the data directly using the barcode as the key – Paulw11 Jun 17 '16 at 22:14
  • @Paulw11 Oh thanks! I did not think of that to be honest, i'm fairly new to ios development. Reading tutorials as I go. Thanks! – David_A Jun 17 '16 at 22:45

0 Answers0