teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

モデルの追記

2015/12/17 07:41

投稿

YoheiFusayasu
YoheiFusayasu

スコア20

title CHANGED
File without changes
body CHANGED
@@ -66,7 +66,54 @@
66
66
  }
67
67
  ```
68
68
 
69
+ Clip Model
70
+ ```Swift
71
+ class Clip: Object, Mappable {
72
+
73
+ // MARK: Realm - stored properties
74
+
75
+ dynamic var id: Int = 0
76
+ dynamic var title: String = ""
77
+ dynamic var created: Double = 0
78
+ dynamic var modified: Double = 0
79
+ dynamic var count_reply: Int = 0
80
+ dynamic var count_pv: Int = 0
81
+ dynamic var is_beginner: Bool = false
82
+ dynamic var is_accepted: Bool = false
83
+ dynamic var is_presentation: Bool = false
84
+ dynamic var tags: [String] = []
85
+ var user: User = User()
86
+ var replies = List<Reply>()
87
+
88
+ // MARK: ObjectMapper
89
+
90
+ class func newInstance(map: Map) -> Mappable? {
91
+ return Clip()
92
+ }
93
+
94
+ required convenience init?(_ map: Map){
95
+ self.init()
96
+ }
97
+
98
+ /// Mapping between ObjectMapper (JSON) and the model properties
99
+ func mapping(map: Map) {
100
+ id <- map["id"]
101
+ title <- map["title"]
102
+ created <- map["created"]
103
+ modified <- map["modified"]
104
+ count_reply <- map["count_reply"]
105
+ count_pv <- map["count_pv"]
106
+ is_beginner <- map["is_beginner"]
107
+ is_accepted <- map["is_accepted"]
108
+ is_presentation <- map["is_presentation"]
109
+ tags <- map["tags"]
110
+ user <- map["user"]
111
+ replies <- map["replies"]
112
+ }
113
+ }
114
+ ```
69
115
 
116
+
70
117
  このRealmモデルは下記のGistを参考に作りました。
71
118
 
72
119
  [Transform arrays with ObjectMapper to Realm's List type](https://gist.github.com/Jerrot/fe233a94c5427a4ec29b)

1

遅くなり申し訳ありません。追記以来の通りに編集いたいしました。

2015/12/17 07:41

投稿

YoheiFusayasu
YoheiFusayasu

スコア20

title CHANGED
File without changes
body CHANGED
@@ -46,6 +46,27 @@
46
46
 
47
47
  とした時に必ずRLMExceptionになってしまい、落ちてしまいます。
48
48
 
49
+ エラー内容は以下です。
50
+ ```txt
51
+ 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.'
52
+ ```
53
+ NSArrayではダメだとかいてあるのですが、RealmSwift公式ドキュメントでは以下のようにDictionaryで渡しています。
54
+ ```swift
55
+ // (2) Dictionaryの値を使ってDogクラスのオブジェクトを作成する
56
+ let myOtherDog = Dog(value: ["name" : "Pluto", "age": 3])
57
+ ```
58
+
59
+ また私のコード上では以下のようにしてインスタンスを生成しています。
60
+ ```swift
61
+ let questions = json["questions"].arrayObject
62
+
63
+ for subJson in questions! {
64
+ let question: Question = Mapper<Question>().map(subJson)!
65
+ self.questions.append(question)
66
+ }
67
+ ```
68
+
69
+
49
70
  このRealmモデルは下記のGistを参考に作りました。
50
71
 
51
72
  [Transform arrays with ObjectMapper to Realm's List type](https://gist.github.com/Jerrot/fe233a94c5427a4ec29b)