I want to thank everybody for their posts. I created a function to test each row before parsing it. In my case, I have rows of CSV, each with an Int followed by a Float. This fuction will not work in all cases, but it was successful in catching the errors I was experiencing in my situation. The function uses the.ASCII table found here http://www.asciitable.com.
I think it could be improved by only allowign the characters that should appear in a valid CSV: "0123456789,-.". But this is fine for my purposes unless I can find an effective working improvement. Thanks again everybody.
func isStringOK(to st: String) -> Bool {
// Includes all letters and some symbols
// See http://www.asciitable.com ASCII Table and Description
print("ENTERING isStringOK - checking for excluded characters...")
var isStringOK = true
var numChar = 0
for ch in st {
if (ch >= ":") {
print(ch)
isStringOK = false
numChar = numChar + 1
}
if (ch < " ") {
print(ch)
isStringOK = false
numChar = numChar + 1
}
if (ch == "/") {
print(ch)
isStringOK = false
numChar = numChar + 1
}
if (ch <= "+") && (ch >= "!") {
print(ch)
isStringOK = false
numChar = numChar + 1
}
if (ch == " ") {print("\n")}
}
print("ch = Number of excluded characters: \(numChar)")
/
print(st)
numChar = st.characters.count
if numChar < 3 {isStringOK = false}
/
let commas = st.characters.filter { $0 == "," }.count
if(commas != 1) {isStringOK = false}
return isStringOK
}