Search Your Question

Device token will be changed or not if we uninstall and reinstall the app ?

Ans : 

The Device token is change in the following conditions.

  • If the user restores backup data to a new device.
  • Reinstalls the App
So update to server from app delegate method didRegisterForRemoteNotificationsWithDeviceToken every time. (Just suggestion)

String Interpolation in Swift

Ans : 

String interpolation means creating string from mix of constants, variables, literals, expressions.

let length:Float = 3.14
var breadth = 10
var myString = "Area of a rectangle is length*breadth"
print(myString)
myString = "\(myString) i.e. = \(length)*\(breadth)"
print(myString)

Output :

Area of a rectangle is length*breadth
Area of a rectangle is length*breadth i.e. = 3.14*10

Another way of string interpolcation in Swift :

Use the Swift String initializer:
String(format: <#String#>, arguments: <#[CVarArgType]#>)

For example: let stringFromNumber = String(format: "%.2f", number)

Swift 5.0 has super powerful string interpolation : 

1. Normal custom String interpolation

Code for Example :

Using the new string interpolation system in Swift 5.0 we can extend String.StringInterpolation to add our own custom interpolations

extension String.StringInterpolation {
    mutating func appendInterpolation(_ number: Int) {
        let formatter = NumberFormatter()
        formatter.numberStyle = .spellOut

        if let result = formatter.string(from: number as NSNumber) {
            appendLiteral(result)
        }
    }
}

Use : print("My age is \(age).")
Output : My age is twenty nine.
Above custom interpolation tells to convert integer to word.

Amazing....hua.....


We can also write above function as following :

mutating func appendInterpolation(format number: Int) {

So, we can use like : print("Hi, I'm \(format: age)."). So there will be no confusion between default interpolation and custom interpolation.

2. String interpolation with parameters. : 

mutating func appendInterpolation(linkedin: String) {
    appendLiteral("<a href=\"https://linkedin.com/in/\(linkedin)\">@\(linkedin)</a>")}

output :
"You should follow me on linkedin : <a href="https://linkedin.com/in/twostraws">@twostraws</a>.\n"

We can also pass multiple parameters.

3. We can pass auto closure as parameter :


extension String.StringInterpolation {
    mutating func appendInterpolation(_ values: [String], empty defaultValue: String) {
        if values.count == 0 {
            appendLiteral(defaultValue())
        } else {
            appendLiteral(values.joined(separator: ", "))
        }
    }
}

let names  = [String]()

print("Crew: \(names, empty: "None").")



Using @autoclosure means that we can use simple values or call complex functions for the default value. Here defaultValue() function called for set default value.


extension String.StringInterpolation {
    mutating func appendInterpolation(if condition: @autoclosure () -> Bool, _ literal: StringLiteralType) {
        guard condition() else { return }
        appendLiteral(literal)
    }
}

let amIiOSDeveloper = true
print("Swift rocks: \(if: amIiOSDeveloper, "(*)")")
print("Swift rocks \(amIiOSDeveloper ? "(*)" : "")")


Normally we use 2nd print option at above. But due to custom string interpolation, we can directly use 1st print option.


4. String interpolation with custom type (class or struct)

struct Employee {
    var name: String
    var designation: String
}

extension String.StringInterpolation {
    mutating func appendInterpolation(_ emp: Employee) {
        appendLiteral("I am \(amp.name) and my designation is \(amp.designation).")
    }
}

let manan = Employee(name: "Manan", designation: "Senior Software Engineer")
print("\(manan)")

Print("\(manan)") gives output as :

I am Manan and my designation is Senior Software Engineer.

if we use print(manan) then it gives class debug description because here no string interpolation used.

Awesome...
Thanks Swift 5.0...

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



Difference between Cocoa and Cocoa Touch

And : 

Application Framework For
i) Cocoa is the application framework for Mac OS X.
ii) Cocoa Touch is the application framework for iPhone and iPod Touch.
Frameworks
i) Cocoa: Foundation and AppKit.
ii) Cocoa Touch: Foundation and UIKit
Absence of certain classes
Cocoa has NSHost and Cocoa Touch doesn't
API
i) Cocoa: All the classes used in Cocoa have the NS prefix Ex: NSTextField
ii) Cocoa Touch: classes used in Cocoa have the UI prefix Ex: UITextField
MVC patterns
i) Cocoa: Cocoa has multiple alternative design patterns – in addition to MVC
ii) Cocoa Touch: The iPhone SDK has a reinforced MVC system, which performs better than the default MVC in Cocoa
Other Differences
There Are also Differences In App Lifecycle, Sandboxing ,Memory Footprint

Difference between POP and OOP

Ans : 

POP - Protocol oriented programming
OOP - Object oriented programming

Swift has both power. POP has more advanced features having object oriented feature itself.



Base class of different class or views

Ans :

Base class of

UITableView : UIScrollView : UIView : UIResponder : NSObject
UIButton : UIControl : UIView : UIResponder : NSObject
NSObject : It is top most super class in swift. NSObject class confirm NSObject protocol.

Q : Is it compulsory to mention NSObject in swift inheritance?
A : No. Any class which is not inheriting any other class have NSObject as super class by default.