Search Your Question

How can we store object in user default?

Ans : 

Actually, you will need to archive the custom object into NSData then save it to user defaults and retrieve it from user defaults and unarchive it again. You can archive it like this
let teams = [Team(id: 1, name: "team1", shortname: "t1"), Team(id: 2, name: "team2", shortname: "t2")]

var userDefaults = UserDefaults.standard
let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: teams)
userDefaults.set(encodedData, forKey: "teams")
userDefaults.synchronize()
and unarchive it like this
let decoded  = userDefaults.data(forKey: "teams")
let decodedTeams = NSKeyedUnarchiver.unarchiveObject(with: decoded) as! [Team]
But if you just did that you will get
.Team encodeWithCoder:]: unrecognized selector sent to instance
You will have to make Team conform to NSCoding just like this
class Team: NSObject, NSCoding {
    var id: Int
    var name: String
    var shortname: String


    init(id: Int, name: String, shortname: String) {
        self.id = id
        self.name = name
        self.shortname = shortname

    }

    required convenience init(coder aDecoder: NSCoder) {
        let id = aDecoder.decodeInteger(forKey: "id")
        let name = aDecoder.decodeObject(forKey: "name") as! String
        let shortname = aDecoder.decodeObject(forKey: "shortname") as! String
        self.init(id: id, name: name, shortname: shortname)
    }

    func encode(with aCoder: NSCoder) {
        aCoder.encode(id, forKey: "id")
        aCoder.encode(name, forKey: "name")
        aCoder.encode(shortname, forKey: "shortname")
    
}



Difference between network location and gps location.

Ans : 

Network location usually refers to cellular location, or wifi location. They are less accurate then GPS (Global Positioning System - satellite based) location. When the app cannot obtain GPS location (probably because the target device is inside a building), the less accurate network location is obtained and uploaded to the map page.



Which method called when I click on next, done button of keyboard?

Ans : textFieldShouldReturn


func textFieldShouldReturn(_ textField: UITextField) -> Bool {
     return false
}

What are UITextField delegate methods?

Ans : 

Copy from Apple Developer Site : Link


Managing Editing

func textFieldShouldBeginEditing(UITextField) -> Bool
Asks the delegate if editing should begin in the specified text field.
func textFieldDidBeginEditing(UITextField)
Tells the delegate that editing began in the specified text field.
func textFieldShouldEndEditing(UITextField) -> Bool
Asks the delegate if editing should stop in the specified text field. 
func textFieldDidEndEditing(UITextField, reason: UITextField.DidEndEditingReason)
Tells the delegate that editing stopped for the specified text field.
func textFieldDidEndEditing(UITextField)
Tells the delegate that editing stopped for the specified text field.
enum UITextField.DidEndEditingReason
Constants indicating the reason why editing ended in a text field.

Editing the Text Field’s Text

func textFieldShouldClear(UITextField) -> Bool
Asks the delegate if the text field’s current contents should be removed.
func textFieldShouldReturn(UITextField) -> Bool
Asks the delegate if the text field should process the pressing of the return button.

What is NSZombie?

Ans : 

It's a memory debugging aid. Specifically, when you set NSZombieEnabled then whenever an object reaches retain count 0, rather than being deallocated it morphs itself into an NSZombie instance. Whenever such a zombie receives a message, it logs a warning rather than crashing or behaving in an unpredictable way. As such, you can debug subtle over-release/autorelease problems without advanced tools or painstaking needle in haystack searches.

The name is a fairly obvious play on the fact that objects are normally considered "dead" when they reach retain count 0. With this setting, they continue to exist in a strange half-life - neither living, nor quite dead. Much like real zombies, except they eat rather fewer brains.


Enlist different background modes

Ans : 

There are different background modes provided from apple and we can see them under Signing & Capabilities > + Capability (New) > Background Mode

Switch on background mode and select which is required in our project.

Background Modes in iOS



  1. Audio, AirPlay and Picture in Picture
  2. Location updates
  3. Void over IP
  4. External accessory communication
  5. Uses Bluetooth LE accessories
  6. Acts as a Bluetooth LE accessory
  7. Background fetch
  8. Remote notifications
  9. Background processing


For tutorial, I recommended raywanderlich.com article 

Does swift supports method overloading?

Ans :  Yes. It supports method overloading.

Source : Programiz