Ich muss Daten in meine tableView
laden, wenn die App startet. Ich verwende Kerndaten, indem ich eine CSV-Datei analysiere. Ich muss die .csv-Datei von Zeit zu Zeit aktualisieren. Der Benutzer muss den Aktualisierungswert anzeigen. wenn ich den folgenden Code verwendetWie CSV-Dateiwert in SWIFT zu analysieren?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let defaults = UserDefaults.standard
let isPreloaded = defaults.bool(forKey: "isPreloaded")
if !isPreloaded{
preloadData()
defaults.set(true, forKey: "isPreloaded")
}
return true
}
es nur die alten Daten von csv
Datei zeigt. Aber wenn ich den folgenden Code
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
preloadData()
return true
}
func preloadData() {
// Load the data file. For any reasons it can't be loaded, we just return
guard let contentsOfURL = Bundle.main.url(forResource: "menudata",withExtension: "csv") else {
return
}
// Remove all the menu items before preloading
removeData()
// Parse the CSV file and import the data
if let items = parseCSV(contentsOfURL: contentsOfURL, encoding:
String.Encoding.utf8) {
let context = persistentContainer.viewContext
for item in items {
let menuItem = MenuItem(context: context)
menuItem.name = item.name
menuItem.detail = item.detail
menuItem.price = Double(item.price) ?? 0.0
do {
try context.save()
} catch {
print(error)
}
}
}
}
Sein Auftritt Aktualisierungswert aber immer von csv
Datei zu laden und nicht aus der Datenbank. Wie könnte dies erreicht werden?
Ich habe 'preloadData()' Methode hinzugefügt. – Alamin