2

I am Getting a Dynamic Array which Consist (Dictionary with Array ) and want to increase the Index of Array and will go on to Next Dictionary on action.

After Parsing The Value in swapLibs

var currentQuizIndex = 0
func Dataparsed() {
    ServiceManager.service(ServiceType.POST, path: urslStr, param: nil, completion: {  (sucess, response, error, code) -> Void in
        if (code == 200) {
            let QuizData  = (swapLibs?.valueForKey("quiz") as! NSArray)
            let  quizData = playInfo.PlayQuizInfo(QuizData[self.currentQuizIndex] as? NSDictionary)
            self.playQuizArray.addObject(quizData)
            self.playQuizTitle?.text = quizData.quizQuestion
            self.playQImage.sd_setImageWithURL(NSURL(string: quizData.quizQImage!))
            self.QNumber?.text = "\(self.Qno)/\(self.noofQuestion)"
        }
    })
}

And The Modal is

class playInfo: NSObject {

    var quizId :           String? = ""
    var quizQId :          String? = ""
    var quizQImage :       String? = ""
    var quizQuestion :     String? = ""
    var quizType :         String? = ""
    var quizIndex :        String? = ""


    class func  PlayQuizInfo(dict: NSDictionary?) -> playInfo {

        let Pinfo = playInfo()
        Pinfo.WrapPlayQuiz(dict)
        return Pinfo
    }

    func WrapPlayQuiz(dict: NSDictionary?)  {
        if dict == nil {
            return
        }
        self.quizId           = dict!.objectForKey("quizId")    as? String
        self.quizIndex        = dict!.objectForKey("index")     as? String
        self.quizQImage       = dict!.objectForKey("QuesImage")     as? String
        self.quizQuestion     = dict!.objectForKey("question")  as? String
        self.quizType         = dict!.objectForKey("Type")      as? String
        self.quizQId          = dict!.objectForKey("questionId")      as? String
    }

}

Here is Structure

{  
   "quiz":[  
      {  
         "quizId":"7295",
         "QuesImage":"http:\/\/proprofs.com\/api\/ckeditor_images\/man-approaches-woman1(1).jpg",
         "question":"How do you know him?",
         "questionId":"216210",
         "Type":"PQ",
         "index":4,
         "keys":[  
            {  
               "answerId":"8266",
               "option":"He's in one or more of my classes, and we're good friends.",
               "AnsImage":"Image Not Available"
            },
            {  },
            {  },
            {  },
            {  }
         ]
      },
      {  },
      {  },
      {  },
      {  },
      {  },
      {  },
      {  },
      {  },
      {  },
      {  },
      {  }
   ]
}

Each Dictionary is Containing same Key As above

Any Help Will Be Appreciated.Thanks.

13
  • Can you show the response structure? Commented Aug 9, 2016 at 5:40
  • Do comment here if you update your question. Commented Aug 9, 2016 at 5:45
  • @Mr.UB You may Check it.Thanks Commented Aug 9, 2016 at 5:53
  • So you are not using keys? And you want to fill playQuizArray with all the data from the quiz array in response? Commented Aug 9, 2016 at 5:55
  • not able to store keys coz it is an array what will be the way to store it ? by declaring an Array [email protected]. Commented Aug 9, 2016 at 6:46

1 Answer 1

1

As I don't have ServiceManager at my end so I have created this code hypothetically. It might solve you issue of saving all data in to one array. It also adds keys in to array as an object.

EDIT 1 : correct QuizKey object array formation. Let me know if any kind of error occurs, as I am unable to test it at my end.

Edit 2: I have made a general ViewController its working perfectly.Try running this View Controller file and you will see the results.

class TestVC: UIViewController {

