How would I achieve this setup in Firebase Realtime Database with Swift:
Database hierarchy
Currently, I am doing this by storing the larger element (with properties familyKey, geofences, and phoneNumbers) as a custom object. Also, the geofences property itself is an array of custom objects. I get an NSException doing this in the described fashion. How else would I go about doing this?
var tempGeofences = [GeofenceData]()
tempGeofences.append(GeofenceData(name: "Hello WOrld", latitude: 0, longitude: 0, radius: 1000))
let familyKey:String = String(Int.random(in: 1000...99999))
let uid:String = Auth.auth().currentUser!.uid
let phoneNumber = "1111111111"
let parent = Parent(phoneNumber: phoneNumber, familyKey: familyKey, geofences: tempGeofences)
databaseRef.child(uid).setValue(parent)
The NSException is thrown on this line:
databaseRef.child(uid).setValue(parent)
Parent class:
import Foundation
public class Parent {
var phoneNumber: String?
var familyKey: String?
var geofences: [GeofenceData]?
init() {
self.phoneNumber = ""
self.familyKey = ""
self.geofences = nil
}
init(phoneNumber: String?, familyKey: String?, geofences:[GeofenceData]) {
self.phoneNumber = phoneNumber
self.familyKey = familyKey
self.geofences = geofences
}
public func getPhoneNumber() -> String {
return phoneNumber!
}
public func getFamilyKey() -> String {
return familyKey!
}
public func getGeofences() -> [GeofenceData] {
return geofences!
}
// left off here, trying to send geofence object to firebase
public func toDictionary() -> Any {
return ["familyKey": familyKey, "geofences": geofences, "phoneNumber": phoneNumber]
}
}
And the GeofenceData class:
import Foundation
import Firebase
public class GeofenceData {
var name: String?
var latitude: Double?
var longitude: Double?
var radius: Float?
init() {
}
init(name: String?, latitude: Double, longitude: Double, radius: Float) {
self.name = name
self.latitude = latitude
self.longitude = longitude
self.radius = radius
}
// left off here, trying to send geofence object to firebase
public func toDictionary() -> Any {
return ["name": name, "latitude": latitude, "longitude": longitude, "radius": radius]
}
public func getName() -> String {
return name!
}
public func getLatitude() -> Double {
return latitude!
}
public func getLongitude() -> Double {
return longitude!
}
public func getRadius() -> Float {
return radius!
}
public func setName(name: String?) {
self.name = name
}
public func saveToFirebase(reference: DatabaseReference) {
let dict = ["name": name, "latitude": latitude, "longitude": longitude, "radius": radius] as Any
reference.child("geofences").child("0").setValue(dict)
}
}
Parent is not an object that Firebase recognizes so it throws an error.
The Firebase guide Reading & Writing Data shows the four types of objects that can be written; String, Number, Dictionary, Array.
One solution is to build a function into the class that returns the data you want to write.
public class Parent {
var phoneNumber: String?
var familyKey: String?
var geofences: [GeofenceData]?
init() {
self.phoneNumber = ""
self.familyKey = ""
self.geofences = nil
}
//other init functions
func getParentDict() -> [String: Any] {
let geoDict = ["name": name,
"latitude": latitude,
"longitude": longitude,
"radius": radius
]
let zeroNode = ["0": geoDict]
let dictForFirebase: [String: Any] = [
"phoneNumber": phoneNumber,
"familyKey": familyKey,
"geofences": zeroNode
]
return dictForFirebase
}
}
and in practice
var tempGeofences = [GeofenceData]()
tempGeofences.append(GeofenceData(name: "Hello WOrld", latitude: 0, longitude: 0, radius: 1000))
let familyKey:String = String(Int.random(in: 1000...99999))
let uid:String = Auth.auth().currentUser!.uid
let phoneNumber = "1111111111"
let parent = Parent(phoneNumber: phoneNumber, familyKey: familyKey, geofences: tempGeofences)
let parentDict = parent.getParentDict
databaseRef.child(uid).setValue(parentDict)
However, one concern is the child node with "0" as the key. That looks like you may be using an array. If there's a good reason that's fine but there are usually much better alternatives to using array's in NoSQL databases. See the legacy but still accurate Firebase post called Arrays Are Evil
EDIT:
Per a comment/question 'how to add another child node following the "0" node"
Assume we know the parent node, qSaEE..., lets add a "1" node
let parentNode = "qSaEE..."
let geofenceRef = firebaseRef.child(parentNode).child("geofences")
let geoDict = ["name": name,
"latitude": latitude,
"longitude": longitude,
"radius": radius
]
let oneNode = ["1": geoDict]
geofenceNode.setValue(oneNode)
Related
I'm new to the Firebase realtime database and relatively new to Swift in general. I am attempting to build a song request app in which users can create events for guests to request songs from the Spotify API. I'm trying to write an Event object to Firebase, which contains nested objects and arrays of different types. However, when it writes to the database, it only writes the strings and none of the arrays or objects. What is the best way to write all this information to the Firebase Database in a nested structure, so that whenever users add song requests, I can edit the array of requests for the given event in firebase.
Here is my code:
Event.swift
struct Event: Codable{
var code: String
var name: String
var host: String
var description: String
var hostUserId: String
var guestIds: [String]
var requests: [Request]
var queue: [Request]
var played: [Request]
//private var allowExplicit: Bool
//private var eventLocation
init(code: String, name: String, host: String, description: String, hostUserId: String){
self.code = code
self.name = name
self.host = host
self.description = description
self.hostUserId = hostUserId
self.guestIds = []
self.requests = []
self.queue = []
self.played = []
}
func toAnyObject() -> Any{
var guestIdsDict: [String:String] = [:]
for id in guestIds{
guestIdsDict[id] = id
}
var requestsDict: [String: Any] = [:]
for request in requests{
requestsDict[request.getId()] = request.toAnyObject()
}
var queueDict: [String: Any] = [:]
for request in queue{
queueDict[request.getId()] = request.toAnyObject()
}
var playedDict: [String: Any] = [:]
for request in played{
playedDict[request.getId()] = request.toAnyObject()
}
return [
"code": code,
"name": name,
"host": host,
"description": description,
"hostUserId": hostUserId,
"guestIds": guestIdsDict,
"requests": requestsDict,
"queue":queueDict,
"played":playedDict
]
}
}
Request.swift
struct Request: Codable{
private var name: String
private var id: String
private var explicit: Bool
private var album: Album
private var artists: [Artist]
private var likes: Int
init(name: String, id: String, explicit: Bool, album: Album, artists: [Artist]){
self.name = name
self.id = id
self.explicit = explicit
self.album = album
self.artists = artists
self.likes = 1
}
func toAnyObject() -> Any{
var artistsDict: [String:Any] = [:]
for artist in artists {
artistsDict[artist.id] = artist.toAnyObject()
}
return [
"name": name,
"id": id,
"explicit": explicit,
"album": album.toAnyObject(),
"artists": artistsDict,
"likes": likes
]
}
mutating func like(){
self.likes += 1
}
mutating func unlike(){
self.likes -= 1
if(self.likes < 0){
self.likes = 0
}
}
mutating func setLikes(count: Int){
self.likes = count
}
func getLikes() -> Int{
return self.likes
}
func getName() -> String{
return self.name
}
func getId() -> String{
return self.id
}
func getExplicit() -> Bool{
return self.explicit
}
func getAlbum() -> Album {
return self.album
}
func getImages() -> [Image] {
return self.album.images
}
func getArtists() -> [Artist] {
return self.artists
}
func getArtistString() -> String{
var artistString = ""
for (i, artist) in self.artists.enumerated(){
artistString += artist.name
if(i != self.artists.endIndex-1){
artistString += ", "
}
}
return artistString
}
}
Album.swift
struct Album: Codable{
let name: String
let images: [Image]
func toAnyObject() -> Any{
var imagesDict: [String: Any] = [:]
for image in images{
imagesDict[image.url] = image.toAnyObject()
}
return [
"name": name,
"images": imagesDict
]
}
}
Artist.swift
struct Artist: Codable{
let id: String
let name: String
func toAnyObject() -> Any{
return ["id": id, "name": name]
}
}
Image.swift
struct Image: Codable{
let height: Int
let url: String
let width: Int
func toAnyObject() -> Any{
return ["height": height, "url": url, "width": width]
}
}
As you are using Codable, you can create a dic out of it as follows:
Step 1: Add this extension to your code
extension Encodable {
var dictionary: [String: Any]? {
guard let data = try? JSONEncoder().encode(self) else { return nil }
return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)).flatMap { $0 as? [String: Any] }
}
}
Step 2: Write below code in your Struct (this you have to do in every struct or you can modify code as per your need).
func createDic() -> [String: Any]? {
guard let dic = self.dictionary else {
return nil
}
return dic
}
Now with the help of struct obj, call createDic() method and you will get a dictionary.
And you can send this dictionary to the firebase.
FULL CODE EXAMPLE:
extension Encodable {
var dictionary: [String: Any]? {
guard let data = try? JSONEncoder().encode(self) else { return nil }
}
struct LoginModel: Codable {
let email: String
let password: String
func createDic() -> [String: Any]? {
guard let dic = self.dictionary else {
return nil
}
return dic
}
}
Please comment if you have any questions.
Happy to help!
I can't find online how to store an array of objects so that the key "line_items" presents numbers for each menuItem with values for each menuItem corresponding to its own number. In other words, I need the numbers to come after line_items rather than the nested key so that each individual MenuItem object can be quickly referenced. I found online how to make it so each key has an array of values, but I need line_items to have an array of MenuItem objects. The following code crashes:
public func uploadTransactionData(_ menuItems: [MenuItem], balanceId: String, subTotal: Int, completion: #escaping (() -> ())) {
guard let userId = Auth.auth().currentUser?.uid else { completion(); return }
let utilitiesManager = UtilitiesManager()
let timestamp = utilitiesManager.timestamp()
let params: [String: Any] = ["date": "\(timestamp)",
"balance_id": "\(balanceId)",
"subtotal": "\(subTotal)",
"user_id": "\(userId)",
"line_items": menuItems
]
Firestore.firestore().document("transaction_history/\(timestamp)").setData(params)
{ err in
if let e = err {
print("$-- error creating user \(e)")
completion()
} else {
completion()
}
}
}
Here's the MenuItem model:
struct MenuItem {
let itemId: String
let name: String
var modifiers: [String]?
var photoName: String?
var photoUrl: String?
var quantity: Int
var price: Int
var sizeAddOnPrice: Int
var toppingsAddOnPrice: Int
let description: String
var size: String
let category: String
init(itemId: String, name: String, modifiers: [String]?, photoName: String?, photoUrl: String?, quantity: Int, price: Int, sizeAddOnPrice: Int, toppingsAddOnPrice: Int, description: String, size: String, category: String) {
self.itemId = itemId
self.name = name
self.modifiers = modifiers
self.photoName = photoName
self.photoUrl = photoUrl
self.quantity = quantity
self.price = price
self.sizeAddOnPrice = sizeAddOnPrice
self.toppingsAddOnPrice = toppingsAddOnPrice
self.description = description
self.size = size
self.category = category
}
Problem:
Your app is crashing because you are trying to save user defined object MenuItem to Firestore. Firestore doesn't allow it. Firestore only supports this datatypes.
Solution:
You can convert your custom object MenuItem to Firestore supported datatypes.
You can do this by making following changes to your code.
Make MenuItem confirm to Codable protocol.
struct MenuItem: Codable {
// Your code as it is.
}
Make following changes to your uploadTransactionData() function:
public func uploadTransactionData(_ menuItems: [MenuItem], balanceId: String, subTotal: Int, completion: #escaping (() -> ())) {
let userId = Auth.auth().currentUser?.uid else { completion(); return }
let utilitiesManager = UtilitiesManager()
let timestamp = utilitiesManager.timestamp()
var list_menuItem = [Any]()
for item in menuItems {
do {
let jsonData = try JSONEncoder().encode(item)
let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: [])
list_menuItem.append(jsonObject)
}
catch {
// handle error
}
}
let params: [String: Any] = ["date": "\(timestamp)",
"balance_id": "\(balanceId)",
"subtotal": "\(subTotal)",
"user_id": "\(userId)",
"line_items": list_menuItem
]
Firestore.firestore().document("transaction_history/\(timestamp)").setData(params)
{ err in
if let e = err {
print("$-- error creating user \(e)")
completion()
} else {
completion()
}
}
}
This is because Firestore doesn't know how to save the value: menuItems
you can map it like this: "objectExample": [
"a": 5,
"b": [
"nested": "foo"
]
]
or:
.setData([
"name": "Frank",
"favorites": [ "food": "Pizza", "color": "Blue", "subject": "recess" ],
"age": 12
])
I'm learning Swift and want to make an app that will show the GPS coordinates of buses on a map. The bus lat and lon come from a JSON (excerpt below):
{
"result":[
{
"Lat":52.276408,
"Lon":21.167618,
"Time":"2018-08-24 11:50:05",
"Lines":"225",
"Brigade":"4"
},
{
"Lat":52.222656,
"Lon":21.102633,
"Time":"2018-08-24 11:51:03",
"Lines":"225",
"Brigade":"2"
},
{
"Lat":52.2100185,
"Lon":21.2054211,
"Time":"2018-08-24 11:51:08",
"Lines":"119",
"Brigade":"2"
},
{
"Lat":52.1676735,
"Lon":21.2222606,
"Time":"2018-08-24 11:51:07",
"Lines":"213",
"Brigade":"3"
}
]
}
I was thinking of creating a Bus class
class Bus {
var latitude : Double = 1.11
var longitude : Double = 2.22
var lines : Int = 0
init (lat: Double, lon: Double, line: Int) {
latitude = lat
longitude = lon
lines = line
}
}
But I'm trying to figure out how to create a collection of these bus objects from the JSON, which (in full) contains around 1000 objects (amount varies throughout the day).
Could someone point me in the right direction? I don't need the fully-coded solution, just some pointers on how should I approach this.
I will most likely be using the SwiftyJSON CocoaPod for JSON parsing, together with Alamofire for getting it.
Thank you!
you can use Below Codable class as Larme said in the comments.
import Foundation
class MyModelClass: Codable {
let result: [Result]
init(result: [Result]) {
self.result = result
}
}
class Result: Codable {
let lat, lon: Double
let time, lines, brigade: String
enum CodingKeys: String, CodingKey {
case lat = "Lat"
case lon = "Lon"
case time = "Time"
case lines = "Lines"
case brigade = "Brigade"
}
init(lat: Double, lon: Double, time: String, lines: String, brigade: String) {
self.lat = lat
self.lon = lon
self.time = time
self.lines = lines
self.brigade = brigade
}
}
If you wanted to use ObjectMapper then you can use below class
import Foundation
import ObjectMapper
class MyModelClass: Mappable {
var result: [Result]?
required init?(map: Map){
}
func mapping(map: Map) {
result <- map["result"]
}
}
class Result: Mappable {
var lat: NSNumber?
var lon: NSNumber?
var time: String?
var lines: String?
var brigade: String?
required init?(map: Map){
}
func mapping(map: Map) {
lat <- map["Lat"]
lon <- map["Lon"]
time <- map["Time"]
lines <- map["Lines"]
brigade <- map["Brigade"]
}
}
write List model like this
class BusListModel {
var list = [Bus]()
var longitude : Double = 2.22
var lines : Int = 0
init (With dict:[String:Any]) {
if let result = dict["result"] as? [[String:Any]]{
for busDetail in result{
let model = Bus(lat: **valueHere**, lon: **valueHere**, line: **valueHere**)
list.append(model)
}
}
}
}
Thank you all for the answers, especially #dahiya_boy
I customised the code posted by #dahiya_boy and ended up with exactly what I need.
Below is the code. I created a jsonString for example purposes.
import Foundation
class MyModelClass: Codable {
let busArray: [Bus]
enum CodingKeys: String, CodingKey {
case busArray = "result"
}
init(busArray: [Bus]) {
self.busArray = busArray
}
}
class Bus: Codable {
let lat, lon: Double
let time, lines, brigade: String
enum CodingKeys: String, CodingKey {
case lat = "Lat"
case lon = "Lon"
case time = "Time"
case lines = "Lines"
case brigade = "Brigade"
}
init(lat: Double, lon: Double, time: String, lines: String, brigade: String) {
self.lat = lat
self.lon = lon
self.time = time
self.lines = lines
self.brigade = brigade
}
}
var jsonString = """
{
"result":[
{
"Lat":52.276408,
"Lon":21.167618,
"Time":"2018-08-24 11:50:05",
"Lines":"225",
"Brigade":"4"
},
{
"Lat":52.222656,
"Lon":21.102633,
"Time":"2018-08-24 11:51:03",
"Lines":"225",
"Brigade":"2"
},
{
"Lat":52.2100185,
"Lon":21.2054211,
"Time":"2018-08-24 11:51:08",
"Lines":"119",
"Brigade":"2"
},
{
"Lat":52.1676735,
"Lon":21.2222606,
"Time":"2018-08-24 11:51:07",
"Lines":"213",
"Brigade":"3"
}
]
}
"""
if let jsonData = jsonString.data(using: .utf8) {
let decodedJSON = try! JSONDecoder().decode(MyModelClass.self, from: jsonData)
print("Latitude: \(decodedJSON.busArray[0].lat), Longitude: \(decodedJSON.busArray[0].lon), Line: \(decodedJSON.busArray[0].lines)")
}
This prints the following console output:
Latitude: 52.276408, Longitude: 21.167618, Line: 225
I have problem adding annotations to mapView. I had success with this code:
func placeAnnotations() {
for _ in placeDetails {
let multipleAnnotations = MKPointAnnotation()
multipleAnnotations.title = place.address
multipleAnnotations.subtitle = place.phone
multipleAnnotations.coordinate = CLLocationCoordinate2D(latitude: place.lat, longitude: place.lng)
mapView.addAnnotation(multipleAnnotations)
}
}
Problem is, it is not conforming to my Place class, thus not showing custom Title, Subtitle and MKAnnotationView. This is code inside viewDidLoad(), where I'm trying to put all the annotations, but it keeps adding only last one. I understood that it overrides all the previous ones from array, but haven't found any other way/method to implement.
var placeDetails = [Place]()
var places = [Place]()
override func viewDidLoad() {
super.viewDidLoad()
downloadPlaceID {
for obj in places {
place.downloadDetails(input: obj.placeId, completed: {
self.placeDetails.append(obj)
//self.placeAnnotations()
self.mapView.addAnnotations(self.placeDetails)
})
}
}
}
And this is my class with all the data conforming to MKAnnotation
protocol, and functions, downloadPlaceID() and downloadDetails()
class Place: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var placeId: String!
var vicinity: String!
var phone: String!
var workHours: Bool!
var lat: Double!
var lng: Double!
var address: String!
var subtitle: String? {
return phone
}
var title: String? {
return address
}
var _placeId: String {
if placeId == nil {
placeId = ""
}
return placeId
}
var _vicinity: String {
if vicinity == nil {
vicinity = ""
}
return vicinity
}
var _phone: String {
if phone == nil {
phone = ""
}
return phone
}
var _workHours: Bool {
if workHours == nil {
workHours = false
}
return workHours
}
var _lat: Double {
if lat == nil {
lat = 0.0
}
return lat
}
var _lng: Double {
if lng == nil {
lng = 0.0
}
return lng
}
var _address: String {
if address == nil {
address = ""
}
return address
}
init(place: [String:Any]) {
if let ids = place["place_id"] as? String {
self.placeId = ids
}
if let vicinities = place["vicinity"] as? String {
self.vicinity = vicinities
}
self.coordinate = CLLocationCoordinate2DMake(0.0, 0.0)
}
func downloadDetails(input: String, completed: #escaping DownloadComplete) {
let details = "\(detailsBaseURL)\(detailsPlaceId)\(input)\(detailsKey)\(detailsSearchAPIKey)"
Alamofire.request(details).responseJSON { response in
let result = response.result
if let dictionary = result.value as? [String:Any] {
if let result = dictionary["result"] as? [String:Any] {
if let phoneNumber = result["formatted_phone_number"] as? String {
self.phone = phoneNumber
}
if let geometry = result["geometry"] as? [String:Any] {
if let location = geometry["location"] as? [String:Any] {
if let latitude = location["lat"] as? Double {
self.lat = latitude
}
if let longitude = location["lng"] as? Double {
self.lng = longitude
}
self.coordinate = CLLocationCoordinate2DMake(self.lat, self.lng)
}
}
if let openingHours = result["opening_hours"] as? [String:Any] {
if let openNow = openingHours["open_now"] as? Bool {
self.workHours = openNow
}
}
if let addressComponents = result["address_components"] as? [[String:Any]] {
let longName = addressComponents[1]["long_name"] as? String
let shortName = addressComponents[0]["long_name"] as? String
self.address = "\(longName!),\(shortName!)"
}
}
}
completed()
}
}
}
func downloadPlaceID (completed: #escaping DownloadComplete) {
let placeURL = URL(string: nearbyURL)
Alamofire.request(placeURL!).responseJSON { (response) in
let result = response.result
if let dictionary = result.value as? [String:Any] {
if let results = dictionary["results"] as? [[String:Any]] {
if let status = dictionary["status"] as? String {
if status == "OK" {
for obj in results {
place = Place(place: obj)
places.append(place)
}
} else {
print("jede govna")
}
}
}
}
completed()
}
There's some odd mixing of variable names and concepts here that makes your code somewhat hard to understand.
As an example, assigning a variable you call vicinities, plural, to an attribute called vicinity, singular. Or not separating your downloadDetails function from your data model.
That aside, it looks to me like you're unnecessarily adding your MKAnnotations many times to your map, by adding your array of [MKAnnotation] to your map in each loop.
I suspect you've done this because you've made it hard for yourself to know when the whole array is done updating its details.
As a quick fix, I'd suggest changing your downloadDetails function to call the completed function with the place you've just downloaded details for. Here's a really simplified, but working, version of what you are trying to do. First your Place class:
class Place: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
override init() {
coordinate = CLLocationCoordinate2DMake(0, 0)
}
func downloadDetails(completed: #escaping (Place) -> Void) {
// Instead of downloading details we are just creating random positions
self.coordinate = CLLocationCoordinate2DMake(CLLocationDegrees(arc4random_uniform(50)), CLLocationDegrees(arc4random_uniform(50)))
// Return the object you've just built
completed(self)
}
}
Now in your view controller, here I am starting with an array of 50 Place objects, getting the details for them and placing them on the map:
var places = [Place]()
for _ in 1...50 {
places.append(Place())
}
for place in places {
place.downloadDetails(completed: { (placeWithDetails) in
self.mapView.addAnnotation(placeWithDetails)
})
}
This results in the map being populated with 50 random places:
I created this class
import UIKit
import CoreLocation
private let geometryKey = "geometry"
private let locationKey = "location"
private let latitudeKey = "lat"
private let longitudeKey = "lng"
private let nameKey = "name"
private let openingHoursKey = "opening_hours"
private let openNowKey = "open_now"
private let vicinityKey = "vicinity"
private let typesKey = "types"
private let photosKey = "photos"
private let phoneNumberKey = "phoneNumber"
private let ratingKey = "rating"
private let priceLevelKey = "priceLevel"
private let websiteKey = "website"
class QPlace: NSObject {
var location: CLLocationCoordinate2D?
var name: String?
var photos: [QPhoto]?
var vicinity: String?
var isOpen: Bool?
var types: [String]?
var rating: Float?
var priceLevel: Int?
var website: String?
init(placeInfo:[String: Any]) {
// coordinates
if let g = placeInfo[geometryKey] as? [String:Any] {
if let l = g[locationKey] as? [String:Double] {
if let lat = l[latitudeKey], let lng = l[longitudeKey] {
location = CLLocationCoordinate2D.init(latitude: lat, longitude: lng)
}
}
}
// name
name = placeInfo[nameKey] as? String
// opening hours
if let oh = placeInfo[openingHoursKey] as? [String:Any] {
if let on = oh[openNowKey] as? Bool {
isOpen = on
}
}
// vicinity
vicinity = placeInfo[vicinityKey] as? String
// types
types = placeInfo[typesKey] as? [String]
// rating
rating = placeInfo[ratingKey] as? Float
//priceLevel
priceLevel = placeInfo[priceLevelKey] as? Int
website = placeInfo[websiteKey] as? String
// photos
photos = [QPhoto]()
if let ps = placeInfo[photosKey] as? [[String:Any]] {
for p in ps {
photos?.append(QPhoto.init(photoInfo: p))
}
}
}
func getDescription() -> String {
var s : [String] = []
if let types = types {
s.append("\(types.joined(separator: ", "))")
}
if let rating = rating {
s.append("Rating: \(rating)")
}
if let priceLevel = priceLevel {
s.append("PriceLevel: \(priceLevel)")
}
if let website = website {
s.append("\(website)")
}
if let isOpen = isOpen {
s.append(isOpen ? "OPEN NOW" : "CLOSED NOW")
}
if let vicinity = vicinity {
s.append("\(vicinity)")
}
return s.joined(separator: "\n")
}
func heightForComment(_ font: UIFont, width: CGFloat) -> CGFloat {
let desc = getDescription()
let rect = NSString(string: desc).boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
return ceil(rect.height)
}
}
to get the place's details that i'll show in a VC of my application by calling this function func getDescription() -> String { } but my problem and i don't know why (because i follow the documentation of google places API) is that the website and also the price level not working, so when in the viewController i call the function getDescription() it load all place's details which i added in that function, but not the website and the price level. What i did wrong? How can i adjust it?
UPDATE
https://developers.google.com/places/ios-api/place-details
static func getNearbyPlaces(by category:String, coordinates:CLLocationCoordinate2D, radius:Int, token: String?, completion: #escaping (QNearbyPlacesResponse?, Error?) -> Void) {
var params : [String : Any]
if let t = token {
params = [
"key" : AppDelegate.googlePlacesAPIKey,
"pagetoken" : t,
]
} else {
params = [
"key" : AppDelegate.googlePlacesAPIKey,
"radius" : radius,
"location" : "\(coordinates.latitude),\(coordinates.longitude)",
"type" : category.lowercased()
]
}
As per the Google Places API:
The key for price level is price_level but you have used priceLevel which might be why you are not seeing the prices
As for website I think it could be case that there is no data available for the property you have selected.