Swift 文字列の㌫エンコード/デコード

addingPercentEncoding

Swiftの文字列(String)には、addingPercentEncoding()という関数が用意されています。これを使えばを文字列から㌫エンコードすることができます。エンコードされた文字列は、%E3%81%82のようなURLのQueryでよく見かけるフォーマットです。

let s = "不思議の国のトムキンス".addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!
print(s)

上記コードは、addingPercentEncoding()のコード例です%E4%B8%8D%E6%80%9D%E8%AD%B0%E3%81%AE%E5%9B%BD%E3%81%AE%E3%83%88%E3%83%A0%E3%82%AD%E3%83%B3%E3%82%B9と出力されます

このパーセントエンコードされた文字列をもとに戻すにはremovingPercentEncodingを使います。removingPercentEncodingは、String内にある関数です

let s = "%E6%80%AA%E7%8D%A3%E2%AD%90".removingPercentEncoding
print(s as Any)

Swift NSSavePanelで Thread 1: EXC_BREAKPOINT

NSSavePanel runModal()

Swift macOS App デフォルトプロジェクトで、NSSavePanel()を使った場合Thread 1: EXC_BREAKPOINT が出て下記のようなエラーが表示されます。

[OpenSavePanels] ERROR: Unable to display save panel: your app has the User Selected File Read entitlement but it needs User Selected File Read/Write to display save panels. Please ensure that your app's target capabilities include the proper entitlements.

Sandboxの設定が「User Selected File = ReadOnly」になっているので「Read / Write」に変更すれば解決します。

User selected file read write
User Selected File = Read / Write

一度でもビルドに成功していてUser Selected File = ReadOnlyに変更した場合下記のメッセージが出ます

error: Entitlements file "xxx.entitlements" was modified during the build, which is not supported. You can disable this error by setting 'CODE_SIGN_ALLOW_ENTITLEMENTS_MODIFICATION' to 'YES', however this may cause the built product's code signature or provisioning profile to contain incorrect entitlements. (in target 'xxx' from project 'xxx')

2022 MJELD TECHNOLOGIES. ALL RIGHTS RESERVED