Search Your Question

CollectionViewDelegate and CollectionViewDataSource Methods

Ans :
Popular methods is designed by blue color.

CollectionViewDelegate Methods

Managing the Selected Cells
func collectionView(UICollectionView, shouldSelectItemAt: IndexPath) -> Bool
Asks the delegate if the specified item should be selected.

func collectionView(UICollectionView, didSelectItemAt: IndexPath)
Tells the delegate that the item at the specified index path was selected.

func collectionView(UICollectionView, shouldDeselectItemAt: IndexPath) -> Bool
Asks the delegate if the specified item should be deselected.

func collectionView(UICollectionView, didDeselectItemAt: IndexPath)
Tells the delegate that the item at the specified path was deselected.

Managing Cell Highlighting
func collectionView(UICollectionView, shouldHighlightItemAt: IndexPath) -> Bool
Asks the delegate if the item should be highlighted during tracking.

func collectionView(UICollectionView, didHighlightItemAt: IndexPath)
Tells the delegate that the item at the specified index path was highlighted.

func collectionView(UICollectionView, didUnhighlightItemAt: IndexPath)
Tells the delegate that the highlight was removed from the item at the specified index path.

Tracking the Addition and Removal of Views
func collectionView(UICollectionView, willDisplay: UICollectionViewCell, forItemAt: IndexPath)
Tells the delegate that the specified cell is about to be displayed in the collection view.

func collectionView(UICollectionView, willDisplaySupplementaryView: UICollectionReusableView, forElementKind: String, at: IndexPath)
Tells the delegate that the specified supplementary view is about to be displayed in the collection view.

func collectionView(UICollectionView, didEndDisplaying: UICollectionViewCell, forItemAt: IndexPath)
Tells the delegate that the specified cell was removed from the collection view.

func collectionView(UICollectionView, didEndDisplayingSupplementaryView: UICollectionReusableView, forElementOfKind: String, at: IndexPath)
Tells the delegate that the specified supplementary view was removed from the collection view.


Handling Layout Changes
func collectionView(UICollectionView, transitionLayoutForOldLayout: UICollectionViewLayout, newLayout: UICollectionViewLayout) -> UICollectionViewTransitionLayout
Asks for the custom transition layout to use when moving between the specified layouts.

func collectionView(UICollectionView, targetContentOffsetForProposedContentOffset: CGPoint) -> CGPoint
Gives the delegate an opportunity to customize the content offset for layout changes and animated updates.

func collectionView(UICollectionView, targetIndexPathForMoveFromItemAt: IndexPath, toProposedIndexPath: IndexPath) -> IndexPath
Asks the delegate for the index path to use when moving an item.

Managing Actions for Cells
func collectionView(UICollectionView, shouldShowMenuForItemAt: IndexPath) -> Bool
Asks the delegate if an action menu should be displayed for the specified item.

func collectionView(UICollectionView, canPerformAction: Selector, forItemAt: IndexPath, withSender: Any?) -> Bool
Asks the delegate if it can perform the specified action on an item in the collection view.

func collectionView(UICollectionView, performAction: Selector, forItemAt: IndexPath, withSender: Any?)
Tells the delegate to perform the specified action on an item in the collection view.

Managing Focus in a Collection View
func collectionView(UICollectionView, canFocusItemAt: IndexPath) -> Bool
Asks the delegate whether the item at the specified index path can be focused.

func indexPathForPreferredFocusedView(in: UICollectionView) -> IndexPath?
Asks the delegate for the index path of the cell that should be focused.

func collectionView(UICollectionView, shouldUpdateFocusIn: UICollectionViewFocusUpdateContext) -> Bool
Asks the delegate whether a change in focus should occur.

func collectionView(UICollectionView, didUpdateFocusIn: UICollectionViewFocusUpdateContext, with: UIFocusAnimationCoordinator)
Tells the delegate that a focus update occurred.

Controlling the Spring-Loading Behavior
func collectionView(UICollectionView, shouldSpringLoadItemAt: IndexPath, with: UISpringLoadedInteractionContext) -> Bool
Returns a Boolean value indicating whether you want the spring-loading interaction effect displayed for the specified item.


UICollectionViewDataSource Methods


Getting Item and Section Metrics
func collectionView(UICollectionView, numberOfItemsInSection: Int) -> Int  (Required)
Asks your data source object for the number of items in the specified section.

func numberOfSections(in: UICollectionView) -> Int
Asks your data source object for the number of sections in the collection view.

Getting Views for Items
func collectionView(UICollectionView, cellForItemAt: IndexPath) -> UICollectionViewCell  (Required)
Asks your data source object for the cell that corresponds to the specified item in the collection view.

