Skip to content

Instantly share code, notes, and snippets.

@MariuszWisniewski
Last active February 3, 2016 21:27
Show Gist options
  • Save MariuszWisniewski/249964d5f723ea871fbe to your computer and use it in GitHub Desktop.
Save MariuszWisniewski/249964d5f723ea871fbe to your computer and use it in GitHub Desktop.
//
// TestQuestions.swift
// TestWarningsSwift
//
// Created by Mariusz Wisniewski on 2/2/16.
// Copyright © 2016 Mariusz Wisniewski. All rights reserved.
//
import Foundation
import syncano_ios
@objc enum QuestionType: Int {
case MultipleChoice = 1
case YesNo = 2
case FillInTheBlank = 3
}
class Question : SCDataObject {
var text = "" //on Syncano : String
var answer = "" //on Syncano : String
var answers : [String]? = nil //on Syncano : String (or Text)
var type : QuestionType = .MultipleChoice //on Syncano : Integer
var choices: [String]? = nil //on Syncano: String
init(text:String, answer:String, type:QuestionType) {
self.type = type
self.text = text
self.answer = answer
super.init()
}
init(text:String, answers:[String], type:QuestionType) {
self.type = type
self.text = text
self.answers = answers
super.init()
}
override init() {
super.init()
}
convenience init(fillInTheBlank text:String, answer:String ) {
self.init(text: text,answer: answer,type: .FillInTheBlank)
}
convenience init(yesNo text:String, answer:String ) {
self.init(text: text,answer: answer,type: .YesNo)
}
convenience init( multipleChoice text:String, answer:String, choices:[String]) {
self.init(text: text,answer: answer,type: .MultipleChoice)
self.choices = choices
}
required init!(coder: NSCoder!) {
super.init(coder: coder)
}
required init(dictionary dictionaryValue: [NSObject : AnyObject]!) throws {
try super.init(dictionary: dictionaryValue)
}
class func __jsonArrayValueTransformer() -> NSValueTransformer {
return MTLValueTransformer(usingForwardBlock: { (answers, _, _) in
if let answers = answers as? String {
let data: NSData = answers.dataUsingEncoding(NSUTF8StringEncoding)!
let anyObj: AnyObject? = try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue: 0))
return anyObj as! [String]
}
return nil
}, reverseBlock: { (answers, _, _) in
if let answers = answers as? [String] {
let data = try? NSJSONSerialization.dataWithJSONObject(answers, options: NSJSONWritingOptions(rawValue: 0))
let string = String(data: data!, encoding: NSUTF8StringEncoding)
return string!
}
return nil
})
}
class func answersJSONTransformer() -> NSValueTransformer {
return self.__jsonArrayValueTransformer()
}
class func choicesJSONTransformer() -> NSValueTransformer {
return self.__jsonArrayValueTransformer()
}
}
//usage example
func testQuestions() {
func getQuestions() {
Question.please().giveMeDataObjectsWithCompletion { questions, error in
print("questions: \(questions), error: \(error)")
}
}
func saveQuestion() {
let question = Question(text: "asasas", answers: ["A","B", "C"], type: .MultipleChoice)
question.choices = ["1", "2", "3"]
question.saveWithCompletionBlock { _ in
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment