2

I've been searching all over the internet for hours and hours and I've tried so many solutions but I'm never succeeding with this problem. So, basically, I'm a java-student developer and trying to create an app for both android and iOS. In Android, everything just works perfectly, if you ever find a problem, EASY PEASY! No problem to find solutions. But for iOS? Well.. I bet you know the answer! It's so damn hard to get things together and work properly.

Tried multiple libraries from git, all of them gives me an error since there either only can handle integers or there's problem with something else. None of these guides however seems to work well with my brain.

What I wonder is if somebody could give me a code-snippet or somehow a guide on how to just read an xls/xlsx/csv file and save the values into an array. I have two columns and would like to save them into two different arrays. The Excel file contains thousands of coordinates, so one array for latitude and one array for longitude.

Hope somebody have a tip or sort of solution for this!

Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Logi95
  • 169
  • 1
  • 9

1 Answers1

0

I would go in excel and convert your xslx file into a csv file. Then I’d try this library: https://github.com/yaslab/CSV.swift/blob/master/README.md

From the documentation on this library, it handles strings.

On that github page there are multiple ways to generate this array (either the csv as a Swift string or a file). This is the example for a file from that GitHub repo:

import CSV

let csvString = "1,foo\n2,bar"
let csv = try! CSVReader(string: csvString)
while let row = csv.next() {
    print("\(row)")
}
// => ["1", "foo"]
// => ["2", "bar"]

A 2-d array would be natural for this (if you’re still set on two separate arrays, see the edit at the end) Check out this answer for how it would work (code from top answer below): Swift append to 2d array

private var cards = [[Int]]()

init() {
     //Fill cards array by adding all cards
     for i in 0...12{
         for x in 0...3{
            cards.append([i+2,x])
        }
     }
 }

So basically your code you’d need to mix the first and second code snippets in this answer. Declare the array(s), then use the csv library as in one of the examples on the README page, then use Swift’s append function to add the latitude and longitude coordinates (you’d do this on the line replacing print("(row)") in the first example).

I have not personally tried this GitHub repo but based on the issues that have been raised and stars my guess is it should work.

Best of luck!

Edit: just realized you wanted two different arrays. In that case you’ll just declare two string arrays and instead of the print("(row)") from the first code snippet you’ll append latitude to one array and longitude to another array as seen in the second code snippet.

Alexander C
  • 676
  • 1
  • 5
  • 12
  • Thanks! This one worked great. But there's a problem with the reading of the csv file I believe. Since there are coordinates, they're separated with a comma i.e. "56,24231" "12,213123", and it seem as it's wrong in splitting the files. Output example ["11", "2538462;58", "8591238]. Any idea on how to solve this? Never mind, I changed all the commas for the coordinates to dots instead, now it works perfectly! – Logi95 May 31 '19 at 14:31
  • Although I marked this as the right answer, I still need some more help! Do you have any suggestion on how to convert the row result to a string in order to store it in a string array? I get lots of errors and I'm pretty stuck! I'm trying to declare var lat = [String]() and then lat.append(row as String!) but it dont work? – Logi95 May 31 '19 at 17:11
  • What does the string look like if you do print("\(row)")? What error message are you seeing? If you copy paste your code I'll try to run it in xcode and see what comes up. Would be easiest for me if your csv is a string instead of a file – Alexander C May 31 '19 at 17:13
  • "\\(row)" should already be a string. I don't know what it looks like cause it depends on your csv file. but based on that string you should be able to do some trimming using the built-in swift equivalent of substring methods and other string manipulation methods to get it into the format you want. – Alexander C May 31 '19 at 17:18
  • print("\\(row)") – Alexander C May 31 '19 at 17:20
  • ["18.2240378;59.3207655"] – Logi95 May 31 '19 at 17:22
  • That's an array isn't it? So, I don't understand how to grab the 1st part of it and 2nd? lat=row[1], lng=row[0] dont work.. EDIT: ofc I mean lat.append(row[0]) – Logi95 May 31 '19 at 17:22
  • This is a workaround (perhaps not the most elegant way to do things but would work): If swift is able to print() it it's either a string already or it's an array and it automatically turns into a string. If it's not already a string, convert the array into a string (for example): let stringArray = ["Bob", "Dan", "Bryan"] let string = stringArray.joined(separator: " ") print(string) // prints: "Bob Dan Bryan" – Alexander C May 31 '19 at 17:26
  • So then you just grab the first word or the second word depending if you want latitude or longitude – Alexander C May 31 '19 at 17:27
  • https://stackoverflow.com/questions/34333881/get-the-first-word-in-a-string-of-words-spaces-substring-first-word-before-s – Alexander C May 31 '19 at 17:27
  • Ok so I got it to work now! var lat = [[String]]() did it, but I don't want to store both values in the lat array. So I would have to split the value where the ; is located. I believe I have to save the row result as a string, then split it into the arrays afterwards. So the problem is how can I save the row result in a string? – Logi95 May 31 '19 at 17:27
  • see previous comments, i think it answers the question – Alexander C May 31 '19 at 17:28
  • Ok great, I give it a try and let you know if it works or not. Thanks bro! – Logi95 May 31 '19 at 17:29
  • So, it didn't work! "Cannot convert value of type 'ArraySlice?' to type 'String?' in coercion" – Logi95 May 31 '19 at 17:40
  • I think it would save us both time and make both happy if you just add a new answer, where you put a code snippet with some comments that can guide me correct or what do you think? – Logi95 May 31 '19 at 17:41
  • https://marcosantadev.com/arrayslice-in-swift/#convert_slice_array, rather not hold your hand and spend more time on an answer that seems sufficient. This is fairly basic stuff. I fix this bug for you then something else comes up then you want me to fix the next bug. This is a free site, I'm not getting paid. The answer got you further than you were before, these comments have been helpful. – Alexander C May 31 '19 at 18:05
  • What you're asking really needs it own question, it's related to what you originally asked but it's a bug that you should be able to work out on your own. I'm not going to code your entire app for you in the comments section today or constantly revise my answers for you when you previously indicated that you would mark the answer correct – Alexander C May 31 '19 at 18:12