2017-01-11 11 views
0

master-detail appの詳細ビューに2つのラベルを表示しようとしています。最初のラベルには「レストラン名」を、2番目のラベルには「現在の場所」を表示します。私は正常に `locationManager 'を実装し、私は正しい現在のユーザーの座標を自分のコンソールに取得します。しかし、私はラベルのテキストに座標を表示したいと思います。私は、次のコードがなぜ座標が受け取られる前に2番目のラベルが設定されているのかという問題を仮定します。それを実装するための最良の方法は何でしょうか?私はRestaurantクラスにちょうどnamecurrentLocationLabelUILabelCurrentLocationRestaurantNameプロパティの名前を変更することを提案、まずUILabel.textを座標で更新

var restaurant: Restaurant? { 
    didSet { 
     self.configureView() 
    } 
} 

var currentLocation: Coordinate? { 
    didSet { 
     self.configureView() 
    } 
} 

答えて

0

func configureView() { 

//to display label with restaurant name 
    if let restaurant = self.restaurant { 
     if let label = self.restaurantNameLabel { 
      label.text = restaurant.RestaurantName 
     } 
    } 

//to display label with user's Latitude and Longitude: 

    if let label = self.CurrentLocation { 
     label.text = "\(currentLocation?.longitude)" 
    } 
} 

は、私はまた、didSetのメソッドを持っています。第2に、それぞれのdidSetブロックにプロパティの対応するラベルを設定する方が簡単です。また、私はflatMapを使用しています。currentLocationの場合、nilならnilを返します。そうでなければ、現在の位置が操作され、クロージャによって返されます。

var restaurant: Restaurant? { 
    didSet { 
     restaurantNameLabel?.text = restaurant.flatMap { $0.name } 
    } 
} 

var currentLocation: Coordinate? { 
    didSet { 
     currentLocationLabel?.text = currentLocation.flatMap { "\($0.latitude), \($0.longitude)" } 
    } 
} 
関連する問題