arrays - 如何从for循环中取出结果

标签 arrays swift

我在 swift 中有一个函数,并且在其中使用了 for 循环。

    let pointTF = [emailTF,passTF,resetEmailTF]

    for i in 0...pointTF.count
    {
        return coachMarksController.helper.makeCoachMark(for: pointTF[i])
    }

我想在循环之外返回这个值?我怎样才能做到这一点?请帮忙

最佳答案

假设上面的代码包装在一个函数中,您可以在 for 循环范围之外定义一个 CoachMark 对象数组,并将生成的 coachMark 附加到该数组中:

var coachMarks: [CoachMark]()

for i in 0...pointTF.count
{
    var cm = coachMarksController.helper.makeCoachMark(for: pointTF[i])
    coachMarks.append(cm)
}

//Then you can return a single CoachMark:
return coachMarks[1]

关于arrays - 如何从for循环中取出结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49527551/

相关文章:

ios - Swift ViewModel 与 UITableViewCell 的关联类型

c++ - 如何用模板类型初始化成员变量数组?

arrays - 无法将常量(结构)类型附加到数组

php - 检查 SQL 表上的数组是否有重复项

java - 如何计算数组中的特定字符 - Java

Javascript elements.length 返回错误的计数?

ios - 如何在具有两个不同单元格的 TableView 中添加一行

ios - UIButton 不响应触摸

ios - 解析 json 数组 swift 返回空元素

ios - 登录后自动将用户导航到 Swift 3.0 中的新 ViewController