Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 一段看似无辜的swift代码,编译时间很长。缺陷_Ios_Swift_Compilation - Fatal编程技术网

Ios 一段看似无辜的swift代码,编译时间很长。缺陷

Ios 一段看似无辜的swift代码,编译时间很长。缺陷,ios,swift,compilation,Ios,Swift,Compilation,我的swift项目未编译: 进度条在那里停留了很长时间。起初,我认为这是由字典文字错误引起的。但不,不是 在长时间删除代码以查看其是否编译后,我终于找到了问题所在。这段代码正是导致编译器崩溃的原因 if let desc = (operation as? CustomStringConvertible)?.description { helpString += "<p>\(NSLocalizedString(desc, comment: ""))</p>" }

我的swift项目未编译:

进度条在那里停留了很长时间。起初,我认为这是由字典文字错误引起的。但不,不是

在长时间删除代码以查看其是否编译后,我终于找到了问题所在。这段代码正是导致编译器崩溃的原因

if let desc = (operation as? CustomStringConvertible)?.description {
    helpString += "<p>\(NSLocalizedString(desc, comment: ""))</p>"
}
如果let desc=(操作为?CustomStringConvertible)?。说明{
helpString+=“\(NSLocalizedString(描述,注释:”)

” }
没有字典/元组/数组文本,只有简单的强制转换和可选绑定。这会导致整个东西卡住

为此,创建一个测试项目或其他东西。将新文件添加到项目中,并将以下代码添加到文件中:

import UIKit

protocol Operation {
    var operationName: String { get }
    var operationAvailableInputs: [(name: String, desc: String)] { get }
    var operationRejectFloatingPoint: Bool { get }
    var operationImage: UIImage? { get }

    func calculate (inputs: [String: Double]) -> [(name: String, from: String, result: String)]?
}

class OperationViewController: UITableViewController, UITextFieldDelegate, UIGestureRecognizerDelegate {
    var operation: Operation!


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showOperationHelp" {
            let vc: UIViewController = segue.destinationViewController
            var helpString: String = "<h1>\(NSLocalizedString(operation.operationName, comment: ""))</h1>"

            if let desc = (operation as? CustomStringConvertible)?.description {
                helpString += "<p>\(NSLocalizedString(desc, comment: ""))</p>"
            }

            helpString += "<ul>"

            for (name, desc) in operation.operationAvailableInputs {
                helpString += "<li>\(name): \(NSLocalizedString(desc, comment: "").stringByReplacingOccurrencesOfString("ANGLE_MEASURE", withString: NSLocalizedString("Radians", comment: "")))</li>"
            }
            helpString += "</ul>"
            helpString += operation.operationRejectFloatingPoint ? NSLocalizedString("This operation accepts integers ONLY. Other forms of input will be ignored", comment: "") : ""
        } else if segue.identifier == "showResults" {
        }
        view.endEditing(true)
    }
}
导入UIKit
协议操作{
var operationName:字符串{get}
变量操作AvailableInputs:[(名称:String,描述:String)]{get}
var operationRejectFloatingPoint:Bool{get}
var-operationImage:UIImage?{get}
func计算(输入:[String:Double])->[(名称:String,发件人:String,结果:String)]?
}
类操作ViewController:UITableViewController、UITextFieldDelegate、UIgestureRecognitizerDelegate{
var操作:操作!
覆盖函数prepareforsgue(segue:UIStoryboardSegue,sender:AnyObject?){
如果segue.identifier==“showOperationHelp”{
让vc:UIViewController=segue.destinationViewController
var helpString:String=“\(NSLocalizedString(operation.operationName,comment:)”
如果让desc=(操作为?CustomStringConvertible)?。说明{
helpString+=“\(NSLocalizedString(描述,注释:”)

” } 帮助字符串+=“
    ” 对于operation.operationAvailableInputs中的(名称,描述){ helpString+=“
  • \(名称):\(NSLocalizedString(描述,注释:)。StringByReplacingOfString(“角度度量”,带字符串:NSLocalizedString(“弧度”,注释:)))
  • ” } 帮助字符串+=“
” helpString+=operation.operationRejectFloatingPoint?NSLocalizedString(“此操作仅接受整数。其他形式的输入将被忽略”,注释:“”): }如果segue.identifier==“showResults”,则为else{ } view.endEditing(真) } }
只有36行代码。现在,使用cmd+B构建项目,您将看到编译被卡在这个文件上。但是,如果注释掉上面提到的行(第21-23行),代码将成功编译

这是Swift编译器中的错误吗?我应该提交错误报告吗?另外,就目前而言,我能做些什么来达到同等的效果

附言

“同等物”的含义:


操作
是一种协议。一些符合协议的类也符合
CustomStringConvertible
。我想检查
操作
是否符合
CustomStringConvertible
,以便我可以将操作的
说明
添加到
帮助字符串

中,我怀疑它是文本字符串中未替换的“”。与其对NSLocalizedString使用字符串插值,不如使用“+”或使用string.format将其连接起来。如果操作是CustomStringConvertible,为什么不使用
?CustomStringConvertible
,因为此强制转换操作始终成功。这里的问题是,我只写了
op is/as CustomStringConvertible
,构建被卡住了。为什么我不需要它呢@KleinMioke
操作
协议不符合
CustomStringConverable
@是的,我知道。因为我第一次写的
操作是什么?CustomStringConvertible
编译器给我一个警告,说
总是成功