質問編集履歴
2
モデルの追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -134,6 +134,100 @@
|
|
134
134
|
|
135
135
|
|
136
136
|
|
137
|
+
Clip Model
|
138
|
+
|
139
|
+
```Swift
|
140
|
+
|
141
|
+
class Clip: Object, Mappable {
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
// MARK: Realm - stored properties
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
dynamic var id: Int = 0
|
150
|
+
|
151
|
+
dynamic var title: String = ""
|
152
|
+
|
153
|
+
dynamic var created: Double = 0
|
154
|
+
|
155
|
+
dynamic var modified: Double = 0
|
156
|
+
|
157
|
+
dynamic var count_reply: Int = 0
|
158
|
+
|
159
|
+
dynamic var count_pv: Int = 0
|
160
|
+
|
161
|
+
dynamic var is_beginner: Bool = false
|
162
|
+
|
163
|
+
dynamic var is_accepted: Bool = false
|
164
|
+
|
165
|
+
dynamic var is_presentation: Bool = false
|
166
|
+
|
167
|
+
dynamic var tags: [String] = []
|
168
|
+
|
169
|
+
var user: User = User()
|
170
|
+
|
171
|
+
var replies = List<Reply>()
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
// MARK: ObjectMapper
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
class func newInstance(map: Map) -> Mappable? {
|
180
|
+
|
181
|
+
return Clip()
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
required convenience init?(_ map: Map){
|
188
|
+
|
189
|
+
self.init()
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
/// Mapping between ObjectMapper (JSON) and the model properties
|
196
|
+
|
197
|
+
func mapping(map: Map) {
|
198
|
+
|
199
|
+
id <- map["id"]
|
200
|
+
|
201
|
+
title <- map["title"]
|
202
|
+
|
203
|
+
created <- map["created"]
|
204
|
+
|
205
|
+
modified <- map["modified"]
|
206
|
+
|
207
|
+
count_reply <- map["count_reply"]
|
208
|
+
|
209
|
+
count_pv <- map["count_pv"]
|
210
|
+
|
211
|
+
is_beginner <- map["is_beginner"]
|
212
|
+
|
213
|
+
is_accepted <- map["is_accepted"]
|
214
|
+
|
215
|
+
is_presentation <- map["is_presentation"]
|
216
|
+
|
217
|
+
tags <- map["tags"]
|
218
|
+
|
219
|
+
user <- map["user"]
|
220
|
+
|
221
|
+
replies <- map["replies"]
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
```
|
228
|
+
|
229
|
+
|
230
|
+
|
137
231
|
|
138
232
|
|
139
233
|
このRealmモデルは下記のGistを参考に作りました。
|
1
遅くなり申し訳ありません。追記以来の通りに編集いたいしました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -94,6 +94,48 @@
|
|
94
94
|
|
95
95
|
|
96
96
|
|
97
|
+
エラー内容は以下です。
|
98
|
+
|
99
|
+
```txt
|
100
|
+
|
101
|
+
Terminating app due to uncaught exception 'RLMException', reason: ''NSArray' is not supported as an RLMObject property. All properties must be primitives, NSString, NSDate, NSData, RLMArray, or subclasses of RLMObject. See http://realm.io/docs/objc/api/Classes/RLMObject.html for more information.'
|
102
|
+
|
103
|
+
```
|
104
|
+
|
105
|
+
NSArrayではダメだとかいてあるのですが、RealmSwift公式ドキュメントでは以下のようにDictionaryで渡しています。
|
106
|
+
|
107
|
+
```swift
|
108
|
+
|
109
|
+
// (2) Dictionaryの値を使ってDogクラスのオブジェクトを作成する
|
110
|
+
|
111
|
+
let myOtherDog = Dog(value: ["name" : "Pluto", "age": 3])
|
112
|
+
|
113
|
+
```
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
また私のコード上では以下のようにしてインスタンスを生成しています。
|
118
|
+
|
119
|
+
```swift
|
120
|
+
|
121
|
+
let questions = json["questions"].arrayObject
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
for subJson in questions! {
|
126
|
+
|
127
|
+
let question: Question = Mapper<Question>().map(subJson)!
|
128
|
+
|
129
|
+
self.questions.append(question)
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
```
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
|
138
|
+
|
97
139
|
このRealmモデルは下記のGistを参考に作りました。
|
98
140
|
|
99
141
|
|