0

I have written this function that reads in values from a text file and stores them in a dictionary. The dictionary is formatted as: [String : Any]. Each key in the dictionary is associated with a Zipped list. The function returns the value associated for that key if the value symbol is present as a key in the dictionary. The function works correctly and is able to return a value for that associated key in the dictionary. However I am unable to iterate the returned list due to the error type 'Any' does not conform to protocol 'Sequence'. I've checked the type of the returned value from the function which is displayed as Zip2Sequence<Array<String>, Array<Any>>. Using dump to show the variable on the console also shows the the returned value is a list type. Any idea as to why I can't iterate the returned list?

The variable _element_data is defined as; var _element_data : Dictionary<String, Any>

func lookup_element_data( symbol : String, copy:Bool = true) -> Any{

        if _element_data.count == 0{

            let Keys : [String] = ["Symbol", "Name", "Z", "Mass", "r_cov", "e_affinity", "p_eig", "s_eig", "Abundance", "el_neg", "ion_pot"];

            //Gets all the data from the file element_data.txt as a 2D string array
            //_get_data_rows works correclty
            let elementDataFile = _get_data_rows(filename: "element_data");

            for items in elementDataFile{

                //Take each item in the list and:
                //filter the items out into Optional(number) and nil
                var convertedNumerals = items.map({ float_or_Nil(x: $0) });
                var tempList : [String] = Array(items[0...1]); //store each string in an array of type Any
                let tempListBack : [Float?] = Array(convertedNumerals[2...]);

                var convertedList : [Any] = tempList;
                convertedList.append(tempListBack); //Combining the two lists together

                //Zipping keys with items together
                let zipped = zip(Keys, convertedList);

                //Asign value with key element data converted to String
                //Each key is assoicated with a list of pairs of each element
                _element_data[tempList[0]] = zipped;
             }
        }

        //If the item is a key in the dictionary
        if _element_data[symbol] != nil{

            //Return the value assoicated with this specific symbol
            return _element_data[symbol]!;
        }else{
            return [];
        }

    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
edwardam
  • 45
  • 1
  • 6

0 Answers0