Array search function not working in Swift

后端 未结 5 1336
Happy的楠姐
Happy的楠姐 2021-01-26 06:20

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         


        
相关标签:
5条回答
  • 2021-01-26 07:02

    I'm not Swift developer but I doubt these logics are correct. Assuming you initialise age to 0 as suggested in other answers, after you find the element that matches "Steven" into the array, you should exit. Otherwise the next iteration could set age to zero again.

    Remember, by default for statement iterates through all elements. If you want to iterate till a condition is met, then a while may be more suitable.

    I've read a tutorial and tested this code on line using swift sand box. Sharing my code on this link.

    class AGE {
        static func getAge() -> Int {
            var age = 0
            var found = false
            var current = 0
            var allElementsChecked = (dataArray.count == 0) //is false unless array is empty
            while !found && !allElementsChecked {
                found = (dataArray[current].name == "Steven")
                allElementsChecked = (current == dataArray.count-1)
                current += 1
            }
            if found
            {
                age = dataArray[current-1].age //minus 1 to undo last increment within loop 
            }
            return age
        }
    }
    

    When comes out from the loop that may be because either "Steven" was found or all elements were checked and not found.

    0 讨论(0)
  • 2021-01-26 07:07

    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).

    0 讨论(0)
  • 2021-01-26 07:13

    You need to initilize it in the beginning:

    var age: Int = 0
    

    If for some reason your for-loop don´t execute then you can´t set your age variable when you only have declared it as var age: Int and not initilized it.

    And regarding:

    I even tried to set var age: Int = 0 at the beginning, but then the function return 0

    That means that you had intilizied your age variable to 0 and the for-loop where never executed and the return will be 0.

    0 讨论(0)
  • 2021-01-26 07:16

    I tested Vadian's answer and it seems to be the correct one as per your requirements.

    Nonetheless, here are other suggestions:

    First, create another instance of your array based on your Person Struct:

    var filteredPerson = [Person]()
    

    If you want to search your dataArray for the age based on the first name:

    func getAge(of name: String){
         filteredPerson = dataArray.filter { $0.name == name }
         print (filteredPerson[0].age) }
    

    To get the age value of "Steven":

    getAge(of: "Steven")
    

    You'll get the Result: 23.

    Note that you can also just get the Int value via return:

    func getAge(of name: String) -> Int {
         filteredPerson = dataArray.filter { $0.name == name }
         return filteredPerson[0].age }
    

    On the other hand, if you have lots of Persons in your list with the same name, and you want to get the age value of a specific Person, just add the lastName parameter:

    func getAge(name: String, lastName: String){
         filteredPerson = dataArray.filter { $0.firstName == name && $0.lastName == lastName }
         print (filteredPerson[0].age) }
    

    And then get the age value via calling:

    getAge(name: "Steven", lastName: "Miller")
    

    Hope this helped someone in some way :)

    0 讨论(0)
  • 2021-01-26 07:21

    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.

    0 讨论(0)
提交回复
热议问题