首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSData (if语句)

NSData (if语句)
EN

Stack Overflow用户
提问于 2014-08-19 18:15:22
回答 2查看 98关注 0票数 1

我试图在数据库中过滤搜索结果。

我在我的应用程序中实现了一个条形码扫描仪。现在,当我扫描时,它扫描条形码并发出警报。按钮索引1导致这个NSData调用:

代码语言:javascript
复制
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

所以,我要做的是:如果扫描的项目不在数据库中,那么数据库的segue使用标识符scanMissing;当然,如果扫描的项在数据库中,那么它就使用scanSuccess。

代码语言:javascript
复制
if(data == nil)
{

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

}
else
{

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

}
}

我的问题是,我不能使用零,因为有一些字节仍然在传输。设置断点时,我获得数据的下列信息:

数据(_NSInlineData) 2字节

我是否必须将if语句更改为2字节才能使其工作?或者我该怎么做?

编辑: PHP代码:

代码语言:javascript
复制
$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);
    }


}


?>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-19 19:00:21

PHP代码输出一个JSON编码的数组。因此,最小的可能响应是[]。无论如何,它不可能是nil

您可以尝试将数据与[]进行比较,也可以解码JSON并检查数组的长度。第二个选择是迄今为止最可靠的国际水文学组织。

代码语言:javascript
复制
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
}

(还没有测试)。

票数 1
EN

Stack Overflow用户

发布于 2014-08-19 19:16:13

您应该通过PHP来处理这个问题。

如果你发现了什么

代码语言:javascript
复制
{
    "Success" : true,
    "Response" : {
        "ID": 1234,
        "Name":"Name string"
    }
}

如果不是

代码语言:javascript
复制
{
    "Success" : false,
    "Response" : {
    }
}

另外,您也可以尝试使用JSONModel

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25390271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档