    //An Array similar to the response you are getting from the server
    var response:[AnyObject] = [
        [
            "quizId" : "1111",
            "QuesImage" : "http://proprofs.com/api/ckeditor_images/man-approaches-woman1(1).jpg",
            "question" : "How do you know him?",
            "questionId" : "216210",
            "Type" : "PQ",
            "index" : 4,
            "keys":[
                [
                    "answerId":"8266",
                    "option":"He's in one or more of my classes, and we're good friends.",
                    "AnsImage":"Image Not Available"
                ],
                [
                    "answerId":"8266",
                    "option":"He's in one or more of my classes, and we're good friends.",
                    "AnsImage":"Image Not Available"
                ],
                [
                    "answerId":"8266",
                    "option":"He's in one or more of my classes, and we're good friends.",
                    "AnsImage":"Image Not Available"
                ]
            ]
        ],
        [
            "quizId" : "2222",
            "QuesImage" : "http://proprofs.com/api/ckeditor_images/man-approaches-woman1(1).jpg",
            "question" : "How do you know him?",
            "questionId" : "216210",
            "Type" : "PQ",
            "index" : 4,
            "keys":[
                [
                    "answerId":"8266",
                    "option":"He's in one or more of my classes, and we're good friends.",
                    "AnsImage":"Image Not Available"
                ],
                [
                    "answerId":"8266",
                    "option":"He's in one or more of my classes, and we're good friends.",
                    "AnsImage":"Image Not Available"
                ],
                [
                    "answerId":"8266",
                    "option":"He's in one or more of my classes, and we're good friends.",
                    "AnsImage":"Image Not Available"
                ]
            ]
        ]
    ]

    var playQuizArray:[playInfo]! = []

    override func viewDidLoad() {
        super.viewDidLoad()

        print(response)

        for dict in response {
            self.playQuizArray.append(playInfo.PlayQuizInfo(dict as? [String:AnyObject]))
        }

        print(self.playQuizArray)

        let quiz = self.playQuizArray[0]
        print("quizId \(quiz.quizId)")
        print("keyAnswerId \(quiz.quizKeys![0].keyAnswerId)")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

class playInfo: NSObject {
    var quizId :           String? = ""
    var quizQId :          String? = ""
    var quizQImage :       String? = ""
    var quizQuestion :     String? = ""
    var quizType :         String? = ""
    var quizIndex :        String? = ""
    //quizKeys will contain quiz array
    var quizKeys :       [QuizKey]? = []

    class func  PlayQuizInfo(dict: [String:AnyObject]?) -> playInfo {

        let Pinfo = playInfo()
        Pinfo.WrapPlayQuiz(dict)
        return Pinfo
    }

    func WrapPlayQuiz(dict: [String:AnyObject]?)  {
        if dict == nil {
            return
        }
        self.quizId           = dict!["quizId"] as? String
        self.quizIndex        = dict!["index"] as? String
        self.quizQImage       = dict!["QuesImage"] as? String
        self.quizQuestion     = dict!["question"] as? String
        self.quizType         = dict!["Type"] as? String
        self.quizQId          = dict!["questionId"] as? String

        //add key object array to the quizKeys
        if let arrKeys = dict!["keys"] as? [AnyObject] {
            for arr in arrKeys {
                let key:QuizKey = QuizKey.QuizKeyInfo(arr as? [String : AnyObject])
                self.quizKeys?.append(key)
            }
        }
    }

}

class QuizKey: NSObject {
    var keyAnswerId :       String? = ""
    var keyOption :         String? = ""
    var keyAnsImage :       String? = ""

    class func  QuizKeyInfo(dict: [String:AnyObject]?) -> QuizKey {
        let QKeys = QuizKey()
        QKeys.WrapQuizKeys(dict)
        return QKeys
    }

    func WrapQuizKeys(dict: [String:AnyObject]?)  {
        if dict == nil {
            return
        }

        self.keyAnswerId      = dict!["answerId"]    as? String
        self.keyOption        = dict!["option"]     as? String
        self.keyAnsImage      = dict!["AnsImage"]     as? String
    }

}
Sign up to request clarification or add additional context in comments.

15 Comments

working Fine @Mr.UB could you please let Me know how to increase Index on Action or Button Pressed .By the way Thanks. For effort
Getting Dict in append(Key) in next qkeys.wrapQuizKeys(Dict) got nil
you mean this line? QKeys.WrapQuizKeys(dict)
done the edit u did worked. Thanks . One More Thing can You tell me which array should i return on table view and how to return it coz i have to show label and Image on Cell
return playQuizArray coz it contains are the data and you have to fetch data using : let quiz = playQuizArray [indexPath.row] and get values like cell.textLabel.text = quiz.quizQuestion
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.