简体   繁体   English

如何返回 swift 中 function 的顶部?

[英]How do I return to the top of a function in swift?

I have this function that picks a random value from a list:我有这个 function 从列表中选择一个随机值:

func pickAttraction(attractionType: Array<Attraction>) -> Attraction {
  var randAttr = attractionType.randomElement()
  if favoritesNames.contains(randAttr!.attractionName) {
    return pickAttraction(attractionType: attractionType)
  } else {
    return randAttr!
  }
}

If the random value is in the array favoritesName, I want it to go back to the top of the function and pick a new value.如果随机值在数组 favoritesName 中,我希望它 go 回到 function 的顶部并选择一个新值。

How can I make this more efficient?我怎样才能使它更有效率?

In addition, is it possible for the program to crash while force-unwrapping randAttr (on line 3) if attractionType always has at least 8 values?此外,如果attractionType类型始终具有至少 8 个值,程序是否可能在强制展开 randAttr(第 3 行)时崩溃?

The question is not worth asking.这个问题不值得问。 It'll never get you there.它永远不会让你到达那里。

func pickAttraction(from attractions: [Attraction]) -> Attraction? {
  attractions.filter { !favoritesNames.contains($0.name) }
    .randomElement()
}

Here is a sample in playground这是操场上的示例

import UIKit
class RandomAttraction:Hashable{
    static func == (lhs: RandomAttraction, rhs: RandomAttraction) -> Bool {
        return lhs.attractionName == rhs.attractionName
    }
    func hash(into hasher: inout Hasher) {
        hasher.combine(attractionName)
    }

    init(attractionName:String) {
        self.attractionName = attractionName
    }
    var attractionName:String

}

var favoritesNames = [RandomAttraction.init(attractionName: "a"),RandomAttraction.init(attractionName: "b")]

var otherNames = [RandomAttraction.init(attractionName: "c"),RandomAttraction.init(attractionName: "d")]



func pickAttraction(attractionType: Array<RandomAttraction>) -> RandomAttraction? {
    //Filters the content of the favoritesNames from attractionType
   let filteredArray = Array(Set(attractionType).subtracting(favoritesNames))
    //If count is zero return nil
    if filteredArray.count == 0{
        return nil
    }
    let randAttr = filteredArray.randomElement()
    return randAttr
}

//MARK: Usage
if let pickedAttraction = pickAttraction(attractionType: otherNames){
    print(pickedAttraction.attractionName)
}else{
    print("No attractions")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何返回Unmanaged <CGColor>!在斯威夫特 - How do I return an Unmanaged<CGColor>! in Swift 如何在Swift中将UIImageView置于“模糊效果”之上? - How do I make my UIImageView be on top of the Blur Effect in Swift? 如果将特定类型传递给Swift中的函数,如何返回nil? - how do you return nil if a certain type is passed to a function in Swift? 如何使用Swift在此Alamofire函数中返回true / false? - How can I return true/false in this Alamofire function using Swift? Swift 3如何转换此函数以返回json变量的内容 - Swift 3 How can I convert this function to return content of json variable 如何从存储在 Firestore (Swift) 中的文档返回对象 - How do I return an object from a document stored in Firestore (Swift) 在Swift中轻按返回键时,如何关闭UIAlertController? - How do I dismiss a UIAlertController when the return key is tapped in Swift? 如何返回此函数的结果? - How do I return the results of this function? 如何在函数内返回UIImage? - How do I return a UIImage inside a function? Swift:如何在AVPlayerLayer的顶部添加图片/文本,使其成为视频的一部分? - Swift: How do I add an image/text on top of my AVPlayerLayer for it to be a part of the video?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM