Monday, July 15, 2019

TableView Issue Faced


Manage height when tableview cell contain another tableview inside:-


Step1:-  Create a height constraint with any value for child tableView and connect an IBOutlet that sets the child tableView height. 
Step12:-  Set the height constraint's constant to tableView.contentSize.height.

self.tableViewHeight.constant = self.tableView.contentsize.height

Tuesday, July 9, 2019

Google Map get Api for location with Alamofire

//MARK: Api for all Location

    var allLocationdataGMS = [LocationModel]()
    var selectedArray = [String]()



func loctaion(name:String,completion:@escaping (_ result:[LocationModel]?) -> Void) {
        
        let url = "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=\(name))&components=country:us&key=\(GOOGLE_API_KEY)"
        
        Alamofire.request(URL(string: url, relativeTo: nil)!, method: .get, parameters:nil , encoding: URLEncoding.default, headers: headers).response { (resposne) in
            
            self.printRequest(resposne)
            
            do {
                let data = try self.decoder.decode(LocationData.self, from: resposne.data ?? Data())
                print("Success")
             
                    completion(data.predictions)
               
                KVNProgress.dismiss()
                
            } catch let error {
                KVNProgress.dismiss()
                self.handleError(data: resposne, error: error)
            }
        }
        
    }
    

//MARK:- api calling with dropDown 


func networkApiCall(){
        
        Network.shared.loctaion(name: self.serach_Loacation.text ?? "") { (result) in
            
            guard let user = result else{
                
                return
            }
            self.allLocationdataGMS = user
            
            self.selectedArrayself.allLocationdataGMS.map({$0.predictionDescription!})
            let dropDown = DropDown()
            
            dropDown.anchorView = self.serach_Loacation
            dropDown.bottomOffset = CGPoint(x: 0, y:(dropDown.anchorView?.plainView.bounds.height)!)
            
            // The list of items to display. Can be changed dynamically
            dropDown.dataSource = self.selectedArray
            dropDown.textColor = UIColor.black
            dropDown.backgroundColor = UIColor.red
            dropDown.selectionAction = { [unowned self] (index: Int, item: String) in
                print("Selected item: \(item) at index: \(index)")
                
            }
            dropDown.show()
    }
    }

//MARK: Api for all Location lat Long

var name = "Noida"
    func loctaionForLatLong(name:String,completion:@escaping (_ result:[LocationModel]?) -> Void) {
        
        let url = "https://maps.googleapis.com/maps/api/geocode/json?"
        
        let geocodeURLString = "\(url)address=\(name)&key=\(GOOGLE_API_KEY)"
        
        
        Alamofire.request(URL(string: geocodeURLString, relativeTo: nil)!, method: .get, parameters:nil , encoding: URLEncoding.default, headers: headers).response { (resposne) in
            
            self.printRequest(resposne)
            
            do {
                let data = try self.decoder.decode(LocationData.self, from: resposne.data ?? Data())
                print("Success")
                
                completion(data.predictions)
                
                KVNProgress.dismiss()
                
            } catch let error {
                KVNProgress.dismiss()
                self.handleError(data: resposne, error: error)
            }
        }
        

    }

//MARK:- api calling for lat long 

func locationForLatLong(){
        
        Network.shared.loctaionForLatLong(name:"Delhi") { (result) in
            
            guard let user = result else{
                
                return
            }
    }


Model:

// MARK: - Welcome
struct LocationData: Codable {
    let predictions: [LocationModel]?
    let status: String?
}

// MARK: - Prediction
struct LocationModel: Codable {
    let predictionDescription, id: String?
    let matchedSubstrings: [MatchedSubstring]?
    let placeID, reference: String?
    let structuredFormatting: StructuredFormatting?
    let terms: [Term]?
    let types: [String]?
    
    enum CodingKeys: String, CodingKey {
        case predictionDescription = "description"
        case id
        case matchedSubstrings = "matched_substrings"
        case placeID = "place_id"
        case reference
        case structuredFormatting = "structured_formatting"
        case terms, types
    }
}

// MARK: - MatchedSubstring
struct MatchedSubstring: Codable {
    let length, offset: Int?
}

// MARK: - StructuredFormatting
struct StructuredFormatting: Codable {
    let mainText: String?
    let mainTextMatchedSubstrings: [MatchedSubstring]?
    let secondaryText: String?
    
    enum CodingKeys: String, CodingKey {
        case mainText = "main_text"
        case mainTextMatchedSubstrings = "main_text_matched_substrings"
        case secondaryText = "secondary_text"
    }
}

// MARK: - Term
struct Term: Codable {
    let offset: Int?
    let value: String?
}

Tuesday, July 2, 2019

Dismiss Two ViewController

Facing Problem When You have to dismiss two view controllers at a time in Swift 3 


For Example- ViewController1-AlertViewController2-ViewController3

Now you have to dismiss ViewController3 to ViewController1 than you have to call -

self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)

Map,Filter In Swift

Higher order functions in Swift


Map the map function returns an array containing the results of applying a mapping or transform function to each item.Map is just like for loop. For Example -

"data": [
    {
      "id": 118,
      "name": "milestone 3",
     },
    {
      "id": 117,
      "name": "testAdmim",
   }

Take out all the values of key "name"-

  let filterOrgId = arrayOfName.map({ $0.name})
  result - ["milestone","testAdmim"]

Filter function loops over every item in a collection/Array, and returns a collection containing only items that satisfy an include condition

Example- 
"data": [
    {
      "id": 118,
      "permission": "yes",
     },
    {
      "id": 117,
       "permission": "no",
   }
]

  let filertedData = org_P.filter{$0.id == "your id"}
       filertedData.forEach {

            print("model data objects::",$0. permission  as? String) // if your condition is true than you will get permission value with particular id
                                     

                 }