duanpeng1532 2014-08-19 18:15
浏览 89
已采纳

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);
    }


}


?>
  • 写回答

2条回答 默认 最新

  • douyi8732 2014-08-19 19:00
    关注

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效