Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Now that you've included your JSON sample, I can see another problem: Your JSON is an array of three arrays, an array of categories, an array of products, and an array of versions. That is a questionable structure (usually an array would be of homogeneous items of the same type). Rather than an array of arrays, I'd make it a dictionary of arrays.</p> <p>This confusing structure in your JSON manifests itself in your Objective-C code. You're trying to iterate through the <code>userdata</code> array, but you can't, because each of the three item is different. </p> <p>So let me suggest two possible fixes. First you if you stick with your array of arrays (which I'm not crazy about), you cannot iterate through that array with a <code>for</code> loop, but you could extract the three arrays it contains like so:</p> <pre><code>NSArray *categories = userdata[0]; NSArray *products = userdata[1]; NSArray *versions = userdata[2]; </code></pre> <p>Now you can iterate through each of those three arrays.</p> <p>Personally, I'd take this a step further and change that PHP that generates the JSON to generate a top level dictionary, e.g. </p> <pre><code>&lt;?php require_once('database_connection.php'); $i = 0; $j = 0; $k = 0; $l = 0; mysql_query('SET CHARACTER SET utf8') or die("MYSQL character set error: ".mysql_error()); $result = array(); $sql=mysql_query("SELECT * FROM categories ORDER BY id ASC") or die(mysql_error()); if(mysql_num_rows($sql) &gt; 0) { while($res=mysql_fetch_array($sql, MYSQL_ASSOC)){ $result['categories'][$i] = $res; $art_sql=mysql_query("SELECT * FROM product WHERE cat_id=" .$res['id']. " ORDER BY id ASC") or die(mysql_error()); if (mysql_num_rows($art_sql) &gt; 0){ while($art_res=mysql_fetch_array($art_sql, MYSQL_ASSOC)){ $result['products'][$k] = $art_res; $k = $k+1; } } $i= $i+1; } $version_sql = mysql_query("SELECT * FROM version_app order by product_id desc limit 1") or die(mysql_error()); $row = mysql_fetch_array($version_sql); $last_version = $row['product_id']; $result['versions'][$l] = array('product_id' =&gt; $last_version); $l = $l+1; } $str_enc = json_encode($result); // note, these str_replace lines are not needed // $str=str_replace('\r','',$str_enc); // $str=str_replace('\t','',$str); // $str=str_replace('\n','',$str); // this stripslashes is a really bad idea, though // $str = stripslashes($str); // you echoed `$str`, but I'll obviously echo @str_enc echo $str_enc; mysql_close(); ?&gt; </code></pre> <p>And if you did that, you could then retrieve your three arrays with</p> <pre><code>NSArray *categories = userdata[@"categories"]; NSArray *products = userdata[@"products"]; NSArray *versions = userdata[@"versions"]; </code></pre> <p>It's not a mission critical change, but it's a more logical representation for three heterogeneous items. But the idea is the same: Extract your three arrays from your JSON, and now you can iterate through them, respectively.</p> <hr> <p>A couple of SQLite-related issues leap out at me</p> <ol> <li><p>One issue is that you have a line of SQL that says:</p> <pre><code>SELECT id FROM categories where cat_id = '%@' </code></pre> <p>But you then proceed to try to read four columns worth of data (even though you only returned one). And even if you changed your SQL to actually return four columns of data, you should really check the <code>sqlite3_column_blob</code> and <code>sqlite3_column_bytes</code> calls to make sure there was something to populate the <code>NSData</code>.</p> <p>As an aside, it's generally safer to not use <code>stringWithFormat</code> when building a SQL statement, but rather use <code>?</code> placeholders instead of printf-formatters and then use <code>sqlite3_bind_xxx</code> functions.</p></li> <li><p>You <code>getMysofas</code> is repeatedly opening the database, but never closing it. Have one <code>sqlite3_close</code> for every <code>sqlite3_open</code> call. Likewise, you should have one <code>sqlite3_finalize</code> statement for each <code>sqlite3_prepare_v2</code> line.</p></li> </ol> <hr> <p>Beyond that you have to identify the source of the crash by either using an <a href="http://developer.apple.com/library/ios/recipes/xcode_help-breakpoint_navigator/articles/adding_an_exception_breakpoint.html" rel="nofollow">exception breakpoint</a>, single-stepping through this code in the debugger, or putting a bunch of <code>NSLog</code> statements in there. See Ray Wenderlich's series <a href="http://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1" rel="nofollow">My App Crashed, Now What?</a>. But you really need to identify the line of code that is causing the problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload