2016-08-06 79 views
2

我試圖打電話給我的數據庫,在這裏我的用戶所在的城市將等於由於Core Location所發現的位置。我可以成功地調用數據庫並找到地理位置,我也可以查詢哪裏城市=「nameOfACity」,但我不知道如何調用城市= cityFromCoreLocation的數據。從另一個班級的IBOutlet獲取價值

我無法調用我爲地理位置結果創建的變量,因爲它位於另一個類中,並顯示爲IBOutlet。我想知道我是否可以: - 找到一種方法來調用該IBOutlet - 或直接調用Geocoder函數中的placemark.locality? (我現在不能這樣做,因爲它也是在另一個類中)

我是新來的swift,所以我仍然不知道如何從另一個類調用一個變量......我讀了主題關於這個問題,但他們並不適應當前的情況下

這裏是我的代碼:

呼叫轉移到數據庫:

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 
} 
} 

類,其中位於我需要的變量:

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 
} 

} 

在此先感謝您的幫助。

回答

1

修改您的getAllArticles方法ModelBD類接受city(String)作爲參數。

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

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

然後在你的VC0,你可以通過城市這個方法:

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

你應該只調用self.getAllArticles你的位置之後。不要撥打電話viewDidLoad

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

當我構建它時,它會顯示「意外發現的零,同時展開可選值」錯誤。 – TheoF

+0

哪條線發生撞車事故? –

+0

我想你不應該在'viewDidLoad'上調用'self.getAllArticles()',因爲那時你還沒有獲得用戶位置。請在您擁有位置後才能撥打電話。 (例如:在'func locationManager(manager:CLLocationManager,didUpdateLocations locations:[CLLocation])''中的'self.displayLocationInfo(pm)'後面調用,但由於該位置不斷更新,所以一定要調用它1 –