質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

2回答

4886閲覧

Swift 3.0 において、PHPのurl encodeと同じエンコーディングをしたい

hakase

総合スコア107

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2016/09/21 06:51

こんにちは!
Swift3.0になって、これまで使っていたエンコーディングができなくなりました。
Swift3.0では

swift3.0

1str.addingPercentEncoding(withAllowedCharacters:NSCharacterSet.xxx)

なメソッドを使わないといけなくなったのですが、
xxxに何を指定したらいいか、わかりません。
withAllowedCharacters:もあっているかわかりません。

PHPのurl encode関数と同じエンコーディングをしたいです。
アルファベットと数字とピリオド以外の特殊文字と日本語だけを%xxxxxに
したいです。

どうコードを書けばいいでしょうか?
困っています。
助けてください。よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

Swift

1 let encodedString = CFURLCreateStringByAddingPercentEscapes( 2 nil, 3 originalString as CFString!, 4 nil, 5 "!*'();:@&=+$,/?%#[]" as CFString!, 6 CFStringBuiltInEncodings.UTF8.rawValue

が手取り早かったです。Swift3.0でも残っていて良かったー。

投稿2016/09/23 05:01

hakase

総合スコア107

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

以下のサイトに載っていることで解決できると思います。

swift 3.0 のメモ

swift

1if let u: String = "http://www.test.com/あいうえお".addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) { 2 print(u) 3 //=> http://www.test.com/%E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A 4}

定義に飛ぶと以下のように使いえるものも分かると思います。

swift

1 2public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgebra { 3 4 public init() 5 public init(charactersIn range: Range<UnicodeScalar>) 6 public init(charactersIn range: ClosedRange<UnicodeScalar>) 7 public init(charactersIn string: String) 8 public init(bitmapRepresentation data: Data) 9 public init?(contentsOfFile file: String) 10 11 public var hashValue: Int { get } 12 public var description: String { get } 13 14 /// A textual representation of this instance, suitable for debugging. 15 public var debugDescription: String { get } 16 17 /// Returns a character set containing the characters in Unicode General Category Cc and Cf. 18 public static var controlCharacters: CharacterSet { get } 19 20 /// Returns a character set containing the characters in Unicode General Category Zs and `CHARACTER TABULATION (U+0009)`. 21 public static var whitespaces: CharacterSet { get } 22 23 /// Returns a character set containing characters in Unicode General Category Z*, `U+000A ~ U+000D`, and `U+0085`. 24 public static var whitespacesAndNewlines: CharacterSet { get } 25 26 /// Returns a character set containing the characters in the category of Decimal Numbers. 27 public static var decimalDigits: CharacterSet { get } 28 29 /// Returns a character set containing the characters in Unicode General Category L* & M*. 30 public static var letters: CharacterSet { get } 31 32 /// Returns a character set containing the characters in Unicode General Category Ll. 33 public static var lowercaseLetters: CharacterSet { get } 34 35 /// Returns a character set containing the characters in Unicode General Category Lu and Lt. 36 public static var uppercaseLetters: CharacterSet { get } 37 38 /// Returns a character set containing the characters in Unicode General Category M*. 39 public static var nonBaseCharacters: CharacterSet { get } 40 41 /// Returns a character set containing the characters in Unicode General Categories L*, M*, and N*. 42 public static var alphanumerics: CharacterSet { get } 43 44 /// Returns a character set containing individual Unicode characters that can also be represented as composed character sequences (such as for letters with accents), by the definition of "standard decomposition" in version 3.2 of the Unicode character encoding standard. 45 public static var decomposables: CharacterSet { get } 46 47 /// Returns a character set containing values in the category of Non-Characters or that have not yet been defined in version 3.2 of the Unicode standard. 48 public static var illegalCharacters: CharacterSet { get } 49 50 /// Returns a character set containing the characters in Unicode General Category P*. 51 public static var punctuationCharacters: CharacterSet { get } 52 53 /// Returns a character set containing the characters in Unicode General Category Lt. 54 public static var capitalizedLetters: CharacterSet { get } 55 56 /// Returns a character set containing the characters in Unicode General Category S*. 57 public static var symbols: CharacterSet { get } 58 59 /// Returns a character set containing the newline characters (`U+000A ~ U+000D`, `U+0085`, `U+2028`, and `U+2029`). 60 public static var newlines: CharacterSet { get } 61 62 /// Returns the character set for characters allowed in a user URL subcomponent. 63 public static var urlUserAllowed: CharacterSet { get } 64 65 /// Returns the character set for characters allowed in a password URL subcomponent. 66 public static var urlPasswordAllowed: CharacterSet { get } 67 68 /// Returns the character set for characters allowed in a host URL subcomponent. 69 public static var urlHostAllowed: CharacterSet { get } 70 71 /// Returns the character set for characters allowed in a path URL component. 72 public static var urlPathAllowed: CharacterSet { get } 73 74 /// Returns the character set for characters allowed in a query URL component. 75 public static var urlQueryAllowed: CharacterSet { get } 76 77 /// Returns the character set for characters allowed in a fragment URL component. 78 public static var urlFragmentAllowed: CharacterSet { get } 79 80 /// Returns a representation of the `CharacterSet` in binary format. 81 public var bitmapRepresentation: Data { get } 82 83 /// Returns an inverted copy of the receiver. 84 public var inverted: CharacterSet { get } 85 86 /// Returns true if the `CharacterSet` has a member in the specified plane. 87 /// 88 /// This method makes it easier to find the plane containing the members of the current character set. The Basic Multilingual Plane (BMP) is plane 0. 89 public func hasMember(inPlane plane: UInt8) -> Bool 90 91 /// Insert a range of integer values in the `CharacterSet`. 92 /// 93 /// It is the caller's responsibility to ensure that the values represent valid `UnicodeScalar` values, if that is what is desired. 94 public mutating func insert(charactersIn range: Range<UnicodeScalar>) 95 96 /// Insert a closed range of integer values in the `CharacterSet`. 97 /// 98 /// It is the caller's responsibility to ensure that the values represent valid `UnicodeScalar` values, if that is what is desired. 99 public mutating func insert(charactersIn range: ClosedRange<UnicodeScalar>) 100 101 /// Remove a range of integer values from the `CharacterSet`. 102 public mutating func remove(charactersIn range: Range<UnicodeScalar>) 103 104 /// Remove a closed range of integer values from the `CharacterSet`. 105 public mutating func remove(charactersIn range: ClosedRange<UnicodeScalar>) 106 107 /// Insert the values from the specified string into the `CharacterSet`. 108 public mutating func insert(charactersIn string: String) 109 110 /// Remove the values from the specified string from the `CharacterSet`. 111 public mutating func remove(charactersIn string: String) 112 113 /// Invert the contents of the `CharacterSet`. 114 public mutating func invert() 115 116 /// Insert a `UnicodeScalar` representation of a character into the `CharacterSet`. 117 /// 118 /// `UnicodeScalar` values are available on `Swift.String.UnicodeScalarView`. 119 public mutating func insert(_ character: UnicodeScalar) -> (inserted: Bool, memberAfterInsert: UnicodeScalar) 120 121 /// Insert a `UnicodeScalar` representation of a character into the `CharacterSet`. 122 /// 123 /// `UnicodeScalar` values are available on `Swift.String.UnicodeScalarView`. 124 public mutating func update(with character: UnicodeScalar) -> UnicodeScalar? 125 126 /// Remove a `UnicodeScalar` representation of a character from the `CharacterSet`. 127 /// 128 /// `UnicodeScalar` values are available on `Swift.String.UnicodeScalarView`. 129 public mutating func remove(_ character: UnicodeScalar) -> UnicodeScalar? 130 131 /// Test for membership of a particular `UnicodeScalar` in the `CharacterSet`. 132 public func contains(_ member: UnicodeScalar) -> Bool 133 134 /// Returns a union of the `CharacterSet` with another `CharacterSet`. 135 public func union(_ other: CharacterSet) -> CharacterSet 136 137 /// Sets the value to a union of the `CharacterSet` with another `CharacterSet`. 138 public mutating func formUnion(_ other: CharacterSet) 139 140 /// Returns an intersection of the `CharacterSet` with another `CharacterSet`. 141 public func intersection(_ other: CharacterSet) -> CharacterSet 142 143 /// Sets the value to the intersection of the `CharacterSet` with another `CharacterSet`. 144 public mutating func formIntersection(_ other: CharacterSet) 145 146 /// Returns the exclusive or of the `CharacterSet` with another `CharacterSet`. 147 public func symmetricDifference(_ other: CharacterSet) -> CharacterSet 148 149 /// Sets the value to the exclusive or of the `CharacterSet` with another `CharacterSet`. 150 public mutating func formSymmetricDifference(_ other: CharacterSet) 151 152 /// Returns true if `self` is a superset of `other`. 153 public func isSuperset(of other: CharacterSet) -> Bool 154 155 /// Returns true if the two `CharacterSet`s are equal. 156 public static func ==(lhs: CharacterSet, rhs: CharacterSet) -> Bool 157} 158

投稿2016/09/21 14:57

編集2016/09/21 14:58
_Kentarou

総合スコア8490

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hakase

2016/09/23 04:25

HTTP通信で、"?A=a&B=b"みたいな文字列で渡したくて、aやbに/や&などが入っていると まずいのです。 PHPのurlencode関数の場合には、:や/や&や=なども%に変換されます。 引数を一つ一つあたっていきますね。
hakase

2016/09/23 04:25

感謝の言葉を書いていませんでした。ありがとうございますー。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問