Tag: swift

How to call async methods from objective-c

Consider the below piece of code, its written in Swift and it uses Async/Await. In order to use this code in Swift, you would do this This can be called from objective-c directly as follows The Objective-C method implementation synthesized by the compiler will create a detached task that calls the async Swift method perform(operation:) with the given string,

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 →

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 →

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 →