What is the difference between setNeedsLayout, layoutIfNeeded and layoutSubviews? Quick Notes.

Screen Shot 2018-06-06 at 9.50.17 AM

When the user taps an on App icon, iOS creates an instance of UIApplication which runs on the main thread. This main thread processes all UI events such as touch, location updates etc.

After app launched successfully, it becomes available for the user to do interactions with it and every single interaction which known as an event is processed in a FIFO manner.

Now while events are being processed there may come some UI update requests which aren’t get fulfilled immediately. Instead, the system waits until the existing process finishes and the next redraw cycle comes. There is a periodic interval between the event processing and UI layout handling. The setLayoutNeeded() and layoutIfNeeded are two UIView’s methods here to ask the system for updating the view with new changes.

The setNeedsLayout() tells the system that you want the view and all it’s subviews to be redrawn in the update cycle.

Here is What Apple’s doc says on this, “Call this method on your application main thread when you want to adjust the layout of a view’s subviews. Note that it returns immediately (Async call) because it does not force an update, it waits for the next update cycle”.

Whereas layoutIfNeeded() is an async call which tells the system that you want the view and it’s subviews to be redrawn immediately without waiting for the next update cycle.

UIView have one more method named layoutSubviews() which you can use for your own implementation to set the frame of your view directly by overriding it. And since it’s default implementation uses the constraints you have given to determine it’s size and positions to get the frame, you should override it unless autoresizing and constraint behavior is not working for you as expected.

In short use setNeedsLayout(), if you are okay with the system to update it in the next upcoming update cycle and use layoutIfNeeded() if you want it to be updated immediately.

Thanks for reading.

Swift | Open source projects for learning and developers to follow on twitter

Screen Shot 2018-10-05 at 10.26.35 AM

Announced in 2014, the Swift programming language has quickly become one of the fastest growing languages in history. Swift makes it easy to write software that is incredibly fast and safe by design.

Blog for developers to highlight, top 10 trending open source to learn.

  1. MessageViewController A SlackViewController replacement written in Swift, compatible with iPhone X, iOS12. Created by Ryan Nystrom
  2. Swift interpreter for Pascal language (Inspired from Ruslan’s Blog). Created by Igor Kulman
  3. CollectionViewSlantedLayout subclass of the UICollectionViewLayout allowing the display of slanted cells in a UICollectionView.Created by Yassir Barchi
  4. Mint Package manager that installs and runs Swift command line tools, created by Yonas Kolb
  5. CryptoSwift Collection of standard and secure cryptographic algorithms implemented in Swift. Created by Marcin Krzyzanowski
  6. CocoaDebug Debugger tools for iOS supports Swift and Objective C, created by CocoaDebug
  7. iOS-Depth-Sampler Examples of Depth APIs in iOS, created by Shuichi Tsutsumi
  8. Universal Link Testing  It fetches and parses apple-app-site-association file for you to quickly check whether Universal Links are working. created by Ethan Huang
  9. Swift Syntax Set of Swift bindings for the libSyntax library. It allows for Swift tools to parse, inspect, generate, and transform Swift source code. Created by Apple
  10. WhatsNewKit Showcase your awesome new app features, Created by Sven Tiigi

 

Thanks for reading!

WWDC 2018 | Keynote |Quick Notes | 2 mins

Screen Shot 2018-06-06 at 9.55.14 AM.png

“Today is all about Software,” Apple CEO Tim said as he took the stage at WWDC 2018.

It all started with statistics.

  • 20 million iOS developers
  • 500 million weekly app store visits
  • 81% using iOS 11 where only 8% using latest Android
  • Developers earned 100 million through App Store

 

Screen Shot 2018-06-06 at 9.50.17 AM.png

iOS

  • New iOS 12 will be released as a free update, every model back to the 5S will get a performance boost.
  • ARKit, second generation, which included a lot of cool updates, most interesting is the ability for multiple users to observe and interact with the same environment from different angles.
  • Apple Screen time, now iOS 12 will tell you how much time you’re spending with your iPhone and which are the apps.
  • Do not disturb a lot of improvements, while you are sleeping will hide the notification, one step one time notifications.
  • Photos, with iOS 12 they have revamped it. Allowing more search options driven by AI, share the photos through iMessage.
  • Siri, it’s handling 10 billion requests per month and now Apple is allowing you to interact with it more, create shortcuts to trigger app features.
  • Animojis, more personal now. It’s super accurate, face-tracking and tongue tracking capabilities. Surely it will feel you very familiar.
  • Group FaceTime, now chat with 32 people at once with emoji, sticker, and other effects support.

iOS Apps updates

  • Stocks, new UI with better performance
  • Apple News app.
  • Apple iBooks getting a redesign and a new name.
  • Voice Memos, iPad getting it for the first time and recording will be now synced to iCloud.

 

Screen Shot 2018-06-06 at 9.51.04 AM.png

watchOS

