【Swift】Tips: あると便利だったextension達(記法編)
はじめに
モバイルアプリサービス部の中安です。
最近ブログを書けていなかったので、小ネタを少しばかり・・・。
アプリを作ってきた際に、あるとなかなか便利だったextensionをボチボチとご紹介していければと思っています。 アプリの目的に合わせてやりかえる必要があったり、もっとよい実装があるかもしれませんが、何かの役に立てば光栄です。
今回は文字列の記法編です。
APIのキーやパラメータ、DBの論理名、その他諸々とスネーク記法を使ったり、キャメル記法を使ったりというシーンは多いかと思いますが、 そういう時に記法の変換を行ってくれるメソッドを準備しておけば色々と助かることも多いでしょう。
文字列をアッパーキャメル記法に
念のための説明ですが、「アッパーキャメル記法」とは単語の最初の文字を大文字にして、単語間には何も入れない(アンダースコアやスペースがない)記法のことです。 Swiftのクラスなどのシンボル名などは、こちらの命名規則ですね。
今回はキャメル記法(後述)からアッパーキャメル記法に変換するという想定で extension を作ってみます。
1 2 3 4 5 6 | extension String { func toUpperCamel() -> String { return capitalized.replacingOccurrences(of: "_", with: "") }} |
シンプルに capitalized という便利な String の機能があります。 これとアンダースコアの置換を組み合わせることでシンプルに実現できます。
例えば、TwitterAPIの戻り値のJSONキー(抜粋)にこれを適用してみると
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | "protected".toUpperCamel()"followers_count".toUpperCamel()"created_at".toUpperCamel()"utc_offset".toUpperCamel()"time_zone".toUpperCamel()"geo_enabled".toUpperCamel()"contributors_enabled".toUpperCamel()"is_translation_enabled".toUpperCamel()"profile_background_image_url".toUpperCamel()"profile_use_background_image".toUpperCamel()"has_extended_profile".toUpperCamel()"default_profile_image".toUpperCamel()"follow_request_sent".toUpperCamel()"translator_type".toUpperCamel()"retweeted_status".toUpperCamel()"in_reply_to_screen_name".toUpperCamel()↓ProtectedFollowersCountCreatedAtUtcOffsetTimeZoneGeoEnabledContributorsEnabledIsTranslationEnabledProfileBackgroundImageUrlProfileUseBackgroundImageHasExtendedProfileDefaultProfileImageFollowRequestSentTranslatorTypeRetweetedStatusInReplyToScreenName |
このようになります。
文字列をキャメル記法に
「キャメル記法」とは、前述のアッパーキャメル記法のルールに則りますが、一番最初の文字だけ小文字になる記法です。 Swiftの変数名などは、こちらの命名規則ですね。
1 2 3 4 5 6 7 8 9 10 | extension String { func toCamel() -> String { if isEmpty { return "" } let range = NSMakeRange(0, 1) let ns = toUpperCamel() as NSString return ns.replacingCharacters(in: range, with: ns.substring(with: range).lowercased()) }} |
前項のアッパーキャメル生成を元に、最初の一文字を lowercase にするだけです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | "protected".toCamel()"followers_count".toCamel()"created_at".toCamel()"utc_offset".toCamel()"time_zone".toCamel()"geo_enabled".toCamel()"contributors_enabled".toCamel()"is_translation_enabled".toCamel()"profile_background_image_url".toCamel()"profile_use_background_image".toCamel()"has_extended_profile".toCamel()"default_profile_image".toCamel()"follow_request_sent".toCamel()"translator_type".toCamel()"retweeted_status".toCamel()"in_reply_to_screen_name".toCamel()↓protectedfollowersCountcreatedAtutcOffsettimeZonegeoEnabledcontributorsEnabledisTranslationEnabledprofileBackgroundImageUrlprofileUseBackgroundImagehasExtendedProfiledefaultProfileImagefollowRequestSenttranslatorTyperetweetedStatusinReplyToScreenName |
このようになります。
文字列をスネーク記法に
「スネーク記法」とは、文字列は全て小文字で表現され、単語と単語はアンダースコアで繋げていく記法です。 先ほどから登場しているTwitterAPIの戻り値の各JSONキーもそうであるように、WebAPIなどでは多用されている記法です。
スネーク記法(前述)からスネーク記法に変換するという想定で extension を作ります。
1 2 3 4 5 6 7 8 9 10 11 12 | extension String { func toSnake() -> String { let pattern = "(?<=\\w)([A-Z])" guard let reg = try? NSRegularExpression(pattern: pattern, options: []) else { return self } let mutable = NSMutableString(string: self) let range = NSMakeRange(0, self.count) reg.replaceMatches(in: mutable, options: [], range: range, withTemplate: "_$1") return (mutable as String).lowercased() }} |
正規表現を使用して、文字列の途中で大文字から始まる単語の先頭にアンダースコアを付け、 lowercase へと変換しています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | "createdAt".toSnake()"utcOffset".toSnake()"timeZone".toSnake()"geoEnabled".toSnake()"contributorsEnabled".toSnake()"isTranslationEnabled".toSnake()"profileBackgroundImageUrl".toSnake()"profileUseBackgroundImage".toSnake()"hasExtendedProfile".toSnake()"defaultProfileImage".toSnake()"followRequestSent".toSnake()"translatorType".toSnake()"retweetedStatus".toSnake()"inReplyToScreenName".toSnake()↓created_atutc_offsettime_zonegeo_enabledcontributors_enabledis_translation_enabledprofile_background_image_urlprofile_use_background_imagehas_extended_profiledefault_profile_imagefollow_request_senttranslator_typeretweeted_statusin_reply_to_screen_name |
これでキャメル記法とスネーク記法の相互互換ができるようになりました。