Search Your Question

How you show current location blue dot on map?

Ans :

self.mapView.myLocationEnabled = YES to hide the blue dot.
set a class to implement CLLocationManagerDelegate to track user's location.

https://stackoverflow.com/a/40058423

What is subscript?

Ans : 

Subscripts are used to access information from a collection, sequence and a list in Classes, Structures and Enumerations without using a method.

These subscripts are used to store and retrieve the values with the help of index without the use of separate method.

To access elements via subscripts write one or more values between square brackets after the instance name.

For example: Array elements are accessed with the help of someArray[index] and its subsequent member elements in a Dictionary instance can be accessed as someDicitonary[key].

Code :

In coding, subscript is same as computed property.



 class monthsInYear {
 private var months = [“Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, "Aug",  "Sep" , "Oct", "Nov" , "Dec"
 subscript(index: Int) -> String {

 get {
       return months[index] // getter is mandatory
  }
 set(newValue) {
       self.months[index] = newValue // setter is optional
     }
   }
 }
 var p = monthsInYear()
 print(p[0]) // prints Jan
 p[0] = “Jan”
 print(p[0]) // prints Jan



If you have any comment, question, or recommendation, feel free to post them in the comment section below!

What is Object mapping that helping Jon parsing?

Ans. : Object mapping is convert to JSON to SomeOtherObject, BussinessModel to PersistenceModel, etc or map fields of JSON to SomeOtherObject fields.

There are many third party library available for same purpose.

1. We use mapping using init method without third party library :

struct User {
  let id: Int
  let name: String
  
  init?(dictionary: [String: Any]) {
    guard let id = dictionary["id"] as? Int else { return nil }
    guard let name = dictionary["user_name"] as? String else { return nil }
    
    self.id = id
    self.name = name
  }
}

do {
  if let userDictionary = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any] {
    let user = User(dictionary: userDictionary)
    //Do something with user
  }
} catch {
  print(error)

}

3. Using codable protocol  (Without third party)
4. SwiftyJson 

In today's world mostly people using 3rd option for  object mapping.

Different task available in NSURLSession

Ans : 

NSURLSession   :       The NSURLSession class and related classes provide an API for downloading data from and uploading data to endpoints indicated by URLs. Your app can also use this API to perform background downloads when your app isn’t running or, in iOS, while your app is suspended.
It is replacement of NSURLConnection from iOS 7.

Types of URL Sessions :


  1. default sessions: behaviour like NSURLConnection 
  2. ephemeral sessions: not cache any content to disk 
  3. download sessions: store the result in file and transferring data even when app is suspended, exits or crashes
Based on above sessions, developers can schedule three types of tasks: 

  1. data tasks: retrieve data to memory 
  2. download tasks: download file to disk 
  3. upload tasks: uploading file from disk and receiving response as data in memory 


func httpGet(request: NSURLRequest!, callback: (String, String?) -> Void) {
            var session = NSURLSession.sharedSession()
            var task = session.dataTaskWithRequest(request){
                 (data, response, error) -> Void in
                 if error != nil {
                     callback(“”, error.localizedDescription)
                 } else {
                     var result = NSString(data: data, encoding:
                                           NSASCIIStringEncoding)!
                     callback(result, nil)
                 }
            }
            task.resume()
        }