I confess feeling little stupid, but after hours of trying I have to ask:
class AGE {
static func getAge() -> Int {
var age: Int
for i
As mentioned in the other answers the issue is that the variable will never be set if the array is empty.
Apart from that your code is inefficient and returns only the expected result if Steven
is the last item in the array. Otherwise the variable age
gets overwritten with 0.
Nevertheless in Swift there are more convenient methods to filter an item in an array than a repeat loop, for example first(where:
class AGE {
static func getAge() -> Int {
return dataArray.first(where: { $0.name == "Steven" })?.age ?? 0
}
}
or it might be preferable to return nil
if Steven
is not in the array
class AGE {
static func getAge() -> Int? {
return dataArray.first(where: { $0.Name == "Steven" })?.age
}
}
PS: last_updated
in your code is not related to the struct Person
at all.