0

I'm new in Swift Programming. I have a question about Swift String. I have a TableView which show data from JSON. Below the snippet :

cell.labelFOStat.text = ("F/O Status   : \(subString(string: featureStr!, startIndex: 0, endIndex: 11))")

    cell.labelHKStat.text = ("H/K Status   : \(subString(string: featureStr!, startIndex: 12, endIndex: 23))" )

    cell.labelID.text = ("ID       : \(subString(string: featureStr!, startIndex: 24, endIndex: 25))")

    cell.labelTime.text = ("Time     : \(subString(string: featureStr!, startIndex: 26, endIndex: 30))")

    cell.labelExplanation.text = ("Explanation : \(subString(string: featureStr!, startIndex: 31, endIndex: 62))")

    cell.labelFOAdult.text = ("F/O Adult : \(subString(string: featureStr!, startIndex: 63, endIndex: 64))")

    cell.labelFOChild.text = ("F/O Child : \(subString(string: featureStr!, startIndex: 65, endIndex: 66))")

    cell.labelHKAdult.text = ("H/K Adult : \(subString(string: featureStr!, startIndex: 67, endIndex: 68))")

    cell.labelHKChild.text = ("H/K Child : \(subString(string: featureStr!, startIndex: 69, endIndex: (featureStr?.characters.count)! - 1))")

Where substring is :

func subString(string:String, startIndex: Int, endIndex: Int) -> String {
    let index = string.index((string.startIndex), offsetBy: startIndex)
    let endIndex = string.index((string.startIndex), offsetBy: endIndex)

    let t:String?

    let str = string[index...endIndex]

    return str
}

This is snippet of my JSON :

[{
  "zinr": "1006",
  "features": "Occupied    Sleep Out   0115:15asd                             0 0 0 0",
  "etage": 10,
  "bezeich": "Deluxe King",
  "house-status": 1,
  "zistatus": 4,
  "userinit": "01",
  "nr": 1
}, {
  "zinr": "709",
  "features": "Vacant      Occupied    0112:46tes                             0 0 0 0",
  "etage": 7,
  "bezeich": "Superior King",
  "house-status": 1,
  "zistatus": 0,
  "userinit": "01",
  "nr": 1
}]

A want to know, how to use the String range to get key "features". Its possible to only make range parameter without start or end index? . I've read this but its still make me little confuse. Thanks in Advance .

Community
  • 1
  • 1
MrX
  • 953
  • 2
  • 16
  • 42
  • Are you trying to split the features string into three different values? – Dave Weston Apr 28 '17 at 02:26
  • yes but its not three, its 9 value. please see my code in `tableview` above. I read some question related `String` swift and its always using start and end index. I want to know how to split it if using range. Let say i want get `Occupied` so i say the range is 11, and countinous same until the last letter. its possible? – MrX Apr 28 '17 at 02:33
  • I'm not sure what you're asking. Does your code above work and you're trying to find a simpler way to do it? – Dave Weston Apr 28 '17 at 02:35
  • My function its work, you can try it. Yes i want make my function more simple with two, the `string: String` and parameters `range: ...??` . But i still not familiar with String in Swift. I just wanna know how to get/split the `feature` key if using only with `range` . – MrX Apr 28 '17 at 02:39
  • http://stackoverflow.com/a/38215613/2303865 – Leo Dabus Apr 28 '17 at 03:14
  • Your substring function is not likely to work in the general case of international text. You should take into account composed characters sequences. You should be able to use the [str substringWithRange:[str rangeOfComposedCharacterSequencesForRange:range]] from NSString to extract a substring. – Scott Thompson Apr 28 '17 at 04:41

1 Answers1

2

The most convenient (though not most efficient) way is to make an extension to String:

extension String {
    subscript(range: CountableRange<Int>) -> String {
        let startIndex = self.index(self.startIndex, offsetBy: range.lowerBound)
        let endIndex = self.index(self.startIndex, offsetBy: range.upperBound)
        return self[startIndex..<endIndex]
    }

    subscript(range: CountableClosedRange<Int>) -> String {
        let startIndex = self.index(self.startIndex, offsetBy: range.lowerBound)
        let endIndex = self.index(self.startIndex, offsetBy: range.upperBound)
        return self[startIndex...endIndex]
    }
}

// Both print Hello
let str = "Hello world"
print(str[0..<5])
print(str[0...4])
Code Different
  • 90,614
  • 16
  • 144
  • 163
  • This way you are unnecessarily offsetting your string endIndex from the beginning when you could be doing from the subrange startIndex – Leo Dabus Apr 28 '17 at 05:45