Search Your Question

Difference between mutable array and immutable array?

Ans : 

Mutable Array content can be changed but Immutable Array content can not be changed.

You can insert, update, remove item from Mutable Array, but same is not possible in Immutable Array. There is no any methods like RemoveItem or ReplaceItem for Immutable Array, which are available for Mutable Array.

So in Immutable Array, content can not be changed after initialisation. It is either be initialised by content or by nil.

Use of Immutable array 

1. Used when we want to give array to other coder who can't be change array content.
2. Used when only fixed contents are there like array of months, days which can't be changed.

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]