Swift返回可选值

时间:2017-05-22 02:25:53

标签: php ios swift

我目前正在开发一个Xcode应用程序,允许用户通过向MySQL数据库发送数据进行注册。我只有几个问题。

我遇到的第一个问题是在第10,13,15行。我发出警告说:“字符串插值产生可选值的调试描述;你的意思是明确说明吗?”无论如何我可以更改我的代码,以便该值不是可选的吗?

此外,发送的响应字符串的措辞如下:“responseString = Optional(此用户名可用)”。我打算使用响应字符串向用户显示信息。反正我是否可以摆脱括号以及可选词?

@IBAction func TextFieldEditingDidChange(_ sender: Any) {
    let request = NSMutableURLRequest(url: NSURL(string: "usernamecheck.php")! as URL)
    request.httpMethod = "POST"
    print("Request: \(request)")
    let postString = "username=\(usernameTxt.text!)"
    request.httpBody = postString.data(using: String.Encoding.utf8)
    let task = URLSession.shared.dataTask(with: request as URLRequest) {
        data, response, error in
        if error != nil {
            print("error=\(error)")
            return
        } // Ends errror If statements
        print("response = \(response)")
        let responseString = NSString(data: data!, encoding:
            String.Encoding.utf8.rawValue)
        print("responseString = \(responseString)")   
    }   
    task.resume()   
}

部分PHP文件:

$a_sql = mysqli_query($con, "SELECT username FROM users WHERE username = '" . mysqli_real_escape_string($_POST['username']) . "' ; ");  
if (empty($_POST['username'])) {   
    $username_error = "Please input username";   
} else {   
    $a_sql;   
}
if ($a_sql && mysqli_num_rows($a_sql) > 0) {   
    $username_exists = "Username is already taken.";   
    echo $username_exists;   
} else {   
    echo "Fail";   
}

3 个答案:

