Category: iOS

Implement Merge Intervals in Swift

Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input. Example 1: Input: intervals = [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6]. Example 2: Input: intervals = [[1,4],[4,5]] Output: [[1,5]] Explanation: Intervals [1,4]

Continue Reading →

Accessing non static constant in static function of swift class

I came across this question on stackoverflow. https://stackoverflow.com/questions/32346456/accessing-non-static-constant-in-static-function-of-swift-class Basically it asked, how we can access instance members from static functions. The answer would be no, we can’t access instance variables from static method. But this is possible using a singleton design pattern. The actual code is shown below This is how I can alter it.

Continue Reading →

Keychain wrapper in objective-c

Computer users often have small secrets that they need to store securely. For example, most people manage numerous online accounts. Remembering a complex, unique password for each is impossible, but writing them down is both insecure and tedious. Users typically respond to this situation by recycling simple passwords across many accounts, which is also insecure.

Continue Reading →

Find the largest number by swapping adjacent digits if they have the same parity in Swift

Problem description: You are given a number. You can swap two adjacent digits, if they are both odd or even. For e.g. given (3,8), you can’t swap them. But you can swap (5,9) since they are both odd. They have the same parity. A similar problem is explained here https://www.youtube.com/watch?v=qEIGhVtZ-sg Function description: swapDigitsAndGetLargestNumber(number: String) Returns:

Continue Reading →

Coming soon in 2020: objc_direct

Before you start reading this tutorial, please read about method swizzling. The Objective-C model of object-oriented programming is based on message passing to object instances. In Objective-C one does not call a method; one sends a message.  In Objective-C, the target of a message is resolved at runtime, with the receiving object itself interpreting the message. A method is identified

Continue Reading →

Word ladder implementation in Swift

https://leetcode.com/problems/word-ladder/ This post contains the implementation for the leetcode problem shown in the link above. A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> … -> sk such that: Every adjacent pair of words differs by a single letter. Every si for 1 <= i <= k is in wordList. Note that beginWord does not need to be in wordList.

Continue Reading →

Permutations of a string in Swift

Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order. Example 1: Input: nums = [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] Example 2: Input: nums = [0,1] Output: [[0,1],[1,0]] Example 3: Input: nums = [1] Output: [[1]] Constraints: 1 <= nums.length <= 6 -10 <= nums[i] <= 10 All the integers of nums are unique.

Continue Reading →

Interacting with Javascript in WKWebView

You can use the WKWebView class to embed web content in your app. To do so, create a WKWebView object, set it as the view, and send it a request to load web content. After creating a new WKWebView object using the initWithFrame:configuration: method, you need to load the web content. Use the loadHTMLString:baseURL: method to begin loading local HTML files or the loadRequest: method to begin

Continue Reading →

Magical Candy Bags

Magical Candy BagsYou have N bags of candy. The ith bag contains arr[i] pieces of candy, and each of the bags is magical!It takes you 1 minute to eat all of the pieces of candy in a bag (irrespective of how many pieces of candy are inside), and as soon as you finish, the bag

Continue Reading →

Element Swapping

Given a sequence of n integers arr, determine the lexicographically smallest sequence which may be obtained from it after performing at most k element swaps, each involving a pair of consecutive elements in the sequence. Note: A list x is lexicographically smaller than a different equal-length list y if and only if, for the earliest

Continue Reading →

Implementing UIScrollView using constraints without using content size

UIScrollView is a view that allows the scrolling and zooming of its contained views. UIScrollView is the superclass of several UIKit classes including UITableView and UITextView. The central notion of a UIScrollView object (or, simply, a scroll view) is that it is a view whose origin is adjustable over the content view. It clips the content to its frame, which generally

Continue Reading →

Struct vs Class in Swift

If you look at structs and classes in Swift, they seem similar, but the most fundamental difference between them is how they handle values passed to them. In general Struct is pass by value, whereas Class is pass by reference. I will explain the difference by showing an example. In the above code, I have

Continue Reading →

Method swizzling in Swift and ObjectiveC

Method Swizzling is the ability that Objective-C Runtime gives us, to switch the implementation of an existing selector at runtime. I came across an article in NSHipster which does method swizzling. The link to the article is here https://nshipster.com/swift-objc-runtime/ For convenience or to work around a bug in a framework, or because there’s just no

Continue Reading →

Dispatch and global queues

Dispatch queues are FIFO queues to which your application can submit tasks in the form of block objects. Dispatch queues execute tasks either serially or concurrently. Work submitted to dispatch queues executes on a pool of threads managed by the system. Except for the dispatch queue representing your app’s main thread, the system makes no

Continue Reading →

Reading and Writing to a JSON in Swift

Lets consider you have a sample JSON as shown belowhttps://gist.github.com/kmdarshan/cfa6a940268f7091faf886ed63b3b559 You can make use of Codable Library (https://developer.apple.com/documentation/swift/codable) in Swift to read and write data from a JSON. Firstly you need to map your data from the JSON onto a Struct as shown below. Once you have done that, its easy to read it using

Continue Reading →