Open21

Swift

saharasahara

Swift-format

https://cockscomb.hatenablog.com/entry/try-swift-format

https://nshipster.com/swift-format/

installation

Homebrew

brew install swift-format

Build from source

This is useful when you want to specify swift version.

# using swift 5.5 version
git clone -b swift-5.5-branch https://github.com/apple/swift-format.git
cd swift-format
swift build

# find build product
find . -type f -name swift-format
#ex) ./swift-format/.build/x86_64-apple-macosx/debug/swift-format

Add directory including swift-format binary to .zshrc.

source ~/.zshrc
# check if swift-format is in your path
swift-format --version

cf. https://medium.com/just-tech-it-now/swift-format-2c6786a15a4d

Configuration

https://github.com/apple/swift-format/blob/swift-5.1-branch/Documentation/Configuration.md

Example

In root directory of project:

% swift-format -m dump-configuration > .swift-format.json 
% code .swift-format.json

configure indentation in .swift-format.json:

...
  "indentation" : {
-    "spaces" : 2  
+    "spaces" : 4
  },
...
saharasahara

Visual Studio Code

https://nshipster.com/vscode/

デバッグ実行事始め

最近、AsyncStreamのbufferingNewestをSwiftファイルに書いて動作を確認しようとしたときに、予想していなかった挙動になった。

そこで、SwiftをVisual studio codeでデバッグ実行するべく調べた。

https://www.swift.org/documentation/articles/getting-started-with-vscode-swift.html

に手順は書いてある。

ざっくり

  1. プロジェクトを作成
  2. launch.jsonにデバッグ実行用の設定を追加
  3. play buttonでデバッグ実行

という流れのようだ。

⌘ Shift P でプロジェクトを作ると、デフォルトで launch.jsonにタスクの設定がなされている。

これらのタスクは、⌘ Shift B で呼び出すことが出来る。

build command

ここまで設定できていれば、⌘ Shift Dからデバッグ実行を開始できる。

saharasahara

Swift Package Manager

Generate Xcode project

swift package generate-xcodeproj can generate xcodeproj file , but recent xcode(s) are enable to open Swift packages directory.

% swift package generate-xcodeproj
warning: Xcode can open and build Swift Packages directly. 'generate-xcodeproj' is no longer needed and will be deprecated soon.
generated: Some/Swift/Package/SPM.xcodeproj

(tested in Swift 5.5.1, Xcode13.1)

saharasahara

Mark as deprecated

@available(*, deprecated, message: "some message")
func someDeprecatedFunc() { } 
// someDeprecatedFunc()の呼び出し元にはワーニングが表示される
saharasahara

Swift Regix

iOS16から、Swiftの言語組み込みの正規表現のAPIが使えるようになった。

https://qiita.com/usamik26/items/5a13d12ffc332ddf0512

https://speakerdeck.com/ainame/swift-5-dot-7debian-waruzheng-gui-biao-xian-woshi-sitemiyou

https://www.fuwamaki.com/article/359

iOS15以前

iOS11から、NSTextCheckingResultにrange(withName:)が追加され、名前付きキャプチャが可能になった。

https://qiita.com/sora0077@github/items/00396608a1d83a253d02