lazy var プロパティ名 : プロパティ型 = 式
</pre>
===KeyPath===
*https://qiita.com/imchino/items/67a987681bca5ad0408b
*「あるデータ型に定義されたプロパティまでの参照(パス)」です。
*キーパス式は、型のプロパティまたは添え字を参照する。
*キー・値監視などの動的プログラミングタスクでは、キーパス式を使用する。
<pre>
\<#type name#>.<#path#>
</pre>
==サブスクリプト==
*コレクションへの統一的なアクセス手法
*firstプロパティ
*lastプロパティ
====変換====
=====Array -> Set =====
<pre>
1> let ary: [Int] = [3,2,3,2]
ary: [Int] = 4 values {
[0] = 3
[1] = 2
[2] = 3
[3] = 2
}
2> print(Set(ary))
[3, 2]
</pre>
===== Set -> Array =====
<pre>
6> let set: Set<Int> = [3,4,5,6]
set: Set<Int> = 4 values {
[0] = 4
[1] = 6
[2] = 3
[3] = 5
}
7> print(Array(set))
[4, 6, 3, 5]
</pre>
==クロージャ==
*基本
*https://qiita.com/atsuastu_jr/items/e3a6990127d96c39a167
<pre>
{ 引数 in 戻り値を返す式 }
<pre>
hello,swift!
</pre>
===コールバックにクロージャを利用する例===
*非同期処理の結果をクロージャを用いて呼び出し元に返す
<pre>
static func accessRequest(completion:((String?) -> Void)?) {
let allTypes = Set([
HKQuantityType.quantityType(forIdentifier: .bodyMass)!,
/* HKQuantityType.quantityType(forIdentifier: .stepCount)!,*/
])
self.getHealthStore()?.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in
if success {
completion?("アクセス許可: \(String(describing: success))")
} else {
completion?("アクセス不許可: \(String(describing: error?.localizedDescription))")
}
}
}
</pre>
hello,swift!
</pre>
===例===
* Message.user.name の重複を取り除き、","で連結して返す
<pre>
func joinAndUniqueUserNames(messages: [Message]) -> String {
var names:Set<String> = []
// クロージャ例
messages.forEach({message in names.insert(message.user.name)})
// トレイリングクロージャ例
messages.forEach { message in
names.insert(message.user.name)
}
return names.joined(separator: ",")
}
</pre>
===属性===
----
print(genericSample(x:1.2, y:"abc"))
</pre>
===some,any===
----
https://jimaru.blog/programming/swift/swift-some-any/
==並行処理==
self.hogeLabel.text = result
}
</pre>
==ユニットテスト==
===非同期処理をテストする===
<pre>
func testAccessRequest() {
var result : String?
let expectation = expectation(description: "非同期待ち")
// 非同期処理の呼び出しとコールバック
HealthKitHelper.accessRequest() { accessResult in
result = accessResult
expectation.fulfill() // 非同期待ち解除
}
waitForExpectations(timeout: 4) // 指定時間応答がなければ失敗
print("ACCESS REQUEST : \(String(describing: result))")
assert(result != nil)
}
</pre>
=====[https://www.typea.info/blog/index.php/2022/06/07/watchos_app_prestudy/ watchOS 開発概要]=====
----
[https://developer.apple.com/documentation/watchos-apps Developer Document]
=====[https://www.typea.info/blog/index.php/2022/06/08/swift_watchos_simple_app_connect_healthkit/ HealthKit利用 watchOS アプリ作成]=====
=====[https://www.typea.info/blog/index.php/2022/06/14/swift_watchos_watchface_complication/ 文字盤に画像や情報を表示する]=====
=====[https://developer.apple.com/documentation/watchos-apps/setting-up-tests-for-your-watchos-app Unit Tests]=====
=====[https://qiita.com/am10/items/e58dfe28f024b3dc39ad WatchKitオブジェクト]=====
====HealthKit====
$R1: Int = 3
</pre>
====[[デザイン]]====
=====[https://photoshopvip.net/102903 配色]=====
*http://photoshopvip.net/95427
*https://colorhunt.co/palette/196224
=====アイコン=====
*https://www.flaticon.com
====書式====
5> print(nfmt.string(from: NSNumber(value:12345)))
Optional("12,345")
</pre>
=====小数点桁揃え=====
<pre>
print(String(format:"%.1f", (5.0 / 3.0)))
1.7
</pre>
=====桁揃え=====
<pre>
print(String(format:"%05d", 99))
00099
</pre>