Ios 编码UTF8返回零Swift3
我正在尝试使用Firebase下载一个csv文件,文件中的字符类似于«aä–a»。 如您所见,我使用的是Swift3编码功能: 字符串(字节:data!,编码:.utf8) 在我的代码中:Ios 编码UTF8返回零Swift3,ios,swift,firebase,encoding,utf-8,Ios,Swift,Firebase,Encoding,Utf 8,我正在尝试使用Firebase下载一个csv文件,文件中的字符类似于«aä–a»。 如您所见,我使用的是Swift3编码功能: 字符串(字节:data!,编码:.utf8) 在我的代码中: func downloadMyCSVWithDLURL(csvName: String, modeType: Bool, modeSync: Bool){ let realm = try! Realm() //Firebase Storage Reference
func downloadMyCSVWithDLURL(csvName: String, modeType: Bool, modeSync: Bool){
let realm = try! Realm()
//Firebase Storage Reference
let storage = FIRStorage.storage()
//Firebase Path to my csv file
if (FIRAuth.auth()?.currentUser?.uid) != nil {
let userID = FIRAuth.auth()?.currentUser?.uid
let downloadRef = storage.reference(forURL: "\(REF_USERS_CSV_FOLDER)").child("USERS_BDD_CSV/\(userID!)/\(csvName).csv")
//Download csv file in "data" as NSData
downloadRef.data(withMaxSize: 1000000, completion: { (data, err) in
//Encoding
let rawString = String(bytes: data!, encoding: .utf8)
let rawLines = rawString?.components(separatedBy: ["\n"])
let count = rawLines?.count
var tableParticipant = [Participant]()
for i in 1...count! {
let datas = rawLines![i].components(separatedBy: ";")
[...]
}
id;lastname;firstname;phoneNBR;mail;qrCode;address;cp;city;company;plusOne;entryType;eventName;isCheck;liveInscr;dateInscr;dateCheck
424ad79a;DOE;Johnàéèäâ;22222222222;doe@john.com;QRcode;14 George Street;2000;Sydney;COMPANY;;VIP;XXX;false;false;null;null
- 数据和数据!=1586ko字节,是我的csv的正确大小
func downloadMyCSVWithDLURL(csvName: String, modeType: Bool, modeSync: Bool){
let realm = try! Realm()
//Firebase Storage Reference
let storage = FIRStorage.storage()
//Firebase Path to my csv file
if (FIRAuth.auth()?.currentUser?.uid) != nil {
let userID = FIRAuth.auth()?.currentUser?.uid
let downloadRef = storage.reference(forURL: "\(REF_USERS_CSV_FOLDER)").child("USERS_BDD_CSV/\(userID!)/\(csvName).csv")
//Download csv file in "data" as NSData
downloadRef.data(withMaxSize: 1000000, completion: { (data, err) in
//Encoding
let rawString = String(bytes: data!, encoding: .utf8)
let rawLines = rawString?.components(separatedBy: ["\n"])
let count = rawLines?.count
var tableParticipant = [Participant]()
for i in 1...count! {
let datas = rawLines![i].components(separatedBy: ";")
[...]
}
id;lastname;firstname;phoneNBR;mail;qrCode;address;cp;city;company;plusOne;entryType;eventName;isCheck;liveInscr;dateInscr;dateCheck
424ad79a;DOE;Johnàéèäâ;22222222222;doe@john.com;QRcode;14 George Street;2000;Sydney;COMPANY;;VIP;XXX;false;false;null;null
错误:
func downloadMyCSVWithDLURL(csvName: String, modeType: Bool, modeSync: Bool){
let realm = try! Realm()
//Firebase Storage Reference
let storage = FIRStorage.storage()
//Firebase Path to my csv file
if (FIRAuth.auth()?.currentUser?.uid) != nil {
let userID = FIRAuth.auth()?.currentUser?.uid
let downloadRef = storage.reference(forURL: "\(REF_USERS_CSV_FOLDER)").child("USERS_BDD_CSV/\(userID!)/\(csvName).csv")
//Download csv file in "data" as NSData
downloadRef.data(withMaxSize: 1000000, completion: { (data, err) in
//Encoding
let rawString = String(bytes: data!, encoding: .utf8)
let rawLines = rawString?.components(separatedBy: ["\n"])
let count = rawLines?.count
var tableParticipant = [Participant]()
for i in 1...count! {
let datas = rawLines![i].components(separatedBy: ";")
[...]
}
id;lastname;firstname;phoneNBR;mail;qrCode;address;cp;city;company;plusOne;entryType;eventName;isCheck;liveInscr;dateInscr;dateCheck
424ad79a;DOE;Johnàéèäâ;22222222222;doe@john.com;QRcode;14 George Street;2000;Sydney;COMPANY;;VIP;XXX;false;false;null;null
致命错误:在展开可选值时意外发现nil
(lldb)
- 设rawString=nil
- 设rawLines=nil
- 让计数=零
有人有什么建议吗?你的CSV不是UTF8编码谢谢,你说得对,在将我的CSV文件重新编码为UTF8后,应用程序不会崩溃。但是出现的是特殊字符而不是正确的字符。因此,在读取
字符串(字节:数据,编码:。无论什么)
您的CSV不是UTF8编码谢谢,您是对的,将我的CSV文件重新编码为UTF8后,应用程序不再崩溃。但是会出现特殊字符而不是正确的字符。因此,在读取字符串(字节:data,encoding:.whatever)时,只需使用原始编码方法