I am inserting an Array into my database as a String and after fetching it I want it to convert it again to Array. So that I can fetch my values again and I can do next operation.
Here below is my array inserting into database(TestQuestion) as a String:
let testQuestionModel : TestQuestion = NSEntityDescription.insertNewObject(forEntityName: "TestQuestion", into: AppDelegate.getContext()) as! TestQuestion
testQuestionModel.optionsArray = "\(question["options"] as! NSArray)"
Example: String Array I am getting from Database
(\n \"Rahul Abhyankar\",\n \"Pinkesh Shah\",\n \"Ramanan Ganesan\",\n \"Dr. Marya Wani\",\n \"\",\n \"\"\n)".
Here is 4 options you can see this is my string after fetching from Database.
1) Rahul Abhyankar.
2) Pinkesh Shah.
3) Ramanan Ganesan.
4) Dr. Marya Wani.
Now how can I convert it into array?
I tried some methods.
let arr = NSArray(object: quetion.optionsArray!).
But I am getting only one object. How can I get my array values same as previous from this string array?
let array:[String] = theLongString.components(separatedBy: ",")But, there is extra "\n", and extra(), so I guess there is also an issue on SAVING them. ClearytestQuestionModel.optionsArray = "\(question["options"] as! NSArray)"isn't the good way.(\n \"Rahul Abhyankar\",\n \"Pinkesh Shah\",\n \"Ramanan Ganesan\",\n \"Dr. Marya Wani\",\n \"\",\n \"\"\n)".Doesn't look like a valid string.