Search Your Question

What is size class? Explain with Example.

Ans : 
Size Classes are groups of screen sizes that are applied to the width and height of the device screen. The two Size Classes that exist currently are Compact and Regular.

The Compact Size Class refers to a constrained space. It is denoted in Xcode as wC (Compact width) and hC (Compact height).

The Regular Size Class refers to a non-constrained space. It is denoted in Xcode as wR (Regular width) and hR (Regular height).

iPhone : Most iPhone devices will use the Compact width (wC) size class in each orientation with some exceptions.
The iPhones 6/7 Plus can inherit the Regular width (wR) size class resulting in the ability to use functionality normally reserved for the iPad, such as the split-view pane. Split-VIew pane is speciall made for iPad.

iPad : All iPad devices use the Regular width (wR) size class regardless of landscape or portrait orientation. In both horizontal and vertical landscape, the iPad will use a split-view pane to take advantage of the available screen space because it falls within this unconstrained size class.

Controlling the UI with Size Classes

In most cases, specifying the width class is sufficient when laying out your app; however, you can use the height class to customize your app as well. For example, if you use a split-view pane in an iPad app, users will see the same pane on iPhone Plus in landscape orientation because they both inherit the Regular width (wR) size class.

If that is not your intention, you can exclude a split-view pane from appearing on the 7 Plus by specifying both the width and height Size Classes. This works because the height of an iPhone Plus in landscape orientation is compact (hC) while all iPad devices have a regular height (hR).

You can also use Size Classes to target specific UI elements like font colors, font sizes, drop shadows, view colors and more to adapt to users’ device screen. These variations give you added control as to how your UI can adapt to different devices and orientations that your user may be using.




Difference between frames and bounds.

Ans : 

Frame :  View's location and size using the parent view's coordinate system
Needed while placing the view in the parent

bounds = View's location and size using its own coordinate system
Needed while placing the view's content or subviews within itself

The bounds of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0).

The frame of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to the superview it is contained within.

So, imagine a view that has a size of  50x50 (width x height) positioned at 15,15 (x,y) of its superview. The following code prints out this view's bounds and frame:

NSLog(@"bounds.origin.x: %f", label.bounds.origin.x);
NSLog(@"bounds.origin.y: %f", label.bounds.origin.y);
NSLog(@"bounds.size.width: %f", label.bounds.size.width);
NSLog(@"bounds.size.height: %f", label.bounds.size.height);

NSLog(@"frame.origin.x: %f", label.frame.origin.x);
NSLog(@"frame.origin.y: %f", label.frame.origin.y);
NSLog(@"frame.size.width: %f", label.frame.size.width);
NSLog(@"frame.size.height: %f", label.frame.size.height);

Output : 

bounds.origin.x: 0
bounds.origin.y: 0
bounds.size.width: 50
bounds.size.height: 50

frame.origin.x: 15
frame.origin.y: 15
frame.size.width: 50
frame.size.height: 50

So, we can see that in both cases, the width and the height of the view is the same regardless of whether we are looking at the bounds or frame. What is different is the x,y positioning of the view. In the case of the bounds, the x and y coordinates are at 0,0 as these coordinates are relative to the view itself. However, the frame x and y coordinates are relative to the position of the view within the parent view (which earlier we said was at 25,25).

For more understanding : Visit this 


Difference between Objective-C Category and Extension

Ans : 

1.Category is a way to add methods to a class whether or not source code is available implies you can add category to foundation classes like NSString and also to your own custom classes.

2.We can add extra instance variable and property in class extension but not in Category.

3.Any variable and method inside the extension is not even accessible to inherited class.

4.Category and Extension both are basically made to handle large code base but category is a way to extend class API in multiple source file while extension is a way to add required methods out side the main interface file.

5.Use category when you have to break your same class code into different source file according to different functionality and Extension when you just need to add some required methods to existing class outside the main interface file. also when you need to modify a publicly declared instance variable in a class. for ex: readonly to readwrite you can re declare it in extension.

