Rlocation pour ssortingngByAddingPercentEscapesUsingEncoding dans ios9?

Dans iOS8 et avant, je peux utiliser:

NSSsortingng *str = ...; // some URL NSSsortingng *result = [str ssortingngByAddingPercentEscapesUsingEncoding:NSUTF8SsortingngEncoding]; 

dans iOS9 ssortingngByAddingPercentEscapesUsingEncoding a été remplacé par ssortingngByAddingPercentEncodingWithAllowedCharacters :

 NSSsortingng *str = ...; // some URL NSCharacterSet *set = ???; // where to find set for NSUTF8SsortingngEncoding? NSSsortingng *result = [str ssortingngByAddingPercentEncodingWithAllowedCharacters:set]; 

et ma question est: où find NSCharacterSet nécessaire ( NSUTF8SsortingngEncoding ) pour le rlocation approprié de ssortingngByAddingPercentEscapesUsingEncoding ?

    Le message de dépréciation dit (emphase mienne):

    Utilisez à la place ssortingngByAddingPercentEncodingWithAllowedCharacters (_ :), qui utilise toujours le encoding UTF-8 recommandé , et qui code pour un composant ou sous-composant d'URL spécifique puisque chaque composant ou sous-composant d'URL a des règles différentes pour les caractères valides.

    Vous n'avez donc besoin de fournir qu'un NSCharacterSet adéquat en tant qu'argument. Heureusement, pour les URL, il existe une méthode de class très pratique appelée URLHostAllowedCharacterSet que vous pouvez utiliser comme ceci:

     let encodedHost = unencodedHost.ssortingngByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet()) 

    Mise à jour pour Swift 3 – la méthode devient la propriété statique urlHostAllowed :

     let encodedHost = unencodedHost.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) 

    Sachez cependant que:

    Cette méthode est destinée à coder en pourcentage un composant d'URL ou une string de sous-composants, PAS une string d'URL complète.

    Pour Objective-C:

     NSSsortingng *str = ...; // some URL NSCharacterSet *set = [NSCharacterSet URLHostAllowedCharacterSet]; NSSsortingng *result = [str ssortingngByAddingPercentEncodingWithAllowedCharacters:set]; 

    où find set pour NSUTF8SsortingngEncoding?

    Il existe des jeux de caractères prédéfinis pour les six composants et sous-composants d'URL qui permettent le encoding en pourcentage. Ces jeux de caractères sont passés à -ssortingngByAddingPercentEncodingWithAllowedCharacters:

      // Predefined character sets for the six URL components and subcomponents which allow percent encoding. These character sets are passed to -ssortingngByAddingPercentEncodingWithAllowedCharacters:. @interface NSCharacterSet (NSURLUtilities) + (NSCharacterSet *)URLUserAllowedCharacterSet; + (NSCharacterSet *)URLPasswordAllowedCharacterSet; + (NSCharacterSet *)URLHostAllowedCharacterSet; + (NSCharacterSet *)URLPathAllowedCharacterSet; + (NSCharacterSet *)URLQueryAllowedCharacterSet; + (NSCharacterSet *)URLFragmentAllowedCharacterSet; @end 

    Le message de dépréciation dit (emphase mienne):

    Utilisez à la place ssortingngByAddingPercentEncodingWithAllowedCharacters (_ :), qui utilise toujours le encoding UTF-8 recommandé , et qui code pour un composant ou sous-composant d'URL spécifique puisque chaque composant ou sous-composant d'URL a des règles différentes pour les caractères valides.

    Vous n'avez donc besoin de fournir qu'un NSCharacterSet adéquat en tant qu'argument. Heureusement, pour les URL, il existe une méthode de class très pratique appelée URLHostAllowedCharacterSet que vous pouvez utiliser comme ceci:

     NSCharacterSet *set = [NSCharacterSet URLHostAllowedCharacterSet]; 

    Sachez cependant que:

    Cette méthode est destinée à coder en pourcentage un composant d'URL ou une string de sous-composants, PAS une string d'URL complète.

    URLHostAllowedCharacterSet NE FONCTIONNE PAS POUR MOI. J'utilise URLFragmentAllowedCharacterSet place.

    OBJECTIF C

     NSCharacterSet *set = [NSCharacterSet URLFragmentAllowedCharacterSet]; NSSsortingng * encodedSsortingng = [@"url ssortingng" ssortingngByAddingPercentEncodingWithAllowedCharacters:set]; 

    Voici des jeux de caractères utiles (inversés):

     URLFragmentAllowedCharacterSet "#%<>[\]^`{|} URLHostAllowedCharacterSet "#%/<>?@\^`{|} URLPasswordAllowedCharacterSet "#%/:<>?@[\]^`{|} URLPathAllowedCharacterSet "#%;<>?[\]^`{|} URLQueryAllowedCharacterSet "#%<>[\]^`{|} URLUserAllowedCharacterSet "#%/:<>?@[\]^` 

    Objectif c

    ce code fonctionne pour moi:

     urlSsortingng = [urlSsortingng ssortingngByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]; 

    Swift 2.2:

     extension Ssortingng { func encodeUTF8() -> Ssortingng? { //If I can create an NSURL out of the ssortingng nothing is wrong with it if let _ = NSURL(ssortingng: self) { return self } //Get the last component from the ssortingng this will return subSequence let optionalLastComponent = self.characters.split { $0 == "/" }.last if let lastComponent = optionalLastComponent { //Get the ssortingng from the sub sequence by mapping the characters to [Ssortingng] then reduce the array to Ssortingng let lastComponentAsSsortingng = lastComponent.map { Ssortingng($0) }.reduce("", combine: +) //Get the range of the last component if let rangeOfLastComponent = self.rangeOfSsortingng(lastComponentAsSsortingng) { //Get the ssortingng without its last component let ssortingngWithoutLastComponent = self.subssortingngToIndex(rangeOfLastComponent.startIndex) //Encode the last component if let lastComponentEncoded = lastComponentAsSsortingng.ssortingngByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.alphanumericCharacterSet()) { //Finally append the original ssortingng (without its last component) to the encoded part (encoded last component) let encodedSsortingng = ssortingngWithoutLastComponent + lastComponentEncoded //Return the ssortingng (original ssortingng/encoded ssortingng) return encodedSsortingng } } } return nil; } } 

    Pour Swift 3.0

    Vous pouvez utiliser le urlHostAllowed caractères urlHostAllowed .

    /// Renvoie le jeu de caractères pour les caractères autorisés dans un sous-composant d'URL hôte.

     public static var urlHostAllowed: CharacterSet { get } WebserviceCalls.getParamValueSsortingngForURLFromDictionary(settingsDict as! Dictionary<Ssortingng, AnyObject>).addingPercentEncoding(withAllowedCharacters: CharacterSet.urlHostAllowed) 

    Quelle est la signification de "Cette méthode est destinée à coder en pourcentage un composant d'URL ou une string de sous-composants, PAS une string d'URL complète." ? – GeneCode le 1er septembre 16 à 8h30

    Cela signifie que vous n'êtes pas censé encoder le https://xpto.example.com/path/subpath de l'URL, mais seulement ce qui se passe après le ? .

    Supposé, parce qu'il y a des cas d'utilisation pour le faire dans des cas comme:

     https://example.com?redirectme=xxxxx 

    xxxxx est une URL entièrement codée.