2016-05-05 73 views

回答

1

使用字符串擴展:

extension String{ 

    func formatDurationsFromYoutubeAPItoNormalTime (targetString : String) ->String{ 

     var timeDuration : NSString! 
     let string: NSString = targetString 

     if string.rangeOfString("H").location == NSNotFound && string.rangeOfString("M").location == NSNotFound{ 

      if string.rangeOfString("S").location == NSNotFound { 
       timeDuration = NSString(format: "00:00") 
      } else { 
       var secs: NSString = targetString 
       secs = secs.substringFromIndex(secs.rangeOfString("PT").location + "PT".characters.count) 
       secs = secs.substringToIndex(secs.rangeOfString("S").location) 

       timeDuration = NSString(format: "00:%02d", secs.integerValue) 
      } 
     } 
     else if string.rangeOfString("H").location == NSNotFound { 
      var mins: NSString = targetString 
      mins = mins.substringFromIndex(mins.rangeOfString("PT").location + "PT".characters.count) 
      mins = mins.substringToIndex(mins.rangeOfString("M").location) 

      if string.rangeOfString("S").location == NSNotFound { 
       timeDuration = NSString(format: "%02d:00", mins.integerValue) 
      } else { 
       var secs: NSString = targetString 
       secs = secs.substringFromIndex(secs.rangeOfString("M").location + "M".characters.count) 
       secs = secs.substringToIndex(secs.rangeOfString("S").location) 

       timeDuration = NSString(format: "%02d:%02d", mins.integerValue, secs.integerValue) 
      } 
     } else { 
      var hours: NSString = targetString 
      hours = hours.substringFromIndex(hours.rangeOfString("PT").location + "PT".characters.count) 
      hours = hours.substringToIndex(hours.rangeOfString("H").location) 

      if string.rangeOfString("M").location == NSNotFound && string.rangeOfString("S").location == NSNotFound { 
       timeDuration = NSString(format: "%02d:00:00", hours.integerValue) 
      } else if string.rangeOfString("M").location == NSNotFound { 
       var secs: NSString = targetString 
       secs = secs.substringFromIndex(secs.rangeOfString("H").location + "H".characters.count) 
       secs = secs.substringToIndex(secs.rangeOfString("S").location) 

       timeDuration = NSString(format: "%02d:00:%02d", hours.integerValue, secs.integerValue) 
      } else if string.rangeOfString("S").location == NSNotFound { 
       var mins: NSString = targetString 
       mins = mins.substringFromIndex(mins.rangeOfString("H").location + "H".characters.count) 
       mins = mins.substringToIndex(mins.rangeOfString("M").location) 

       timeDuration = NSString(format: "%02d:%02d:00", hours.integerValue, mins.integerValue) 
      } else { 
       var secs: NSString = targetString 
       secs = secs.substringFromIndex(secs.rangeOfString("M").location + "M".characters.count) 
       secs = secs.substringToIndex(secs.rangeOfString("S").location) 
       var mins: NSString = targetString 
       mins = mins.substringFromIndex(mins.rangeOfString("H").location + "H".characters.count) 
       mins = mins.substringToIndex(mins.rangeOfString("M").location) 

       timeDuration = NSString(format: "%02d:%02d:%02d", hours.integerValue, mins.integerValue, secs.integerValue) 
      } 
     } 
     return timeDuration as String 
    } 

} 

用法:

override func viewWillAppear(animated: Bool) { 

let youtubeVideoDurationString = "PT15M51S" 

let resultString = youtubeVideoDurationString.formatDurationsFromYoutubeAPItoNormalTime(youtubeVideoDurationString) 
    print(resultString) 

} 
0

轉換小時:分鐘:秒格式: -

使用字符串擴展:

extension String{ 

func parseVideoDurationOfYoutubeAPI(videoDuration: String?) -> String { 

var videoDurationString = videoDuration! as NSString 

var hours: Int = 0 
var minutes: Int = 0 
var seconds: Int = 0 
let timeRange = videoDurationString.rangeOfString("T") 

videoDurationString = videoDurationString.substringFromIndex(timeRange.location) 
while videoDurationString.length > 1 { 

    videoDurationString = videoDurationString.substringFromIndex(1) 

    let scanner = NSScanner(string: videoDurationString as String) as NSScanner 
    var part: NSString? 

    scanner.scanCharactersFromSet(NSCharacterSet.decimalDigitCharacterSet(), intoString: &part) 

    let partRange: NSRange = videoDurationString.rangeOfString(part! as String) 

    videoDurationString = videoDurationString.substringFromIndex(partRange.location + partRange.length) 
    let timeUnit: String = videoDurationString.substringToIndex(1) 


    if (timeUnit == "H") { 
     hours = Int(part as! String)! 
    } 
    else if (timeUnit == "M") { 
     minutes = Int(part as! String)! 
    } 
    else if (timeUnit == "S") { 
     seconds = Int(part! as String)! 
    } 
    else{ 
    } 

} 
return String(format: "%02d:%02d:%02d", hours, minutes, seconds) 

} }

用法: 倍率FUNC viewWillAppear中(動畫:BOOL){

let youtubeVideoDurationString = "PT15M51S" 

    let result_Hour_Minute_Second = youtubeVideoDurationString.parseVideoDurationOfYoutubeAPI("PT15M51S") as String 

    print("result_Hour_Minute_Second: \(result_Hour_Minute_Second)") 

} 
2

更簡單的實現考慮到在HH返回值:MM:僅ss格式。

extension String { 

    func getYoutubeFormattedDuration() -> String { 

     let formattedDuration = self.stringByReplacingOccurrencesOfString("PT", withString: "").stringByReplacingOccurrencesOfString("H", withString: ":").stringByReplacingOccurrencesOfString("M", withString: ":").stringByReplacingOccurrencesOfString("S", withString: "") 

     let components = formattedDuration.componentsSeparatedByString(":") 
     var duration = "" 
     for component in components { 
      duration = duration.characters.count > 0 ? duration + ":" : duration 
      if component.characters.count < 2 { 
       duration += "0" + component 
       continue 
      } 
      duration += component 
     } 

     return duration 

    } 

} 

**斯威夫特3

func getYoutubeFormattedDuration() -> String { 

    let formattedDuration = self.replacingOccurrences(of: "PT", with: "").replacingOccurrences(of: "H", with:":").replacingOccurrences(of: "M", with: ":").replacingOccurrences(of: "S", with: "") 

    let components = formattedDuration.components(separatedBy: ":") 
    var duration = "" 
    for component in components { 
     duration = duration.characters.count > 0 ? duration + ":" : duration 
     if component.characters.count < 2 { 
      duration += "0" + component 
      continue 
     } 
     duration += component 
    } 

    return duration 

} 

樣品結果:

"PT3H2M31S".getYoutubeFormattedDuration() //returns "03:02:31" 
"PT2M31S".getYoutubeFormattedDuration() //returns "02:31" 
"PT31S".getYoutubeFormattedDuration() //returns "31" 
相關問題