So far biggest functions of the Apple iWatch is fitness, now Apple adding more components which let you challenge your friends.

  • Automatic workout detection using your heart-beats(once you stop working out your heart rate decreases, so it will automatically stop).
  • Walkie-Talkie, Texting/phone are tough using watch screen so Apple added an instant audio messaging app called walkie-talkie (uses the wifi or cellular to interact with other devices).
  • Siri on Watch, allow third-party apps to show you more information. Now just lift your wrist and start talking it means no more “Hey Siri”.
  • Podcasts app is coming on your wrist.

 

Quick Notes | Swift classes into Objective-C Code when you have multiple targets.

How to use Swift classes into Objective Code when you have multiple targets.

For using Swift classes in Objective-C Code when you have one target in your app. We generally need to andimport TargetName-Swift.h next to add support in your Swift file you need to inherit if from NSObject or use @objc flag.

What if you have multiple targets in your codebase? As you cannot import, TargetNameA-Swift.h TargetNameB-Swift.h etc. In that case, you can tell the compiler that not to create Swift module specific to target change it to project name because the project will be same and unique.

For achieving this select your every target, Build Settings and change the build configuration.

Objective-C Generated Interface Header Name —> replace  $(SWIFT_MODULE_NAME)-Swift.h that is specific to the each target, with ​​​​$(PROJECT_NAME)-Swift.h

Screen Shot 2018-04-19 at 11.02.20 AM.png

Screen Shot 2018-04-19 at 11.02.38 AM.png

Later import PROJECT_NAME-Swift.h in your Objective C code to make use of your Swift code.

iOS Wireless Install, Debug Builds using WiFi | Quick Notes

Yes, correct you heard it right. It was the most awaited feature because nobody wants to spend a lot on Apple connectors, haha. But unfortunately, I don’t see people are utilizing this features. So here we go with one to kick start.

Prerequisites:

  1. iOS device with later or iOS 11 OS version.
  2. MacBook with macOS 10.12.4 or later.
  3. Xcode IDE with 9 or later
  4. Developer Macbook and iOS device should be on same WiFi network.

I hope after reading prerequisites you are clear about what is gonna happen. Let’s move on further steps.

Step: 1 Here you need your connector cable for first and yes last time. Connect your iOS device and open Xcode.

Step 2: Switch to,Window,Devices and Simulators options, below is the screen for reference.

Screen Shot 2018-03-28 at 10.44.58 AM.png

Step 3: Just enable the optionConnect via network.

Step 4: Sometimes it will ask you to provide your iOS device IP to enter but in my case, it didn’t ask.

Thanks for reading!

Creating and Distributing iOS Frameworks | Swift | Tips & Tricks

Why do we need to create frameworks? because it has three major purposes mentioned below and also you can share your code with your team, other developer or iOS community.

  • Code encapsulation
  • Code modularity
  • Code reuse

Let’s create the iOS framework.

Screen Shot 2018-02-05 at 8.24.50 PM

Now just give some meta-data about your framework like name, organization, identifier etc (I hope you’re already aware of it).

Screen Shot 2018-02-05 at 8.26.54 PM.png

We’re good to go till now. Just add the files that you want to keep within your framework.

add

Build the framework project to make sure that you get build succeeded with no build warnings or errors.

Things that you need to take care.

  • Make sure to check Copy items if needed, so that the files actually copy into the new project instead of just adding a reference. Frameworks need their own code, not references, to be independent.
  • Double-check that each of the files has Target Membership in ThreeRingControl to make sure they appear in the final framework. You can see this in the File Inspector for each file.
  • Double-check the access modifiers, while creating framework access modifiers plays very important roles.
  • if you’re creating Swift framework make sure you’re extending classes from NSObject otherwise it won’t reflect once you will import the framework.

Thanks for reading.

STUPID principles | Quick Notes

Recent blog: SOLID vs STUPID

STUPID principles and yes this may hurt your feeling but yes if you are following this you are writing stupid code.

Singleton

Well known design pattern, but mostly understood one as well. Like me, people are also fighting with Singleton syndrome. In that case, whenever we create a class we create it Singleton but why? That is definability not cool. Singleton also considered anti-patterns. We should think and avoid if possible because the program using Singleton are hard to test because of global state, with the global state they hide their dependencies.

Tight Coupling

Avoiding static things is important to avoid tight coupling. If it’s there then making a change in one module requires to changes another module too. Now it’s really difficult to reuse and difficult to test.

Untestability

Testing is not hard, what you think? We don’t write test case may be because we don’t have the time or the code we are writing is bad because of tight coupling?

Premature Optimization

One of my favorite quote from Donald Knuth, premature is the root of all evil. There are only cost and no benefits. Actually optimised code is much complex as compared to just writing a loop.

Indescriptive Naming

That’s what I like most and I try to follow as much as possible. Name of your class, variables or methods should be clear because at the end of the day human is only going to read because programming languages are for human not for computers, computers can understand 0 or 1 very well.

Duplication

That’s bad. Writing same code again and again so don’t repeat yourself, keep it simple and strong. Write Code only once.

Thank you for reading the quick introduction of STUPID principles, next will be SOLID principles.