Skip to content

Instantly share code, notes, and snippets.

@thomaspockrandt
Created December 4, 2015 19:28
Show Gist options
  • Save thomaspockrandt/6febc9ae8ee95c82f8f8 to your computer and use it in GitHub Desktop.
Save thomaspockrandt/6febc9ae8ee95c82f8f8 to your computer and use it in GitHub Desktop.
Simple Swift M3U Parser
if
let path = NSBundle.mainBundle().pathForResource("tvhd", ofType: "m3u"),
let content = try? String(contentsOfFile: path, encoding: NSUTF8StringEncoding)
{
let lines = content.componentsSeparatedByCharactersInSet(.newlineCharacterSet())
var streams = [String: NSURL]()
var streamName: String?
for line in lines {
if line.containsString("#") {
if let match = line.rangeOfString("^#EXTINF:-1,", options: .RegularExpressionSearch) {
streamName = line.substringWithRange(Range<String.Index>(start: match.endIndex, end: line.endIndex))
}
} else if !line.isEmpty {
if
let _streamName = streamName,
let url = NSURL(string: line)
{
streams[_streamName] = url
}
}
}
return streams
} else {
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment