Skip to content

Instantly share code, notes, and snippets.

View kishikawakatsumi's full-sized avatar
🏠
Working from home

Kishikawa Katsumi kishikawakatsumi

🏠
Working from home
View GitHub Profile
import Foundation
/// Calculates the `n`th Fibonacci number
///
/// This function returns `0` for all negative numbers.
func fib(_ n: Int) -> Int {
guard n > 0 else {
return 0
}
@kishikawakatsumi
kishikawakatsumi / FB12645750
Created July 17, 2023 08:34
Writing the correct code when persisting models containing associations is very difficult
If the incorrect code is written when attempting to persist a model containing associations, either run-time errors occur or only the associations are not saved (although they appear to be saved correctly).
The latter makes it very difficult to find the problem, as it appears to be working correctly without any errors.
Of the four code examples below, only the first one saves the data correctly, including the association.
All of the remaining three cases do not work correctly.
The important thing is to insert the association source object into the context first.
Otherwise, you will get either a run-time error or the unexpected behaviour that only the related objects are not saved.
```swift
@kishikawakatsumi
kishikawakatsumi / FB12645485
Created July 17, 2023 07:56
[SwiftData] Classes qualified with @model macro allow incomplete initializers
Classes qualified with @Model macro allow incomplete initializers
The compiler does not throw an error if a class with @Model, as in the following code, implements only an incomplete initializer (in this example, the properties are not initialized at all).
This would normally (if the plain class is not macro qualified) result in a compile error "Return from initializer without initializing all stored properties".
```swift
import SwiftData
@Model
final class Event {
@kishikawakatsumi
kishikawakatsumi / FB12646222
Created July 17, 2023 09:34
[SwiftData] Compiler cannot reject models containing types that cannot be saved.
SwiftData raises a runtime error when attempting to save a property of CGFloat type, but the compiler cannot validate it.
Models with properties of type CGFloat, such as the example below, cannot be saved in SwiftData. It will cause a run-time error.
I believe that the compiler should reject such incorrect models. However, such models cannot be made an error at compile-time, so the following documentation statement that SwiftData can safely describe the model layer is highly questionable.
> Using modern language features like macros, SwiftData enables you to write code that is fast, efficient, and safe, enabling you to describe the entire model layer (or object graph) for your app.
```swift
@Model
@kishikawakatsumi
kishikawakatsumi / gist:d8d6543eceaeeeaf36394f29897d9a2e
Created August 18, 2023 00:50
「モバイル・エコシステムに関する競争評価 最終報告」に対する意見募集
政府が民間の自由な経済活動への介入は原則として慎重であるべきです。報告書で述べられているApp Storeの問題点は政府が規制や介入をしなければならないほどの解決困難であったり多数の被害者が発生しているようなものではありません。
1.OSやブラウザ等の仕様変更等について
大規模なソフトウェア製品における本質的な問題です。規制を設けたところで解決する問題ではありません。
報告書ではAppleが必要な情報を適切に提供していないという指摘があるが、これは正しくなく、過去と比較して現在の情報公開の状況は大きく改善されています。
ベータ版についての情報についても、10年前はWWDCの情報は参加者のみがアクセスできましたが、現在はデベロッパーアカウントがあればすべて閲覧可能になっています。デベロッパーアカウントも有料のデベロッパープログラムは必要なく、Apple IDがあればよいので、事実上すべての人間に情報は開示されています。
ベータ版が安定するまでに長い時間がかかり対応する時間が限られているという指摘については先に述べたとおりソフトウェアの本質的な問題です。モバイルデバイスのOSははるかに複雑になっておりむしろ毎年新機能や改善が行われているということは驚愕すべきことです。
@kishikawakatsumi
kishikawakatsumi / TableHeaderView-AutoLayout.swift
Created April 29, 2020 11:19
Auto sizing UITableView header
...
headerView.translatesAutoresizingMaskIntoConstraints = false
tableView.tableHeaderView = headerView
NSLayoutConstraint.activate([
headerView.topAnchor.constraint(equalTo: tableView.topAnchor),
headerView.widthAnchor.constraint(equalTo: tableView.widthAnchor),
headerView.centerXAnchor.constraint(equalTo: tableView.centerXAnchor)])
tableView.tableHeaderView?.layoutIfNeeded()
@kishikawakatsumi
kishikawakatsumi / ReorderingRealmResultsInTableView.swift
Created July 27, 2016 04:51
Reorder Realm Results in UITableView
import UIKit
import RealmSwift
class Data: Object {
dynamic var name = ""
...
dynamic var order = 0 // 並べ替えのためのカラムが必要
}
@kishikawakatsumi
kishikawakatsumi / TabView.swift
Created November 27, 2019 04:51
My SwiftUI TabView
import SwiftUI
struct TabView: View {
var views: [TabBarItem]
@State var selectedIndex: Int = 0
init(_ views: [TabBarItem]) {
self.views = views
}
<中間報告の該当箇所: 72ページ>
> a. アップデートに対応する十分な時間を確保した上での事前告知の実施 (OS のアップデートが与える影響の大きさに応じて、(より長期の)適切な準備期間の義務付け)
> b. 最新バージョンに関する適切な情報開示(変更に伴うデータの引継ぎやAPIとの連携に関する運用面での対応に関するもの等を含む)
> c. デベロッパからの問い合わせに関する手続・体制の整備
> d. 運営状況の政府への報告と、政府によるモニタリング・レビューの実施
プラットフォーム側が提供する情報の読解力によって差が生じているという事実は存在するが、それは事業者の能力に起因する問題であり能力の低い事業者に合わせて規制を設計することはこの分野の健全な発展を阻害し、自由な競争すら行われなくなる恐れがある。
またソフトウェアは変化が激しく、AppleやGoogleでさえも1年以上の期間にわたる予測を正確んい行うことは困難である。ほとんどの場合において、情報が遅かったり不確実であったり、プラットフォームやファーストバーティが優遇されているように見えたり、という問題はプラットフォーム側が自身の利益のために意図的に情報を隠しているわけではなく必然と考えられる(もちろん例外はある)。
<中間報告の該当箇所: 113ページ>
アプリストアの問題は大きく、セキュリティ、市場(の独占)、検閲の3点がそれぞれ独立しつつ関係しているのですべてを一緒に議論してしまうのは短絡的な議論となる恐れがある。
セキュリティについて。iOSにおいてセキュリティの根幹は強固なサンドボックス機構であり、サイドローディングの禁止と審査がセキュリティの向上に寄与しているというAppleの説明は誤解を招く意図があると考えられる。
またアプリのレビューはセキュリティの専門家が行っているわけではなく、レビューで検知できなかった不正な挙動をするアプリは過去に複数確認されている。
iOSにおいて現状のアプリストアの存在はセキュリティの担保には関係ない。その点でこの調査報告はおおむね妥当である。
ただし、サイドローディングに関しては現状においてもストアを介さない配布方法は制限がありながらも認められており、ほとんどのユースケースはそれで対応できると考えられる。利用者の選択肢を増やすという意味でサイドローディングが可能になることは望ましいが、それはあくまで利用者とプラットフォーム側の力関係によってなされるべきであり、行政機関が強制するようなことではない。