数组搜索功能在Swift中不起作用 [英] Array search function not working in Swift

查看:69
本文介绍了数组搜索功能在Swift中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我承认自己有点愚蠢,但经过数小时的努力,我不禁要问:

  class AGE {

static func getAge() - > Int {

var age:Int

用于dataArray中的项目{
if items.name ==Steven{
age = items.age
}

else {
age = 0
}
}

//初始化前使用的变量age - 为什么?
回报年龄
}
}

我甚至试图设定 var age:Int = 0 开头,但函数返回0 。我希望有人能够在这一点上原谅我这个基本问题。任何帮助表示赞赏。



为了更清楚地了解dataArray的外观:

 <$ c 
let name:String
let lastName:String
let age:Int
}

让person1:Person = Person(姓名:Steven,姓氏:miller,年龄:23​​)
让person2:Person = Person(姓名:jana,姓氏:drexler,年龄:31)
让person3: Person = Person(name:hanna,姓氏:montana,年龄:56)

var dataArray = [Person]()

dataArray.append(person1)
dataArray.append(person2)
dataArray.append(person3)

更新



尝试组合所有答案的本质,解决方案必须如下所示:
class AGE {

  static func getAge() - > Int {

var age:Int = 0

为dataArray中的项目{
而items.firstName ==Steven{
age = items.age

返回年龄

中断//这个中断永远不会因为返回而被执行。
}
break //在此处设置中断,循环将在第一轮后中断
}
返回时间
}
}

此代码适用于第二次循环,但仅适用于第一次循环。剩下的问题是,在循环达到目标后如何设置返回和中断。返回或中断将阻止另一个步骤。

解决方案

可能的程序流路径导致初始化年龄在返回之前并不详尽。在 dataArray 为空的情况下,永远不会输入循环中的正文,< c $ c> age 永远不会初始化。在你的例子中解决这个问题的最简单的方法是在声明后简单地初始化 age 0 对于意外 0 ,即使在 dataArray 的情况下也会返回
>包含名称属性值Steven的元素:对于中的每个传递... 循环中 not 包含一个元素,其中名称属性值 Steven,您将将 age 的值重置为 0 。这意味着只有当Steven个值元素是 dataArray 中的最后一个时,非 0 age 值仍然存在。我没有看到任何理由在初始化之后再次将 age 设置为 0 ,所以您可以通过例如在匹配的情况下从 for 循环执行提前退出( break )。或者,根据您想要实现的内容,只需在第一次匹配时方法 return
items.last_updated 早期的 return 语句可能会影响性能)。


I confess feeling little stupid, but after hours of trying I have to ask:

class AGE {

     static func getAge() -> Int {

        var age: Int

        for items in dataArray {
            if items.name == "Steven" {
                age = items.age
            }

            else {
                age = 0
            }
        }

        // variable "age" used before being initialized - WHY?
        return age
    }
}

I even tried to set var age: Int = 0 at the beginning, but then the function return 0. I hope someone could forgive me this basic question at this point. Any help appreciated.

To make it more clear how dataArray looks like:

struct Person {
    let name: String
    let lastName: String
    let age: Int
}

let person1: Person  = Person(name: "Steven", lastName: "miller", age: 23)
let person2: Person = Person(name: "jana", lastName: "drexler", age: 31)
let person3: Person = Person(name: "hanna", lastName: "montana", age: 56)

var dataArray = [Person]()

dataArray.append(person1)
dataArray.append(person2)
dataArray.append(person3)

Update

Trying to assemble the essence of all answers, the solution has to look like this: class AGE {

    static func getAge() ->Int {

        var age: Int = 0

        for items in dataArray {
            while items.firstName == "Steven" {
                age = items.age

                return age

                break       // This break will never be executed because of return.
            }
            break           // setting the break here, the loop will break after first round
        }
        return age
    }
}

This code works, (with the second break) but only for the first loop. The question remaining is, how to set up return and break after the loop hits its target. Either return or break will prevent the other step.

解决方案

The possible program flow paths leading to the initialization of age before its return are not exhaustive. In case dataArray is empty, the body of the for ... in loop will never be entered, and age will never initialized. The simplest approach to resolve this issue in your particular example would be simply initialize age to 0 upon it's declaration.

Regarding the "unexpected" 0 return even in case dataArray contains an element with a name property valued "Steven": for each pass in the for ... in loop that does not contain an element with a name property valued "Steven", you will reset the value of age to 0. This means that only if the "Steven" valued element is the last of the dataArray will the non-0 age value persist. I don't really see any reason to ever set age to 0 again beyond initialization, so you could resolve this by e.g. performing an early exit (break) from the for loop in case of a match. Alternatively, depending on what you want to achieve, simply return items.last_updated from the method upon first match (just note that early return statements could affect performance).

这篇关于数组搜索功能在Swift中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