用于方向步骤循环的Swift 2的语法

时间:2016-06-20 09:44:58

标签: swift loops swift2

任何人都可以帮助Swift 2中的这个循环的语法。代码来自一个非常有用的教程(http://www.devfright.com/category/map-kit-framework/mkdirectionsrequest/),它涵盖了路径方向,但它在Objective C中,我知道有很多元素是现已弃用,例如++。我在将前两行代码转换为Swift 2时遇到了问题。任何帮助都非常感激。

        for (int i = 0; i < routeDetails.steps.count; i++) {
            MKRouteStep *step = [routeDetails.steps objectAtIndex:i];
            NSString *newStep = step.instructions;
            self.allSteps = [self.allSteps stringByAppendingString:newStep];
            self.allSteps = [self.allSteps stringByAppendingString:@"\n\n"];
            self.steps.text = self.allSteps;


        }

3 个答案:

答案 0 :(得分:1)

如果您不需要显式索引,请忘记索引循环。

if (window.getSelection) {
    if (window.getSelection().empty) {  // Chrome
        window.getSelection().empty();
    } else if (window.getSelection().removeAllRanges) {  // Firefox
        window.getSelection().removeAllRanges();
    }
} else if (document.selection) {  // IE?
    document.selection.empty();
}

或仍 swiftier

for step in routeDetails.steps {
    let newStep = step.instructions
    allSteps += "\(newStep)\n\n"
    steps.text = allSteps
}

答案 1 :(得分:0)

在Swift 2中:

for i in 0..< routeDetails.steps.count {

    var step: MKRouteStep = routeDetails.steps[i]
    var newStep: String = step.instructions
    self.allSteps = self.allSteps.stringByAppendingString(newStep)
    self.allSteps = self.allSteps.stringByAppendingString("\n\n")
    self.steps.text = self.allSteps
}

答案 2 :(得分:0)

对于使用route.step说明的任何其他人,我将其放置在地图上的文本字段中。非常感谢Vardian解决这个问题。

    self.allSteps += route.steps.map({$0.instructions}).joinWithSeparator("\n\n")
    self.directionsText.text = self.allSteps