2016-08-06 50 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 –