2

I am having some trouble using JSONModel in Swift.

I am trying to create a ToDo list app that would persist a collection of items so that the ToDo items are preserved when the app is closed. This is the code I use:

class ToDoItem: JSONModel {
    var name: String = ""
    var isCompleted: Bool = false
    var createdOn: NSDate = NSDate()
}

class ToDoList: JSONModel {
    var items: [ToDoItem] = []
}

I can convert a ToDoItem to JSON by calling toJSONString() but the same method doesn't work with ToDoList, it returns nil. Any idea why is this happening?

Sterbic
  • 213
  • 5
  • 15
  • unrelated to your question but I think you need to set `createdOn` in your init. What you currently have will use the same value for every ToDoItem (…at least I'm pretty sure - haven't run this code) – Jiaaro Jul 14 '14 at 17:55
  • Those are just placeholder values. I use the init of the superclass and than manually fill the values in. I am new to the iOS ecosystem but this is how you usually handle things when doing persistence in Java. – Sterbic Jul 14 '14 at 18:00
  • I still didn't test JSONModel with Swift. Provided that things are rather unstable in Swift I guess I will wait a bit before implementing Swift compatibility – Marin Todorov Jul 14 '14 at 20:24
  • Luka, did you manage to solve this issue somehow? – vburojevic Sep 05 '14 at 09:04
  • Yes, by writing my own serialization routines... Last time I checked a few weeks ago there was still no way of getting this thing to work properly. – Sterbic Sep 05 '14 at 15:55
  • Hey Luka, did you get any solution, am facing same problem? Can you post some code? – Dilip Lilaramani Jun 10 '15 at 16:52
  • Hey, the last time I check there was still no workaround. I haven't coded much in Swift in the last few months so a solution I'm not aware of may have been found. If you need a library for handling JSON objects try using SwiftyJSON https://github.com/SwiftyJSON/SwiftyJSON – Sterbic Jun 11 '15 at 15:53

2 Answers2

2

JSONModel does not support Swift due to incompatibilities with the reflection supported by the Obj-C runtime. This reflection ability is currently required by JSONModel in order to resolve types correctly. We are looking into alternative methods of defining the type mappings though.

Specifically, JSONModel relies on the use of protocols to determine the type of items in collection types, such as dictionaries, arrays, etc. Protocols defined in Swift are not visible at runtime - preventing JSONModel from deserializing collection types correctly.

For the time being, you have two options:

  1. switch from JSONModel to another JSON (de)serialization library with support for Swift
  2. define your models in Objective-C

I know this isn't ideal, but I'm afraid a workaround is not possible with the current JSONModel behavior.

James Billingham
  • 760
  • 8
  • 32
-1

If your array is a member of some other class you can use a helper method.

Data class

@protocol Transaction
@end

@interface Transaction : JSONModel
  ...
@end

A "wrapper" class

@interface TransactionPage : JSONModel

@property (strong, nonatomic) NSNumber* pageNumber;
@property (strong, nonatomic) NSNumber* pageSize;
@property (strong, nonatomic) NSNumber* totalRecords;
@property (strong, nonatomic) NSNumber* totalPages;
@property (strong, nonatomic) NSArray<Transaction>* records;

- (NSArray<TphTransactionSummary*>*) recordsForSwift;  // <<< Helper method

@end

Implementation on the helper method

- (NSArray<TphTransactionSummary*>*) recordsForSwift {
    return self.records;
}
Nikolay Nahimov
  • 810
  • 1
  • 6
  • 6