Fondamentalement, j'ai une URL Youtube en tant que chaîne, je veux extraire l'ID vidéo de cette URL. J'ai trouvé un peu de code dans c objectif qui est comme ci-dessous:Youtube Video Id de l'URL - Swift3
NSError *error = NULL;
NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:@"?.*v=([^&]+)"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:youtubeURL
options:0
range:NSMakeRange(0, [youtubeURL length])];
if (match) {
NSRange videoIDRange = [match rangeAtIndex:1];
NSString *substringForFirstMatch = [youtubeURL substringWithRange:videoIDRange];
}
Quand je suis en train de convertir ce code swift3 qui est:
var error: Error? = nil
var regex = try! NSRegularExpression(pattern: "?.*v=([^&]+)", options: .caseInsensitive)
var match = regex!.firstMatch(in: youtubeURL, options: [], range: NSRange(location: 0, length: youtubeURL.length))!
if match {
var videoIDRange = match.rangeAt(1)
var substringForFirstMatch = (youtubeURL as NSString).substring(with: videoIDRange)
}
Donne erreur:
fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=2048 "The value “?.*v=([^&]+)” is invalid."
Quelqu'un peut-il aidez-moi à propos de cette erreur ou n'importe qui explique comment obtenir l'ID vidéo de l'URL dans Swift 3.
Merci d'avance
seule ligne 'URLComponents de retour (chaîne: youtubeUrl) ?. queryItems .first (où: {0.name $ == "v"})? ?. value' –
Bon point. Mettra à jour mon exemple. – totiG
Remerciements ...... –