iOSエンジニアのつぶやき

毎朝8:30に iOS 関連の技術について1つぶやいています。まれに釣りについてつぶやく可能性があります。

【Swift】特定の文字列の置き換え

今回は下記のようなsemantic versioningのバージョンを

2.34.0

下記のような数値で取り出した際のメモです。(用は文字列操作)

2340

結論

StringProtocolのreplacingOccurrencesメソッドを使うことで、特定の文字列をリプレースできます。

let versionString = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String // 2.34.0
if let version = Int(versionString.replacingOccurrences(of: ".", with: "")) {
    print(version) // 2340
}

てな感じで本日も以上となります🍺

その他の記事

yamatooo.blog

yamatooo.blog

yamatooo.blog