2016-08-06 17 views
2

Ich versuche, einen Anruf an meine Datenbank zu machen, wo die Stadt meines Benutzers gleich dem Standort sein wird, der dank Core Location gefunden wird. Ich kann erfolgreich einen Anruf an die Datenbank machen und den geolocation finden, ich kann auch eine Abfrage machen, wo city = "nameOfACity" aber ich finde nicht heraus, wie man Daten wo city = cityFromCoreLocation aufruft.Erhalten Wert von einem IBOutlet aus einer anderen Klasse

Ich kann die Variable, die ich für das Ergebnis der Geolocation erstellt habe, nicht aufrufen, da sie sich in einer anderen Klasse befindet und als IBOutlet angezeigt wird. Ich habe mich gefragt, ob ich kann: - finden Sie eine Möglichkeit, das IBOutlet aufrufen - oder rufen Sie direkt die Placemark.locality von Geocoder-Funktion? (was ich im Moment nicht machen kann, da es auch in einer anderen Klasse ist.

Ich bin neu bei swift, also weiß ich immer noch nicht, wie ich eine Variable aus einer anderen Klasse aufrufen soll ... Ich lese Themen zu diesem Thema, aber sie passen sich nicht auf den aktuellen Fall

Hier ist mein Code:.

Aufruf an die Basisdaten:

import UIKit 

let sharedInstance = ModelBD() 

class ModelBD: NSObject { 

var database: FMDatabase? = nil 

class func getInstance() -> ModelBD { 
    if (sharedInstance.database == nil) { 
     sharedInstance.database = FMDatabase(path: Utilities.getPath("bddFrance.sqlite")) 
    } 

    return sharedInstance 
} 


func getAllArticles() -> NSMutableArray { 
    sharedInstance.database!.open() 

    let resultSet: FMResultSet! = sharedInstance.database!.executeQuery("SELECT * FROM bddF WHERE ville = \(VC0.myCity)", withArgumentsInArray: nil) 
    let arrDataArticles : NSMutableArray = NSMutableArray() 

    if (resultSet != nil) { 
     while resultSet.next() { 
      let articlesInfo : ArticlesData = ArticlesData() 
      articlesInfo.nameArticle = resultSet.stringForColumn("nom") 
      articlesInfo.cityArticle = resultSet.stringForColumn("ville") 
      articlesInfo.districtArticle = resultSet.stringForColumn("quartier") 
      articlesInfo.countryArticle = resultSet.stringForColumn("pays") 

      print("--") 
      print("nameArticle \(articlesInfo.nameArticle)") 
      print("cityArticle \(articlesInfo.cityArticle)") 
      print("districtArticle \(articlesInfo.districtArticle)") 
      print("countryArticle \(articlesInfo.countryArticle)") 

      arrDataArticles.addObject(articlesInfo) 
     } 
    } 

    sharedInstance.database!.close() 
    return arrDataArticles 
} 
} 

Klasse, wo die Variable befindet, die ich brauche:

import UIKit 
import CoreLocation 

class VC0: UIViewController, UITableViewDelegate, UITableViewDataSource, CLLocationManagerDelegate { 

let LocationManager = CLLocationManager() 

var arrDataArticles: NSMutableArray! 

@IBOutlet weak var myCity: UINavigationItem! 

@IBOutlet weak var myTableView: UITableView! 

var images = UIImage(named: "avatar") 

override func viewDidLoad() { 
    super.viewDidLoad() 

    //CoreLocation 
    self.LocationManager.delegate = self 
    self.LocationManager.desiredAccuracy = kCLLocationAccuracyBest 
    self.LocationManager.requestWhenInUseAuthorization() 
    self.LocationManager.startUpdatingLocation() 

    //Data 
    self.getAllArticles() 
} 

//Location 
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

    CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: { 
     (placemarks, error) -> Void in 

     if error != nil { 
      print("Error") 
     } 

     if let pm = placemarks?.first { 
      self.displayLocationInfo(pm) 
     } 
     else { 
      print("Error : data error") 
     } 

    }) 
} 

func displayLocationInfo (placemark: CLPlacemark) { 

    self.LocationManager.stopUpdatingLocation() 

    print(placemark.subThoroughfare) 
    print(placemark.thoroughfare) 
    print(placemark.locality) 
    print(placemark.postalCode) 
    print(placemark.subAdministrativeArea) 
    print(placemark.administrativeArea) 
    print(placemark.country) 
    myCity.title = placemark.locality 

} 

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { 
    print("Error :" + error.localizedDescription) 
} 

//Data 
func getAllArticles() { 
    arrDataArticles = NSMutableArray() 
    arrDataArticles = ModelBD.getInstance().getAllArticles() 
    myTableView.reloadData() 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return arrDataArticles.count 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let myCell: CustomCell = tableView.dequeueReusableCellWithIdentifier("cell") as! CustomCell 

    let article: ArticlesData = arrDataArticles.objectAtIndex(indexPath.row) as! ArticlesData 

    myCell.rank.text = "\(indexPath.row + 1)" 
    myCell.photo.image = images 
    myCell.name.text = article.nameArticle 
    myCell.city.text = article.districtArticle 

    return myCell 
} 

} 

Vielen Dank im Voraus für Ihre Hilfe.

Antwort

1

Ändern Sie Ihre getAllArticles Methode in der Klasse ModelBD, um city (String) als Argument zu akzeptieren.

func getAllArticlesFromCity(city: String) -> NSMutableArray { 
    sharedInstance.database!.open() 

    let resultSet: FMResultSet! = sharedInstance.database!.executeQuery("SELECT * FROM bddF WHERE ville = '\(city)'", withArgumentsInArray: nil) 
    ... 

Dann in Ihrem VC0, können Sie die Stadt in diesem Verfahren nur passieren:

func getAllArticles() { 
    if let city = myCity.title { 
     arrDataArticles = NSMutableArray() 
     arrDataArticles = ModelBD.getInstance().getAllArticlesFromCity(city) 
     myTableView.reloadData() 
    } 
} 

Und Sie sollten self.getAllArticles nur anrufen, nachdem Sie den Standort zu erhalten. Nennen Sie es nicht auf viewDidLoad.

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    //... 
    if let pm = placemarks?.first { 
    self.displayLocationInfo(pm) 
    self.getAllArticles() 
    } 
    //... 
+0

Es zeigt einen "unerwartet gefundenen Null beim Entpacken eines optionalen Werts" Fehler, wenn ich es erstelle. – TheoF

+0

In welcher Zeile passiert der Unfall? –

+0

Ich denke, Sie sollten 'self.getAllArticles()' nicht auf 'viewDidLoad' aufrufen, weil Sie zu der Zeit noch nicht den Benutzerstandort haben. Bitte machen Sie den Anruf erst, nachdem Sie den Standort haben. (Beispiel: Aufruf nach 'self.displayLocationInfo (pm)' in 'func locationManager (Verwalter: CLLocationManager, didUpdateLocations locations: [CLLocation])'. Aber vergewissern Sie sich, dass Sie es einmal aufrufen, da der Standort ständig aktualisiert wird. –

Verwandte Themen