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

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条