title: How to encode only all special characters in PercentEncoding is swift 5
body: <p>I am running the code from similar questions from stackoverflow to percentage encode only  all the special characters(@!#$%^&amp;*()~-_=+;:'/.,&lt;&gt;{}[])(not encoding alphanumeric characters) on xcode playground . Here are my attempts</p>
  <pre><code>          import UIKit
      
      //////////////
      //Attempt 1
      var originalString = &quot;Test@123!#$%^&amp;*()~-_=+;:'/.,&lt;&gt;{}[]&quot;
      
      let escapedString = originalString.addingPercentEncoding(withAllowedCharacters:NSCharacterSet.urlQueryAllowed)
      
      print(&quot;Attemp 1:&quot;,escapedString)
      ///////////////////
      //Attempt 2
      func encodeValue(_ string: String) -&gt; String? {
          guard let unescapedString = string.addingPercentEncoding(withAllowedCharacters: CharacterSet(charactersIn: &quot;:/&quot;).inverted) else { return nil }
      
          return unescapedString
      }
      
      let encodedString = encodeValue(&quot;Test@123!#$%^&amp;*()~-_=+;:'/.,&lt;&gt;{}[]&quot;)
      print(&quot;Attemp 2:&quot;,encodedString)
      //////////////////////
      //Attempt 3
      extension String {
          var encoded: String? {
              return self.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
          }
      }
      
      
      let encodedUrl = &quot;Test@123!#$%^&amp;*()~-_=+;:'/.,&lt;&gt;{}[]&quot;.encoded
      print(&quot;Attemp 3:&quot;,encodedUrl)
          //////////////
      //Attempt 4
      let myurlstring = &quot;Test@123!#$%^&amp;*()~-_=+;:'/.,&lt;&gt;{}[]&quot;
      let urlwithPercentEscapes = myurlstring.addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed)
      print(&quot;Attemp 4:&quot;,urlwithPercentEscapes)
  
  //////////////
  //Attempt 5
  var s = &quot;Test@123!#$%^&amp;*()~-_=+;:'/.,&lt;&gt;{}[]&quot;
  let encodedLink = s.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)
  let encodedURL = NSURL(string: encodedLink!)! as URL
  print(&quot;Attemp 5:&quot;,encodedURL)
  </code></pre>
  <p>Here are the results</p>
  <blockquote>
  <p>Attemp 1:
  Optional(&quot;Test@123!%23$%25%5E&amp;*()~-_=+;:'/.,%3C%3E%7B%7D%5B%5D&quot;)</p>
  <p>Attemp 2: Optional(&quot;Test@123!#$%^&amp;*()~-_=+;%3A'%2F.,&lt;&gt;{}[]&quot;)</p>
  <p>Attemp 3:
  Optional(&quot;Test%40123!%23$%25%5E&amp;*()~-_=+;%3A'%2F.,%3C%3E%7B%7D%5B%5D&quot;)</p>
  <p>Attemp 4:
  Optional(&quot;Test@123!%23$%25%5E&amp;*()~-_=+;:'/.,%3C%3E%7B%7D%5B%5D&quot;)</p>
  <p>Attemp 5: Test@123!%23$%25%5E&amp;*()~-_=+;:'/.,%3C%3E%7B%7D%5B%5D</p>
  </blockquote>
  <p>Non of them are percentage encoding the complete special characters.I want to encode all of the special character just like  following special character are never encoded in almost all attempts.</p>
  <blockquote>
  <p>*()~-_=+;:'/.,</p>
  </blockquote>
  <p>Kindly tell me the code to percentage encode all of my special character including *()~-_=+;:'/.,   ? Thanks</p>