【问题标题】:"No Results" When No face shows up (Swift) -Simple“没有结果”当没有面孔出现时(Swift)-简单
【发布时间】:2016-04-16 11:14:23
【问题描述】:

我制作了一个应用程序,您可以在其中输入知名人士,然后他们的脸是从维基百科中提取的。是的,当您输入具有维基百科页面的名称时,一切正常。但是,如果您输入其他没有结果的内容,我需要有一些内容显示“没有结果”。我的代码在下面,但永远不会调用“无结果”,即使没有结果也是如此。

 func textFieldShouldReturn(textField: UITextField) -> Bool {
    realLoadingLbl.alpha = 1

    textField.resignFirstResponder()
  faceImageView.alpha = 0


    self.realLoadingLbl.text = "Loading...Trying to Grab Photo..."



    if timerRunning == false{
        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: ("Counting"), userInfo: nil, repeats: true)
        timerRunning = true
    }





    if let textFieldContent = textField.text{
        do {
            try WikiFaceRec.faceForPerson(textFieldContent, size: CGSize(width: 200, height: 250), completion: {(image:UIImage?, imageFound:Bool!) -> ()  in
                if imageFound == true{
                    self.timerCount = 6
                    self.timerRunning = false
                   self.realLoadingLbl.alpha = 0
                    dispatch_async(dispatch_get_main_queue(), {() -> Void in
                        self.faceImageView.image = image
                        self.faceImageView.alpha = 1
                        WikiFaceRec.centerImageViewOnFace(self.faceImageView)
                    })
                    if imageFound == false{
                        self.faceImageView.alpha = 0
                        self.realLoadingLbl.text = "No Results Found. Check your spelling and try again."
                        print("NO RESULTS")

                    }
                }
        })
        }catch WikiFaceRec.WikiFaceError.CouldNotDownloadImage{
        print("Wikipedia not currently open")
        }catch{
            print("error")
            self.faceImageView.alpha = 0
            self.realLoadingLbl.text = "No Results Found. Check your spelling and try again."
            print("NO RESULTS")
        }

    }

    return true
}

【问题讨论】:

  • 设置一些断点,看看你的程序在无法加载图像时实际在做什么。您目前有 2 个区域“无结果”。除非您能提供更多信息,否则我们可能需要了解“faceForPerson”的实际作用。
  • imageFOund == false 永远不会被调用。
  • 那么 Bool 总是正确的。也许改为检查图像?这是一个可选的,所以如果它返回为空,则没有图像。这里有 3 个级别的错误检查。你有一个 try/catch,如果没有返回图像,我假设它会引发错误,那么你有 Bool,也可以检查 UIImage。这有意义吗?
  • 在 y FaceRecognition 类中,它确实说 imageFound = false 没有图像
  • 天哪。我认为您在前一个 if 语句中嵌入了另一个 if 语句

标签: ios swift uiimageview boolean wikipedia


【解决方案1】:

您的if imageFound = false 语句包含在您的if imageFound = true 语句中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    相关资源
    最近更新 更多