答案 0 :(得分:1)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav id="jmenu">
  <div>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
  </div>
  <ul style="width:80%;border:1px">
    <li class="no-sub" id="index.php"><a class="top-heading" href="index.php">首页</a></li>
    <li align="left" tabindex="0" class="" style="z-index: 1;"><a class="top-heading" id="H00000001" aria-haspopup="true">你不可不知</a><i class="caret"></i>
      <div class="dropdown" style="transition: none 0s ease 0s ; height: auto;">
        <div class="dd-inner">
          <ul class="column">
            <li id="M00000001" style=""><a href="news.php?id=M00000001" style="">网络安全</a></li>
            <li id="M00000002" style=""><a href="news.php?id=M00000002" style="">青少年</a></li>
            <li id="M00000003" style=""><a href="news.php?id=M00000003" style="">亲子关系</a></li>
            <li id="M00000004" style=""><a href="news.php?id=M00000004" style="">夫妻关系</a></li>
            <li id="M00000005" style=""><a href="news.php?id=M00000005" style="">情侣关系</a></li>
            <li id="M00000006" style=""><a href="news.php?id=M00000006" style="">同性恋</a></li>
            <li id="M00000007" style=""><a href="news.php?id=M00000007" style="">色情危害</a></li>
            <li id="M00000008" style=""><a href="news.php?id=M00000008" style="">其他</a></li>
          </ul>
        </div>
      </div>
    </li>
    <li align="left" tabindex="0" style="z-index: 1;" class=""><a class="top-heading" id="H00000002" aria-haspopup="true">我们的活动</a><i class="caret"></i>
      <div class="dropdown" style="transition: none 0s ease 0s ; height: auto;">
        <div class="dd-inner">
          <ul class="column">
            <li id="M00000009" style=""><a href="news.php?id=M00000009" style="">当前活动</a></li>
            <li id="M00000010" style=""><a href="news.php?id=M00000010" style="">活动历史</a></li>
            <li id="M00000011" style=""><a href="news.php?id=M00000011" style="">讲员资料</a></li>
            <li id="M00000012" style=""><a href="news.php?id=M00000012" style="">活动照片</a></li>
          </ul>
        </div>
      </div>
    </li>
    <li align="left" tabindex="0" class="" style="z-index: 1;"><a class="top-heading" id="H00000003" aria-haspopup="true">资料库</a><i class="caret"></i>
      <div class="dropdown" style="transition: none 0s ease 0s ; height: auto;">
        <div class="dd-inner">
          <ul class="column">
            <li id="M00000013" style=""><a href="news.php?id=M00000013" style="">教材下载</a></li>
            <li id="M00000014" style=""><a href="news.php?id=M00000014" style="">活动宣传材料</a></li>
            <li id="M00000015" style=""><a href="news.php?id=M00000015" style="">讲座会材料</a></li>
            <li id="M00000016" style=""><a href="news.php?id=M00000016" style="">短片,动画,影片</a></li>
            <li id="M00000017" style=""><a href="news.php?id=M00000017" style="">广播节目</a></li>
          </ul>
        </div>
      </div>
    </li>
    <li align="left" tabindex="0" class="" style="z-index: 1;"><a class="top-heading" id="H00000004" aria-haspopup="true">真人见证</a><i class="caret"></i>
      <div class="dropdown" style="transition: none 0s ease 0s ; height: auto;">
        <div class="dd-inner">
          <ul class="column">
            <li id="M00000018" style=""><a href="news.php?id=M00000018" style="">单亲妈妈</a></li>
            <li id="M00000019" style=""><a href="news.php?id=M00000019" style="">其他分享</a></li>
          </ul>
        </div>
      </div>
    </li>
    <li align="left" tabindex="0" class="" style="z-index: 1;"><a class="top-heading" id="H00000005" aria-haspopup="true">新闻案列</a><i class="caret"></i>
      <div class="dropdown" style="transition: none 0s ease 0s ; height: auto;">
        <div class="dd-inner">
          <ul class="column">
            <li id="M00000034" style=""><a href="news.php?id=M00000034" style="">2017</a></li>
            <li id="M00000035" style=""><a href="news.php?id=M00000035" style="">2016</a></li>
            <li id="M00000036" style=""><a href="news.php?id=M00000036" style="">2018</a></li>
          </ul>
        </div>
      </div>
    </li>
    <li align="left" tabindex="0" class="" style="z-index: 1;"><a class="top-heading" id="H00000006" aria-haspopup="true">推荐</a><i class="caret"></i>
      <div class="dropdown" style="transition: none 0s ease 0s ; height: auto;">
        <div class="dd-inner">
          <ul class="column">
            <li id="M00000020" style=""><a href="news.php?id=M00000020" style="">举报不良内容</a></li>
            <li id="M00000021" style=""><a href="news.php?id=M00000021" style="">软体推荐</a></li>
            <li id="M00000022" style=""><a href="news.php?id=M00000022" style="">性上瘾检测表</a></li>
            <li id="M00000023" style=""><a href="news.php?id=M00000023" style="">祷告文</a></li>
            <li id="M00000024" style=""><a href="news.php?id=M00000024" style="">网络安全研习会表格下载</a></li>
            <li id="M00000025" style=""><a href="news.php?id=M00000025" style="">关怀同性恋者辅导员培训会表格下载</a></li>
          </ul>
        </div>
      </div>
    </li>
    <li align="left" tabindex="0" class="" style="z-index: 1;"><a class="top-heading" id="H00000007" aria-haspopup="true">关于我们</a><i class="caret"></i>
      <div class="dropdown" style="transition: none 0s ease 0s ; height: auto;">
        <div class="dd-inner">
          <ul class="column">
            <li id="M00000026" style=""><a href="news.php?id=M00000026" style="">计划缘起</a></li>
            <li id="M00000027" style=""><a href="news.php?id=M00000027" style="">计划目标</a></li>
            <li id="M00000028" style=""><a href="news.php?id=M00000028" style="">执行单位和人力情形</a></li>
            <li id="M00000029" style=""><a href="news.php?id=M00000029" style="">执行步骤</a></li>
            <li id="M00000030" style=""><a href="news.php?id=M00000030" style="">预订执行进度</a></li>
            <li id="M00000031" style=""><a href="news.php?id=M00000031" style="">实施对象和期程,预期效益</a></li>
            <li id="M00000032" style=""><a href="news.php?id=M00000032" style="">XOX标志</a></li>
          </ul>
        </div>
      </div>
    </li>
    <li id="recmd" class="no-sub" style="color: unset; background-color: unset;">
      <a class="top-heading" href="news.php?id=news.php?id=R" style="vertical-align: middle;">
        <p1 style="color: rgb(231, 255, 0);">推</p1>
        <p2 style="color: rgb(0, 237, 255);">荐</p2>
      </a>
    </li>
    <li class="no-sub" id="news.php?id=A"><a class="top-heading" href="news.php?id=news.php?id=A">关于我们</a></li>
  </ul>

</nav>

这是打开选项的方法之一,但最终取决于你想做什么......

你应该真正阅读Swift中的选项。这是一个非常容易和初学者的话题,每个人都在学习Swift应该学习

答案 1 :(得分:0)

如果让Message = Message {     打印(消息) }

答案 2 :(得分:0)

guard let message = responseString else {
    return
}
print(message)

实际上,你正在向消息展开一个可选的响应字符串,现在如果它包含nil,它将不会给出任何异常,它将落入其他情况。 另一方面,该值将存储在消息中并可以进一步使用

相关问题