1

i have to generate the range of string and get the startLocation of sub string and the last location of sub string. i have converted the swift 2 code into swift 4 and i am getting error.

Value of type 'Range?' (aka 'Optional>') has no member 'location'

so how can i get the location in swift 5?

let innerData = data.subdata(with: innerRange)
let headerRange = innerData.range(of: "\r\n\r\n".data(using: .utf8)!, options: NSData.SearchOptions())
let startLocation = headerRange.location + headerRange.length
golu_kumar
  • 231
  • 2
  • 9
  • 2
    headerRange is an optional. Did you try unwrap it first ? – claude31 Sep 08 '19 at 18:52
  • @claude31 `headerRange` doesn't have a `location` property. It's of type `Range?`, not `NSRange`. – rmaddy Sep 08 '19 at 18:56
  • You may find this answer more than sufficient for your issue: [enter link description here](https://stackoverflow.com/questions/39677330/how-does-string-substring-work-in-swift) – Obi Sep 08 '19 at 19:24
  • @claude31 i have tried after force unwrapping headerRange but it didn't work, actually i don.t know why "location" is used there. I got this code from github and i had to convert it into swift 4. – golu_kumar Sep 09 '19 at 16:55

0 Answers0