0

Here is the situation..

I've inserted a whole NSArray in one field of my SQLite table, now I retrieve the data and it becomes a string. How can I convert it into NSArray again?

This is the contents of my NSArray inserted in SQLite:

"<tbl_news:annyfksM8D:(null)> {\n    \"news_content\" = \"Lorem ipsum dolor sit\";\n    \"news_date\" = \"Sep 23, 2013\";\n    \"news_featuredimage\" = \"uploads/1373124691956-160x120.jpg\";\n    \"news_title\" = \"Lorem Ipsum2\";\n}",

    "<tbl_news:fEgOpdgflF:(null)> {\n    \"news_content\" = \"Lorem ipsum dolor sit.\";\n    \"news_date\" = \"Sep 23, 2013\";\n    \"news_featuredimage\" = \"uploads/2560x1440.jpg\";\n    \"news_title\" = \"Lorem Ipsum3\";\n}"

It is an array from parse. Can anyone know how will i be able to convert it to NSArray. I've already tried Convert NSString to NSArray but i think doing the solution there will no solve my problem.

Community
  • 1
  • 1
khatzie
  • 2,509
  • 4
  • 22
  • 37
  • Choose a delimiter, split the string on that and then add the individual objects to your array (which might be better suited as `NSMutableArray`) with something like `[yourArray addObject:yourObject]`. – Robert Oct 16 '13 at 08:48
  • 2
    There *is no way* to convert that string back to an array *reliably*. It seems that you just used the `description` method to convert the array to a string for storing it in SQLite, which is not a good idea. You should better use a suitable format instead, for example convert the array to JSON, which can be converted back to an array. – Martin R Oct 16 '13 at 09:05
  • Actually, what you inserted was the `description` of an NSDictionary. Toss what you have and do it right. – Hot Licks Oct 22 '13 at 16:13

1 Answers1

-1

Don't store your NSArray as a string into your database. Convert it to NSData, store it as a blob, retrieve it back as NSData, and then convert that back into an NSArray:

// Convert NSArray to NSData
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
...

// Store it as a blob in sqlite
sqlite3_bind_blob(insert_statement, 1, [data bytes], [data length], NULL);

...

// Get the bytes back fromthe database and convert it into an NSArray
NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data]
jnortey
  • 1,625
  • 2
  • 10
  • 17
  • If you had taken the time to read my answer, you'd see that I'm not telling you how to convert the string back into an array. You are currently storing the DESCRIPTION of the array which is a STRING. I'm suggesting that you instead convert the array to NSDATA which can be stored as a BLOB, retrieved, and converted back into an NSARRAY. @Hot Licks - The fact that it is actually a dictionary doesn't change the answer. A dictionary can still be converted into NSData as long as the contents are basic types or implements encodeWithCoder. – jnortey Oct 23 '13 at 03:57