NSData(if语句)

问题描述:

I'm trying to filter search results within a database.

I have a barcode scanner implemented in my iOS application. Now when I scan, it scans the barcode and brings up an alert. Button Index 1 leads to this NSData call:

NSString *salesStr = @"http://10.0.0.1:";
salesStr = [salesStr stringByAppendingString:@"8080"];
salesStr = [salesStr stringByAppendingString:@"/barcode.php?password="];
salesStr = [salesStr stringByAppendingString:@"test"];
salesStr = [salesStr stringByAppendingString:@"&db="];
salesStr = [salesStr stringByAppendingString:@"testDB"];
salesStr = [salesStr stringByAppendingString:@"&barNum="];
salesStr = [salesStr stringByAppendingString:self.scannedItem];

NSURL * url = [NSURL URLWithString:salesStr];
NSData * data = [NSData dataWithContentsOfURL:url];

10.0.0.1:8080/barcode.php?password=test&db=testDB&barNum=0123456789012

So what I'm trying to do is: If the scanned item is not in the database, then the database segues using the identifier scanMissing; and of course if the scanned item is in the database, then it uses scanSuccess.

if(data == nil)
{

    [self performSegueWithIdentifier:@"scanMissing" sender:self];

}
else
{

    [self performSegueWithIdentifier:@"scanSuccess" sender:self];

}
}

My problem is that I can't use nil, because there are some bytes that are still transferring. When I set up breakpoints, I get the following information for data:

data (_NSInlineData) 2 bytes

Do I have to change the if statement to 2 bytes to make it work? Or what do I do?

EDIT: PHP CODE:

$host = "localhost";
$db = $_REQUEST['db'];
$user = "root";
$pass = $_REQUEST['password'];
$barcode = $_REQUEST['barNum'];

$connection = mysql_connect($host, $user, $pass);

//Check to see if we can connect to the server
if(!$connection)
{
    die("Database server connection failed.");
}
else
{
    //Attempt to select the database
    $dbconnect = mysql_select_db($db, $connection);

    //Check to see if we could select the database
    if(!$dbconnect)
    {
        die("Unable to connect to the specified database!");
    }
    else
    {
        $query = "SELECT ITEM_ID, ITEM_Kitchen_Name FROM it_titem WHERE ITEM_ID=$barcode";
        $resultset = mysql_query($query, $connection);

        $records = array();

        //Loop through all our records and add them to our array
        while($r = mysql_fetch_assoc($resultset))
        {
            $records[] = $r;
        }

        //Output the data as JSON
        echo json_encode($records);
    }


}


?>

Your PHP code outputs a JSON-encoded array. So, the smallest possible response is []. In any case it couldn't be nil.

You can try to compare data with [], or you can decode the JSON and check the length of the array. The second option is by far the most reliable IMHO.

NSError *error = nil;
NSArray *results = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

if (error != nil)
{
    // There was an error parsing the JSON, do whatever you want
}
else if (results.count == 0)
{
    // no results
}
else
{
    // results
}

(haven't tested it).

IMHO You should handle this by your PHP.

If you find something

{
    "Success" : true,
    "Response" : {
        "ID": 1234,
        "Name":"Name string"
    }
}

If not

{
    "Success" : false,
    "Response" : {
    }
}

Also, you can try use JSONModel