Read : What is Category?

What is extension and How to use it?

Ans :
Swift Extension :

Add a new swift file with File > New > File... > iOS > Source > Swift File, but you can call them what you want.
The general naming convention is to call it TypeName+NewFunctionality.swift

Make extension of Double

Double+Conversions.swift

import Swift // or Foundation

extension Double {

    func celToFahren() -> Double {
        return self * 9 / 5 + 32
    }

    func fahrenToCel() -> Double {
        return (self - 32) * 5 / 9
    }
}

How to make extension:

let boilingPointCel = 100.0
let boilingPointFaren = boilingPointCel.celToFahren()
print(boilingPointFaren) // 212.0

Make extension of UIColor

UIColor+CustomColor.swift

import UIKit

extension UIColor {

    class var customGreen: UIColor {
        let darkGreen = 0x008110
        return UIColor.rgb(fromHex: darkGreen)
    }

    class func rgb(fromHex: Int) -> UIColor {

        let red =   CGFloat((fromHex & 0xFF0000) >> 16) / 0xFF
        let green = CGFloat((fromHex & 0x00FF00) >> 8) / 0xFF
        let blue =  CGFloat(fromHex & 0x0000FF) / 0xFF
        let alpha = CGFloat(1.0)

        return UIColor(red: red, green: green, blue: blue, alpha: alpha)
    }

}
See here also.

Using extension :

view.backgroundColor = UIColor.customGreen

Summary : Once you define an extension it can be used anywhere in your app just like the built in class functions. In Objective-C extensions are known as categories.

Objective C Extension : 

In objective c, when you want to make behavior of some property private you use class extension.
-> it comes with .m file only.
-> mainly for properties.
The implementation of the extension must be in the main @implementation block of the file.
Extension can only be added to the classes whose source code is available because compiler compile the source code and extension at same time.

Difference between Delegate and Datasource

Ans :

A delegate type object responds to actions that another object takes.
i.e  the UITableViewDelegate protocol has methods such as didSelectRowAtIndexPath for performing actions upon a user selecting a particular row in a table and willDisplayCell which called before delegate use cell to draw row.

DataSource type object gives data to another object.
i.e UITableViewDataSource protocol has methods such as cellForRowAtIndexPath and numberOfSectionInTaboeView dictating what should be displayed in the table.

Understanding Delegate in More Detail : 

If Object X call Object Y to perform an action. Object X should know when Object Y complete task and take action after that. 

Here we can tell that X is delegate object of Y. Y will have a reference of X. So X will implement delegate methods of Y. So Y can notify to X via delegate method.

One more point we can say that Delegate about controlling of UI and DataSource about controlling data.

Buy CHRYOSOLITE Superman Fan Art Men's Round Neck TShirt

Difference between Sqlite and CoreData

Ans  : Sqlite is database and Coredata is Object Relational Model which is layer between UI and Database. Coredata is memory efficient and Querying to Sqlite is not so efficient.
So we can't compare Sqlite and Coredata. Both are different thing.


What are persistent storage in iOS and Which one is most secure?

Ans : There are SIX types of persistent storage.

1. Userdefaut
2. Property List
3. Sqlite
4. Keychain
5. Files
6. Coredata

If brief answer they asked then follow :

1. Userdefaut : NSUserDefault class allow us to store small amount of data. It can store NSData, NSString, NSArray, NSDictionary,  NSNumber,
The maximum data can we saved depends on iOS. Currently it can store 4GB of data.
But if file is too large, then it takes too much time for retrieve and write data in file. So we can save small amount of data only. Otherwise it waste time.
We can also store our custom objects in userDefaults. We achieve this by conforming our class to NSCoding protocol. We can then convert our custom object into NSData with the help of NSKeyArchiver class. The NSData is then stored into userDefaults like other objects. Similarly we can get NSData from userDefaults and then using NSKeyUnarchiver convert the NSData back to our custom objects.