func collectionView(UICollectionView, viewForSupplementaryElementOfKind: String, at: IndexPath) -> UICollectionReusableView
Asks your data source object to provide a supplementary view to display in the collection view.

Reordering Items
func collectionView(UICollectionView, canMoveItemAt: IndexPath) -> Bool
Asks your data source object whether the specified item can be moved to another location in the collection view.

func collectionView(UICollectionView, moveItemAt: IndexPath, to: IndexPath)
Tells your data source object to move the specified item to its new location.

Configuring an Index
func indexTitles(for: UICollectionView) -> [String]?
Asks the data source to return the titles for the index items to display for the collection view.

func collectionView(UICollectionView, indexPathForIndexTitle: String, at: Int) -> IndexPath
Asks the data source to return the index path of a collection view item that corresponds to one of your index entries.

Write program for ascending sorting Int Array in any language

Ans : 
(Interviewer told syntax doesn't matter, write pseudo-code type program)

1. Not using direct function.

 void main()
    {

        int i, j, a, n, number[30];
        printf("Enter the value of N \n");
        scanf("%d", &n);

        printf("Enter the numbers \n");
        for (i = 0; i < n; ++i)
            scanf("%d", &number[i]);

        for (i = 0; i < n; ++i)
        {

            for (j = i + 1; j < n; ++j)
            {

                if (number[i] > number[j])
                {

                    a =  number[i];
                    number[i] = number[j];
                    number[j] = a;

                }

            }

        }

        printf("The numbers arranged in ascending order are given below \n");
        for (i = 0; i < n; ++i)
            printf("%d\n", number[i]);

    }

2. If we write in swift using higher order function, then

var numbers = [45,6,113,56,8,56,43,78]

print(numbers.sort()) //Sorting in Ascending order

print(numbers.sort(>)) //Sorting in Descending order

Interviewer mostly asked this question to check your programming logic. So he/she don't require syntax.

Q. Sorting strings array
A.
Method 1 : 
var sortedArray = swiftArray.sorted { $0.localizedCaseInsensitiveCompare($1) == ComparisonResult.orderedAscending }

Method 2 : 
let sortedNames = names.sort { $0.name < $1.name }
let sortedNames = names.sorted(by: <)


Difference between Swift Array and Objective C Array

Ans : Swift arrays (Array and for short, []) are passed by value which means
that every object contained will be copied.

NSArray are implemented as classes (and bridged from ObjC) so they're passed as references.

In Swift, we can declare three type of array.
One is Array or [],
Second is NSArray,
Third is NSMutableArray.

NSArray and NSMutableArray are coming from Objective C by bridge. So interviewer may ask what are difference between types arrays in swift?

Extract value from Array : 

Get 3rd Value from Array

Objective C : [arr objectAtIndex:2] // 2 due to index start from 0 in array

Swift : arr[2] 

Equilibrium Problem - Left and right side and top and bottom side sum of any point are same in matrix


Problem Description :

#import <Foundation/Foundation.h>


int leftArraySum(NSMutableArray *A,int n)
{
    NSInteger sum  = 0;
    for(int i=0;i<n;i++)
    {
        sum += [[A objectAtIndex:i] integerValue];   
    }
    return sum;
}

int reightArraySum(NSMutableArray *A,int n)
{
     NSInteger sum  = 0;
    for(int i=n+1;i<[A count];i++)
    {
        sum += [[A objectAtIndex:i] integerValue];   
    }
    return sum;
}

int topArraySum(NSMutableArray *A,int n)
{
     NSInteger sum  = 0;
    for(int i=0;i<n;i++)
    {
        sum += [[A objectAtIndex:i] integerValue];   
    }
    return sum;
}

int bottomArraySum(NSMutableArray *A,int n)
{
     NSInteger sum  = 0;
    for(int i=n+1;i<[A count];i++)
    {
        sum += [[A objectAtIndex:i] integerValue];   
    }
    return sum;
}

int solution(NSMutableArray *A) {
    // write your code in Objective-C 2.0
 
    NSMutableArray *rowArr = [A objectAtIndex:0];
    int colCount = [rowArr count];
    int rowCount = [A count];
 
    NSMutableArray *arrRowSum = [[NSMutableArray alloc] initWithCapacity: rowCount];
    NSMutableArray *arrColSum = [[NSMutableArray alloc] initWithCapacity: colCount];
 
    for(int i=0;i<rowCount;i++)
    {
        NSArray *colArr = [A objectAtIndex:i];
        NSNumber * sum = [colArr valueForKeyPath:@"@sum.self"];
        [arrRowSum insertObject:sum atIndex:i];
    }
 
    for(int i=0;i<colCount;i++)
    {
     
        NSInteger sum = 0;
        for(int j=0;j<rowCount;j++)
        {
           sum +=  [[[A objectAtIndex:j] objectAtIndex:i] integerValue];
        }
        NSNumber *sumVal = [NSNumber numberWithInt:sum];
        [arrColSum insertObject:sumVal atIndex:i];
    }
 
    int result = 0;
    for (int i=1;i<rowCount;i++)
    {
        int left = leftArraySum(arrRowSum,i);
        int right = reightArraySum(A,i);
        if(left  == right)
        {
            for(int j=0;j<colCount;j++)
            {
                int top = topArraySum(A,j);
                int bottom = bottomArraySum(A,j);
                   if(top == bottom )
                {
                    result = i+j;
                    break;
                }
            }
        }
    }
 
    return result;
}


What types of feature google map provide to developers?

Ans : 

Google map features :
  • Plotting places Near Me. Like near by hospitals, restaurants, temples
  • Direction ( route ), Distance, Travel time, Live traffic
  • Street view for 3D location
  • Geofencing
  • Live tracking location
  • Get current location
  • Put markers on various places
Alternatives of Google map api :
I am using MapMyIndia api due to high cost of Google map apis. I have also implemented here map. Both are nice.

There are two versions of here-map, free and premium. Premium here map service is better than free here map service. I have implemented live tracking of car, replay route, using both apis.

What are collection types in objective C and Swift?

Ans : 

In  iOS, there are 3 collection types

i. Array 
ii. Dictionary 
iii. Set.

There are two types of collection types. Mutable and Immutable. Mutable can be changed but immutable can not be changed once initialised.

Q. How to check nil keys in dictionary?
A. There is not any nil keys in dictionary.

Determine index of one integer in another integer - online programming question

Problem Description : Determine index of one integer in another integer

i.e If input A = 23,B = 1472367 then Output : 3 . Because '23' is 3rd index in '1472367'.

Asked in Company : Futurescape Technologies Pvt Ltd  via Codility.com

Ans : 

#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>

// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");

int solution(int A, int B) {
    // write your code in Objective-C 2.0
    NSString *strA = [NSString stringWithFormat:@"%d",A];
    NSString *strB = [NSString stringWithFormat:@"%d",B];
 
   NSRange range =   [strB rangeOfString:strA];
   if(range.location == NSNotFound)
   return -1;
   else
   return range.location;
}


Do you like to use storyboard? Why? Can more than 1 storyboard allowed?

Ans : I like to use storyboard.

I am using storyboard due to advantage over XIB : See here

Yes, we can use more than 1 storyboard. We can make different storyboard for different features in same project.

Navigating from one storyboard to another storyboard :

1. Using segue :

  • Drag & Drop storyboard reference
  • Give name and assign viewcontroller (to which we want to navigate)
  • Give segue from viewcontroller to storyboard reference.
2. Using coding :


var anotherStoryboard = UIStoryboard(name: "another", bundle: nil)
var vc: UIViewController = anotherStoryboard.instantiateViewController(withIdentifier: "loginView")
present(vc, animated: false)




What is multiplier and ratio in auto layout in ios?

Ans :


Difference between Multiplier and Ratio : 

There is not any difference. Both are using for same purpose.

The Aspect Ratio is really just a convenient way to express the Multiplier when working in Interface Builder. It effectively gets "converted" to a multiplier.

You can confirm this while debugging by inspecting the constraint's .multiplier property. If you set a view's width to 60, and multiplier to 1:2 (resulting in an auto-layout height of 120), the actual value of .multiplier will be 0.5.

So, in my view, it depends on what feels more natural.

If I want a view to be 90% of the width of another view, I am much more likely to set the Multiplier to 0.9 --- which gives the exact same result as setting it to 9:10.

However, if I want a view to maintain an aspect ration of, say, 3-to-2, I am much more likely to set the Multiplier to 3:2 rather than 1.5.

Using a ratio can also be convenient when you have "non-simple" values. That is, it's easy to understand that a ratio of 3:2 is the same as 1.5. But what if I have an image with native size of 281 x 60, and I want to use those values to maintain ratio? 281:60 is easier to understand than .multiplier = 4.68333339691162.

And, while they are interchangeable, it is probably a bit more intuitive to use Ratio when constraining an object to itself - e.g. I want my view's width to be 2 x its own height, so 2:1 - and using Multiplier when constraining one object to another - e.g. I want my view's width to be 75% of the width of its superview, so 0.75.

Difference between nil, NIL and null

Ans : 

The difference is that while
NULL represents zero for any pointer,
nil is specific to objects (e.g., id) and
Nil is specific to class pointers.


Nil,Null and nil are used with below
1> Nil for Objective c Class
2> nil for Objective c object
3> Null for C pointer
Example:
1>Class A=Nil;
2>NSString strName=nil;
3>char *pointerChar = NULL;