| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

差分

ナビゲーションに移動 検索に移動
11,495 バイト追加 、 2024年9月12日 (木) 10:53
=====XCTest=====
*ユニットテスト用
 
====[https://developer.apple.com/jp/mac-catalyst/ Mac Catalyst]====
*https://developer.apple.com/jp/mac-catalyst/
Mac Catalystを使って構築したMac向けのネイティブAppは、iPad向けAppとコードを共有でき、Mac専用の機能を追加することもできます。macOS Montereyでは、最新のAPIを使用して、ウインドウのタイトルバーにポップアップボタン、ツールチップ、サブタイトルを表示することができます。また、タッチ操作の代替機能、キーボードで操作する機能、SiriのIntentのサポートを提供できるほか、Commandキーを押しながら「P」を押していつでもプリントできるようにするなど、さまざまな操作への対応が追加されました
 
====[https://www.swift.org/package-manager/ Swift Package Manager]====
----
*https://www.swift.org/package-manager/
*https://blitzgate.co.jp/blog/2234/
*https://qiita.com/hironytic/items/09a4c16857b409c17d2c
=====[https://swiftpackageindex.com/ Swift Package Index]=====
===[[Ubuntu]]へインストール===
====Interface Builder====
----
=====[https://www.typea.info/blog/index.php/2022/03/08/xamarin_mac_app_storyboard/ storyboard]=====
*[https://www.typea.info/blog/index.php/2022/03/08/xamarin_mac_app_storyboard/ storyboard]
 
=====[https://www.typea.info/blog/index.php/2022/03/11/nstableview_celbased/ NSTableView CellBased]=====
*[https://www.typea.info/blog/index.php/2022/03/11/nstableview_celbased/ NSTableView CellBased]
 
=====[https://www.typea.info/blog/index.php/2022/03/23/nstableview_viewbased_macos/ NSTableView ViewBased]=====
*[https://www.typea.info/blog/index.php/2022/03/23/nstableview_viewbased_macos/ NSTableView ViewBased]
 
=====[https://www.typea.info/blog/index.php/2022/04/03/nsalert_macos/ NSAlert ダイアログ]=====
*[https://www.typea.info/blog/index.php/2022/04/03/nsalert_macos/ NSAlert ダイアログ]
 
====AutoLayout====
----
=====[https://www.typea.info/blog/index.php/2022/03/26/autolayout_concept/ AutoLayout概要]=====
*[https://www.typea.info/blog/index.php/2022/03/26/autolayout_concept/ AutoLayout概要]
 
=====[https://www.typea.info/blog/index.php/2022/03/28/interface_builder_autolayout/ AutoLayout操作]=====
*[https://www.typea.info/blog/index.php/2022/03/28/interface_builder_autolayout/ AutoLayout操作]
=====[https://www.typea.info/blog/index.php/2022/05/30/swift_ios_chart/ AutoLayout簡易操作 TinyConstraint]=====
*[https://www.typea.info/blog/index.php/2022/05/30/swift_ios_chart/ AutoLayout簡易操作 TinyConstraint]
 
====画面遷移====
----
*https://qiita.com/superman9387/items/c006ced215352f28a7b9
=====[https://www.typea.info/blog/index.php/2022/03/13/storyboard_childwindow_macos/ 子画面表示]=====
*[https://www.typea.info/blog/index.php/2022/03/13/storyboard_childwindow_macos/ storyboard を使って子画面を表示]
=====[https://www.typea.info/blog/index.php/2022/03/15/storyboard_segue_connect_screen/ segue(セグエ)で画面遷移]=====
----
*[https://www.typea.info/blog/index.php/2022/03/15/storyboard_segue_connect_screen/ storyboard segue(セグエ)で画面遷移]
*segueを呼び出し画面遷移させる
<pre>
//呼び出し元
performSegue(withIdentifier: "some_segue_id", sender: nil)
</pre>
=====[https://www.typea.info/blog/index.php/2022/06/04/ios_swift_navigation_controller/ Navigation Controller ナビゲーションバー]=====
*[https://www.typea.info/blog/index.php/2022/06/04/ios_swift_navigation_controller/ NavigationController ナビゲーションバー]
=====[https://www.typea.info/blog/index.php/2022/03/12/interfacebuilder_storyboard_tabview_screentransition/ Tab Viewの利用と画面遷移]=====
*[https://www.typea.info/blog/index.php/2022/03/12/interfacebuilder_storyboard_tabview_screentransition/ storyboard を使って Tab Viewの利用と画面遷移]
*[https://www.typea.info/blog/index.php/2022/03/11/nstableview_celbased/ NSTableView CellBased]
*[https://www.typea.info/blog/index.php/2022/03/23/nstableview_viewbased_macos/ NSTableView ViewBased]
*[https://www.typea.info/blog/index.php/2022/03/26/autolayout_concept/ AutoLayout概要]
==コマンドライン==
=====Float=====
=====Double=====
 
====型を調べる====
<pre>
1> print(type(of:"hello"))
String
</pre>
===変数の定義===
*リテラルは、"で囲む
*\n 改行など\でエスケープ
====書式====
<pre>
let frmt = String(format: "%03d", 1)
</pre>
====トリミング====
----
====初期化====
----
*Optional<T> は T?で宣言できる: String?*Optional<T> を T! で宣言すると、使用時に自動的にアンラップされる: String!
<pre>
5> let i = Optional(1)
*メモリ上のバイト列を管理
*Foundationのimportが必要
====Stringとの相互変換====
<pre>
let data: Data? = str.data(using: .utf8)
let str: String? = String(data: data, encoding: .utf8)
</pre>
==コレクション==
</pre>
====map関数スライス====*元の配列内の全てのアイテムに対してアクションを行い結果に基づいた配列を作成
<pre>
8> l$R5: [Int] = 8 values { [0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5 [5] = 6 [6] = 7 [7] = 8} 9> l[2..<5]$R6: ArraySlice<Int> = 3 values { [2] = 3 [3] = 4 [4] = 5</pre> ====map関数====*元の配列内の全てのアイテムに対してアクションを行い結果に基づいた配列を作成<pre>let base = [1,2,3]
let dbl = base.map {
(val) -> Int in
</pre>
===辞書 [https://developer.apple.com/documentation/swift/dictionary Dictionary<Key, Value>]===
----
*キーと値をもつコレクション
3>
</pre>
*====削除====
<pre>
3> sex.removeValue(forKey:"1")
}
}
</pre>
====空の辞書====
<pre>
var emptyDict: [String: String] = [:]
</pre>
$R0: Bool = true
</pre>
===イテレーター===
[https://developer.apple.com/documentation/swift/iteratorprotocol IteratorProtcol]
<pre>
let animals = ["Antelope", "Butterfly", "Camel", "Dolphin"]
var animalIterator = animals.makeIterator()
while let animal = animalIterator.next() {
print(animal)
}
</pre>
 
==演算子==
*C言語の演算子はほぼ使える
*インクリメント(++)、デクリメント(--) は廃止された
===高度な演算===
----
*Foundation をインポートすることで、sin、logなど利用可能になる
===型の判定===
式 as 型
</pre>
====アップキャスト(as)====----
*as
*コンパイル可能なアップキャストは常に成功
b: String = "abc"
</pre>
 ====ダウンキャスト(as? as!)====----
*as? キャストが正しく行われた場合オプショナル型、不正な場合、nil
*as! 強制キャスト失敗した場合、実行時エラー
let j= a as! Int
^
</pre>
 
====独自演算子の定義====
----
*https://qiita.com/shimesaba/items/cb36c0120a0a9f509da7
<pre>
infix operator *** {
associativity none
precedence 130
}
func ***(lhs: Int, rhs: Int) -> Int {
return lhs * rhs
}
</pre>
lazy var プロパティ名 : プロパティ型 = 式
</pre>
 ===KeyPath===*https://qiita.com/imchino/items/67a987681bca5ad0408b*「あるデータ型に定義されたプロパティまでの参照(パス)」です。*キーパス式は、型のプロパティまたは添え字を参照する。*キー・値監視などの動的プログラミングタスクでは、キーパス式を使用する。<pre>\<#type name#>.<#path#></pre> ==サブスクリプト==
*コレクションへの統一的なアクセス手法
<pre>
8> r.reduce("", {result, element in result + "-" + String(element) })
$R3: String = "-1-2-3"
</pre>
=====カウントを取る =====
<pre>
let count = sequence.reduce(0) { acc, row in acc + 1 }
</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>
</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> ===属性===----Swiftにおけるクロージャには、いくつかの属性を指定できます====@escapeing属性====----https://qiita.com/imchino/items/48564b0c23a64f539060*クロージャは、関数に引数として渡されたとき「関数をエスケープ」できます。*エスケープしたクロージャは、関数に戻った後でも呼び出し可能です。*エスケープクロージャのよくある例として、非同期処理をする完了ハンドラとしてのクロージャがあります。*関数の実行が完了した後にクロージャを呼び出すためには、エスケープしておく必要があります。<pre> func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) { // Call the handler with the current timeline entry handler(nil) }</pre> ==ジェネリクス== <pre>func isEquals<T: Equatable> (_ x: T, _ y: T) -> Bool { return x == y
}
print("\(isEquals("a", "b"))")
print(genericSample(x:1.2, y:"abc"))
</pre>
 
===some,any===
----
https://jimaru.blog/programming/swift/swift-some-any/
==並行処理==
===[https://ticklecode.com/swfitgdp/ ディスパッチキュー]===
----
*UIスレッド
*[https://ticklecode.com/swfitgdp/ Swiftでの非同期処理GDP|ディスパッチキューの解説|DispatchQueue.globalとmain]
*[[イベントディスパッチスレッド]]
----
*Operationは実行タスクと情報をカプセル化したものOperationQueueがキューの役割
 
====Mainスレッドで実行====
----
*UIスレッド
<pre>
DispatchQueue.main.async {
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>
==[[正規表現]]==
==Tips==
===print改行させない=watchOS=========[https://www.typea.info/blog/index.php/2022/06/04/ios_watchos_dev_env/ watchOS を iOS プロジェクトに統合]===== =====[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/11/swift_watchos_complication_watchface_run_app/ コンプリケーションを文字盤に追加する]===== =====[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=========[https://www.typea.info/blog/index.php/2022/05/31/ios_swift_healthkit/ iOS SwiftにHealthKitを組み込む]==========[https://www.typea.info/blog/index.php/2022/06/04/swift_ios_healthkit_healthdata/ ヘルスデータを読む]==========[https://www.typea.info/blog/index.php/2022/06/09/swift_watchos_save_healthdata/ ヘルスデータを書き込む]===== ====チャート====[https://www.typea.info/blog/index.php/2022/05/30/swift_ios_chart/ iOS Swiftにチャートを組み込む]====[[Sqlite]]====[https://www.typea.info/blog/index.php/2022/05/19/ios_swift_sqlite_via_typesafe_wapper/ iOS SwiftでSQLite使用する] ====ランダム====<pre> 2> Int.random(in:1 ... 9999)$R1: Int = 8549</pre>====Sum/Average====<pre> 1> let l = [1,2,3,4,5]l: [Int] = 5 values { [0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5} 2> l.reduce(0,+)$R0: Int = 15 3> l.reduce(0,+) / l.count$R1: Int = 3</pre> ====[[デザイン]]=========[https://photoshopvip.net/102903 配色]=====*http://photoshopvip.net/95427*https://colorhunt.co/palette/196224 =====アイコン=====*https://www.flaticon.com ====書式=========カンマつき数値=====<pre> 2> import Foundation 3> let nfmt = NumberFormatter() 4> nfmt.numberStyle = .decimal 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> ===日付=======書式====<pre> let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd" return formatter.string(from: day)</pre> ====曜日判定====<pre> let calendar = Calendar(identifier: .gregorian) return calendar.component(.weekday, from: date)</pre>====日付計算====<pre> 3> let today = Date()today: Foundation.Date = 2022-06-18 02:38:05 UTC 4> let tommorow = Calendar.current.date(byAdding: .day, value:1, to: today) tommorow: Foundation.Date? = 2022-06-19 02:38:05 UTC</pre>*NSDateに変換<pre>2> let d = Calendar.current.date(byAdding: .month, value:1, to: Date()) as! NSDate d: NSDate = 2022-07-03 15:38:43 UTC</pre> ===[[JSON]]===[[Swift Sample]] ===print改行させない===----<pre>
print(value, terminator: "")
</pre>
 
===ファイルを読む===
----

案内メニュー