ios - 如何在其正文中解开守卫声明?
<p><p>我的错误陈述是:</p>
<p>在“保护条件”中声明的变量在其主体中不可用</p>
<p>我的代码是:</p>
<pre><code> extension ViewController {
func uploadImage(image: UIImage, progress: (percent: Float) -> Void,
completion: (tags: , colors: ) -> Void) {
guard let imageData = UIImageJPEGRepresentation(image, 0.5) else {
Alamofire.upload(
.POST,
"http://api.imagga.com/v1/content",
headers: ["Authorization" : "Basic xxx"],
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data: imageData, name: "imagefile",
fileName: "image.jpg", mimeType: "image/jpeg")
}
</code></pre>
<p>以上是程序的一部分。 </p>
<p>错误发生在包含“data: imageData”的行中</p>
<p>提前致谢!</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>考虑这个 <strong>guard</strong> 示例:</p>
<pre><code>guard let variable = optionalVariable else { return }
// Variable is safe to use here
</code></pre>
<p>还有这个 <strong>if</strong> 例子:</p>
<pre><code>if let variable = optionalVariable {
// Variable is safe to use here
}
</code></pre>
<p>在您的情况下,您将这两个概念混为一谈。您将 <strong>guard</strong> 用作 <strong>if</strong> 语句。您可以将警戒更改为 if,或将代码移到 elseblock 之外。</p>
<p><strong>guard</strong> 语句可能有点困惑!考虑将其用作循环内的 <strong>continue</strong> 语句。</p></p>
<p style="font-size: 20px;">关于ios - 如何在其正文中解开守卫声明?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/39245779/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/39245779/
</a>
</p>
页:
[1]