Property in Swift - Computed property | Property Observers (WillSet & DidSet)

   Computed property -  Computed properties are part of a property type in Swift.  Stored properties are the most common which save and return a stored value  Computed properties calculate (rather than store) a value. A computed property provides a getter and an optional setter to indirectly access other properties and values. Computed property in swift is an interesting topic as you can write code inside a property in swift. Computed properties are provided by classes, structures, and enumerations but stored properties are provided only by classes and structures. This computed property swift tutorial has some tips on how to use the computed property in the right way and avoid the common mistakes that swift programming beginners do while computed property. Example :- Computed property A Calculate Simple Intrest struct CalculateLoan {      var amount : Int      var rate : Int      var years : Int      var simpleInterest: Int {          get {              return ( amount * rate

Questions & Answers

1) - Name Of Four Important Data Type

  • NSString - Representing String
  • NSInteger -  Representing Integer
  • CGFloat -   Representing a floating point value
  • BOOL -      Representing Boolean value
2) - What are the UI Elements in ios?

      UI Elements are ios Button, textField, label, image or more
3)  reuseIdentifier in the UITableViewCell ?

     The reuseIdentifier tells UITableView which cells may be reused within the table, effectively        grouping together rows in a UITableView that differ only in content but have similar layouts. This improves scroll performance by alleviating the need to create new views while scrolling. Instead the cell is reused whenever dequeueReusableCellWithIdentifier: is called.

4) Execution States in iOS

      Not Running: The app is completely switched off, no code is being executed.
Inactive: The app is running in the foreground without receiving any events.
Active: The app is running in the foreground and receiving events.
Background: The app is executing code in the background.
Suspended: The app is in the background but is not executing any code

5) Difference between Stored Properties and Computed Properties

Stored Properties:- Stored Properties concept to store the instance of constants and variables. Store property define the "let" Keyword and Stored property of variable "var" Keyword defined

struct Number {
let price = 1000.8
var digits: Int
}
var n = Number(digits: 12345)
n.digits = 87

print("\(n.digits)")
print("\(n.price )")
n.price = 8.7 //cannot assign to property: 'price ' is a 'let' constant (error)

Lazy Stored Property:- 

class Bacic{
   lazy var a = number()    // `var` declaration is required.
}
class number {
   var name = "Swift 4"
}
var firstsample = Bacic()

print(firstsample.no.name)

Instance Variables - Computed Properties :- Stored properties also have instance variables for back up purposes to store the values declared in stored property.

"Computed Properties provide a getter and an optional setter to retrieve and set other properties and values indirectly."

class sample {
   var x = 0.0, y = 0.0
   var leth = 100.0, brdth = 50.0

   var middle: (Double, Double) {
      get {
         return (leth / 2, brdth / 2)
      }
   
      set(axis){
         x = axis.0 - (leth / 2)
         y = axis.1 - (brdth / 2)
      }
   }
}

var result = sample()
print(result.middle)
result.middle = (0.0, 10.0)

print(result.x)
print(result.y)


6):- What is Difference Between Synchronous and Asynchronous Task ?


 Synchronous:- Wait until the task had completed A Synchronous

Asynchronous:- Complete a task in background and can notify you when completed an Asynchronous

7):- NSArray Object ?
There are three part of NSArray object
  1) A Domain
  2) an error code
  3) user info Directory

The domain is a string that identifies what categories of errors this error is coming from.

8):- What is the Enum
 Enum is a type Basically that contains a group of related values in the same umbrella and enables you to work with those values in a type-safe way within your code.

9):- What is @synthesize in Objective-C ? 

Synthesize generates getter and setter methods for your property or the Variable.

10):- how to use @dynamic in Objective-C ? 

We use dynamic for subclasses of NSManagedObject. @dynamic tells the compiler that getter and setters are implemented somewhere else.

11):- What is the difference strong, weaks, read only and copy ? 

Strong, weak, assign property attributes define how memory for that property will be managed.

Strong means that the reference count will be increased and the reference to it will be maintained through the life of the object

Weak, means that we are pointing to an object but not increasing its reference count.
It’s often used when creating a parent child relationship. The parent has a strong reference to the child but the child only has a weak reference to the parent.

Read only, we can set the property initially but then it can’t be changed.

Copy, means that we’re copying the value of the object when it’s created. Also prevents its value from changing.

11):- Difference Between Concurrency, Operation Queue, Grand Central Dispatch

Concurrency:- Doing multiple things at the same time, Running multiple program in parallel or Talking advantage of number of cores available multicore CPUs  Concurrency is dividing up the execution paths of your program so that they are possibly running at the same time. The common terminology: process, thread, multithreading, and others. Terminology.

Process:- An instance of an executing app
Thread:- Path of execution for code
Multithreading:- Multiple threads or multiple paths of execution running at the same time.
Concurrency:- Execute multiple tasks at the same time in a scalable manner.
Queues;- Queues are lightweight data structures that manage objects in the order of First-in, First-out (FIFO).
Synchronous vs Asynchronous tasks

Grand Central Dispatch (GCD) - GCD is a library that provides a low-level and object-based API to run tasks concurrently while managing threads behind the scenes. Terminology.

Dispatch Queues:- A dispatch queue is responsible for executing a task in the first-in, first-out order.
Serial Dispatch Queue :- A serial dispatch queue runs tasks one at a time.
Concurrent Dispatch Queue:- A concurrent dispatch queue runs as many tasks as it can without waiting for the started tasks to finish.
Main Dispatch Queue:- A globally available serial queue that executes tasks on the application’s main thread.

12):- NSOperation  Vs NSOperationQueue Vs NSBlockOperation

NSOperation :- NSOperation adds a little extra overhead compared to GCD, but we can add dependency among various operations and re-use, cancel or suspend them.

NSOperationQueue:- It allows a pool of threads to be created and used to execute NSOperations in parallel. Operation queues aren’t part of GCD.

NSBlockOperation :- NSBlockOperation allows you to create an NSOperation from one or more closures. NSBlockOperations can have multiple blocks, that run concurrently.



Part 2 - Top 50 iOS Interview Questions and Answers Swift and Objective C

Top 50 Mostly Put Up  iOS Interview Questions and Answers

1 - introduction your self, Name, Qualifications what,done, where, experience strong week, family like etc.
2 - Basic Knowlage basic knowledge OOPS C, java, Databade

   Encapsulation
   Inheritance
   Overriding versus Overloading
   Types versus Instances
   Composition
   Polymorphism
   Access Control

3. How to call the function in objective c
    ans - [className methodName]
     if a same class then [self methods]

4. Explain dot(.) notation
   
    ans - dot(.) notation shortcut for calling getters and setters properties


5.  Difference between the atomic and Nonatomic 

   Atomic is the default behavior will ensure the present process is completed by the CPU, before           another process accesses the variable is not fast, as it ensures the process is completed entirely
 
   Non-Atomic is NOT the default behavior faster (for synthesized code, that is, for variables                   created  using @property and @synthesize) not thread-safe may result in unexpected behavior,           when two different process access the same variable at the same time.


6. What is the KVC and KVO.
     KVC (Key Value Coding ) means accessing a property or value using a string.
     id someValue  = [myObject valueForKeyPath: @"nnnn"];
    KVO (Key Value Observer ) allows you to observe change to a property or value.
   





   










      

















Comments

Post a Comment