2. Property List : As userdefaut save data in plist file, so like userdefaut, Property List is not also made for save large amount of data. There is one method of NSArray and NSDictionary as writeToFile for saving data.

3. Sqlite : If your application deals with large amount of data with relationship then we should use sqlite. It's API is written in C language and embedded with our application so it is very fast. There are ORM for bringing gap between obj c app and sqlite like, FMDB,  Realm

4. Keychain : If you want to save highly sensitive and secure data like passwords and secret codes then there is a good news for you. Storing data in keychain is most secure way. To store data, I have taken library named SwiftKeyChainWrapper from cocoapods.

To Save data in Keychain : 

let saveSuccessful: Bool = KeychainWrapper.standard.set("Some String", forKey: "myKey")

To get data from Keychain :

let retrievedPassword: String? = KeychainWrapper.standard.string(forKey: "userPassword")

To remove data from keychain : 

let removeSuccessful: Bool = KeychainWrapper.standard.remove(key: "myKey")

5. Files : You can save data to any type of file. There are three type of folder like Document, Library, Tmp fo saving various type of file.

6. Core Data : Apple’s solution for persistence allows applications to persist data of any form and retrieve it. It  isn’t technically a database, although it usually stores its data in one (an SQLite DB). It’s not an object-relational mapper (ORM), though it can feel like one. It’s truly an object graph, allowing you to create, store, and retrieve objects that have attributes and relationships to other objects. Its simplicity and power allow you to persist data of any form, from basic data models to complex.

I have written for loop from 1 to 10. But when i=7 then then it comes out from loop. I have not written any specific keyword nor exception generated. How it is possible?

ANS :

for(int i=0, i<10:i++)
{
     if(i==6)
          {
              i=10;
           }
}
Here I am trying to iterate from 0 to 9. This loop is iterating till Condition i<10 matches.
Now in iterating, when loop comes i=6 I will make i=10.
So in next iteration, i<10 condition is mismatched. So it comes out from loop.
There is no any keyword used or any exception generated.

Bonus Tip : You can run swift code online here


What is StackView? What is advantage and distribution type of stackview?

Ans : Stack View allows to layout views in a stack in either horizontal or vertical fashion. In Xcode-7, stackview is introduced.

Advantage : Stacks are containers that keep views aligned automatically.

Distribution Types :

Fill(Default) : When you place your controls inside a UIStackView with Fill set as the distribution, it will keep all but one of the controls at their natural size and stretch one of them to fill the space. It determines which control to stretch by noting which one has the lowest Content Hugging Priority (CHP).

Fill Equally : With this type, each control in a UIStackView will be of equal size. All of the space between the controls will be used up, if possible. I added a spacing of eight between the UITextFields, so again you could see the size of each one. With this type, the CHP does not matter, because each control is the same size.

Fill Proportionally : The UIStackView will ensure the controls maintain the same proportion relative to one another as your layout grows and shrinks. Unlike the previous two settings, the Fill Proportionally distribution needs the controls to have an intrinsic content size. The Fill and Fill Equally distribution tell their child controls how big they should be, but this one is the other way around (as long as there is enough space for all of your controls to be their natural size). The proportions for the images and labels are maintained for the different layout sizes.

Equal Spacing : This distribution type will maintain an equal spacing between each of the controls and will not resize the controls themselves.

Equal Centring :  It will equally space the centres of the controls. Space between every control is equal.


What is inout parameter in swift?

 Function parameters are constants by default. Trying to change the value of a function parameter from within the body of that function results in a compile-time error. This means that you can’t change the value of a parameter by mistake. If you want a function to modify a parameter’s value, and you want those changes to persist after the function call has ended, define that parameter as an in-out parameter instead.

func add(x: Int, y: Int) -> Int
{
x=7              error: Can not assign to value x, x is let constant
return(x+y)
}

func add(x: inout Int, y: Int) -> Int
{
x=7           
return(x+y)
}

Bonus Tip : You can run swift code online here