Home Make categories from string in XML data
Reply: 1

Make categories from string in XML data

C M
1#
C M Published in 2017-12-07 21:54:12Z

I looked everywhere but I don't see anything specific to the thing I am trying to achieve, especially with iOS/swift. I would like to make age categories from the XML parser request I am calling in my app.

The age information will have to be called from the tag in the xml data:

<item>
<description>ANTHONY JOHNSON, Age: 17 From: PEORIA, IL</description></item>

I already have a call to get the description tag in full:

func parser(_ parser: XMLParser, didEndElement elementName: String, 
 namespaceURI: String?, qualifiedName qName: String?) { 

 if elementName == "description" {
        self.person.desc = self.foundCharacters; }

     if elementName == "item" {
tempData.desc = self.person.desc; }

Item class:

 class Item { 
  var desc = "";
}

I just need to make categories from the age value only. I plan on using them with an enum:

enum ageCategories: String {

    case ageUnder10 = "Age 10 and under"
    case age10Plus = "Age 10-15"
    case age15Plus = "Age 15+"
}

My questions are:

  • Do I make a call in the XML parser to get the ages? How would I organize the call to get the ages part of the description string only and put them in categories?

OR

  • Is there an easier way to call just the ages from the string I have already called in the XML parser and then make categories with that? i.e: replacingOccurrences(range searchRange:"Age")
rmaddy
2#
rmaddy Reply to 2017-12-07 22:27:20Z

Your question boils down to finding an age in the description string. The following shows one solution using Scanner. This code assumes that the age will be a number after the text "Age:".

let desc = "ANTHONY JOHNSON, Age: 17 From: PEORIA, IL"
var ageRange: ageCategories? = nil

let scanner = Scanner(string: desc)
if scanner.scanUpTo("Age:", into: nil) && scanner.scanString("Age:", into: nil) {
    var age = 0
    if scanner.scanInt(&age) {
        switch age {
        case 0..<10:
            ageRange = .AgeUnder10
        case 10..<15:
            ageRange = .Age10Plus
        case 15...:
            ageRange = .Age15Plus
        default:
            break
        }
    }
}

if let ageRange = ageRange {
    print("Found age range of \(ageRange)")
} else {
    print("No valid age found")
}

Adjust the ranges in the case statements to match your actual needs. The strings you are assigning to your enum values are ambiguous. Is 10 supposed to be .AgeUnder10 or .Age10Plus?

FYI - enum values should start with lowercase letters and the enum name should start with uppercase. So your enum should be:

enum AgeCategories: String {
    case ageUnder10 = "Age under 10"
    case age10Plus = "Age 10-14"
    case age15Plus = "Age 15+"
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.328994 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO