トップ 最新 追記

Cocoa練習帳

iOS/iPhone/iPad/watchOS/tvOS/MacOSX/Android プログラミング, Objective-C, Cocoa, Swiftなど

2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|03|

2022-10-10 [Swift]protocol Identifiable

ユニークな値を持つidプロパティを要求するのがIdentifiableプロトコル。

import Cocoa
 
struct MyItem: Identifiable {
    var title: String
}

idプロパティが存在しないため、以下のエラーとなった。

Type 'MyItem' does not conform to protocol 'Identifiable'
Do you want to add protocol stubs?

idプロパティを追加したら、エラーは解消。

import Cocoa
 
struct MyItem: Identifiable {
    var id = UUID()
    var title: String
}
 
let item = MyItem(title: "test")
print("\(item)")
MyItem(id: 5B56ABEF-5002-4A50-AD5B-42B91842DFB6, title: "test")

2022-10-11 [Swift]Codable

シリアライズのためのプロトコルのEncodableとDecodableがあるが、その両方に対応するのがCodableだ。

typealias Codable = Decodable & Encodable

基本データ型のIntやDouble、StringなどはプロトコルCodableに適合しているので、基本データ型で構成されている場合は以下のように定義する。

struct MyItem: Identifiable, Codable {
    var id = UUID()
    var title: String
}

トップ 最新 追記