Posts

Showing posts from 2017

NSPredicate with Swift.

Image
With NSPredicate in Objective-C it was easy to filter the NSARRAY and get the desired results ex: In the Array for Food items, if we want to filter results which has food item's having 'ice' in the foodName. And with Swift it's even getting simpler , with the use of filter method. With recent version of Swift, the use of NSPredicate is discouraged. Currently with Swift 4.0 the following code fails, but it could work in earlier versions of swift. Let me explain it to you with simple NSPredicate used along with Swift.   let foodArray = [ Food (foodId: "1" , foodName: "Pizza" , spiceLevel: 5 ),                          Food (foodId: "2" , foodName: "Pasta" , spiceLevel: 4 ),                          Food (foodId: "3" , foodName: "IceCream" , spiceLevel: 0 ),                          Food (foodId: "4" , foodName: "Yogurt IceCream" , spiceLevel: 0 )]                    

Swift - Optional Protocol Implementation

Image
In my previous blog, I explained the Protocol and optional protocol in a generic way. Now, we will dive deep into Protocol implementation and may be this is how the UITableViewDataSource and UITableViewDelegate would have been implemented. Objective's of this document would be to understand the optional protocol following concepts 1) Check implementation of the optional method by writing ? , after the name of the method. 2) When a method is optional it's type automatically becomes as optional. example type (Int) -> String becomes (Int) -> String? _________________________________________________________________________________ Lets start with a controller implementation , listed in the below code. There will no UI re-presentation , the protocol explanation is solely for simple understanding. 1)  ProfileViewsDelegate.swift. We will be implementing the protocol for the ProfileViews.swift. import Foundation import UIKit @objc protocol Profile

Swift Protocol and Implementation

Image
Protocols in Swift , define requirements that has to be implemented by a Class adopting or conforming to it. Protocols can be adopted by a Class , Structure or Enumeration , all of which can actually implement them based on the requirements. But, in one scenario you can't adopt protocol for Structure and Enumeration. Please go through the entire article for further understanding. Lets consider a simple example with Inheritance and Requirement (Protocol defines Requirements). Object Inheritance. Department Object is inherited by Administration , Finance and Human Resource Department. So all the departments will have inherited properties like departmentName and departmentId. Protocol. Protocol define the requirements. These requirements can be required or optional.  requirements If you notice in the design listed below, you will notice two type's for requirements. 1) Required  func dressCode(departmentType: Int ) -> UIColor func numberO

How to set Status Bar in iOS?

Image
Setting the  UIStatus Bar Style   is very simple in iOS. Below is how it looks like with lightContent Style and the Default Style. Based on the app. UIView backgroundColor, you can set the way a status bar should appear. There are two ways to achieve this. The first one being the recommended one since iOS 7. For this one would have to make changes in Info.plist. 1) In iOS 7 onwards, you can specify the bar style per view controller. For this the value for Key UIViewControllerBasedStatusBarAppearance in info.plist should be set to YES . Now in your controller you should override a property  preferredStatusBarStyle  as coded below.     override var preferredStatusBarStyle: UIStatusBarStyle {         get {             return . lightContent         }      } Changing the return to .default will give you dark status bar style. 2) So, if you are planning to use the default method which API says its Deprecated, you need to  UIViewControllerB

Swift Class versus Structure

In Swift Just like classes play a important role , Structure do play a important role. What could be the notable difference? First one is Structure is pass by value and Classes are pass by reference.  Below is a simple example to understand the meaning of it. func getAddress( _ object: Any )-> String {     var newObject = object     var address: String = String ( "Could Not CalculateAddress" )     withUnsafePointer (to: &newObject) {                  address = " \ ( $0 )"     }     return address } func getAddressForClass( _ object: AnyObject ) -> Any {     return Unmanaged < AnyObject >. passUnretained (object). toOpaque () } //Structure struct User{     var name: String     var userId: Int } func changeStructuredValue( _ inputStruct: User ){     var newInputStruct = inputStruct     newInputStruct. name =   "Rose"     print ( "Modifying Struct , Addres

CMTimeMakeWithSeconds explained

Image
CMTime  is structure which has two main components,  timeScale  and  value. CMTIME = VALUE  / TIMESCALE where, TimeScale =  fraction of a second each unit in numerator occupies. CMTIME = Value / TimeScale 1)  public   func  CMTimeMake( _  value:  Int64 ,  _  timescale:  Int32 ) ->  CMTime CMTimeMake ( 6000 ,  10 ) Means, there are 6000 units , each unit occupies 1/10 of a second 2)  public   func  CMTimeMakeWithSeconds( _  seconds:  Float64 ,  _  preferredTimescale:  Int32 ) ->  CMTime CMTimeMakeWithSeconds(10.0,500) 10.0 = VALUE /500 Therefore VALUE = 5000 So, there are 5000 units, each occupy 1/500 of a second

UILabel dynamic height with Autolayout

Image
Adding a UILabel to UIScrollView and with a couple of lines of code you would be able to achieve the dynamic height. But, there is one more way and thats without writing a single line of code. So, lets get started on it. Approach 1: Mistakes that we would make is explained below. (UILabel and UIScrollView) Add a UIScrollView , set top, left, bottom, right margin set to 0 against superview as per your requirement. Add the UILabel and we do the same for UILabel (top,left,bottom,right margin set to 0 with respect to SuperView, which is UIScrollView ) With this approach scrollview would not get idea on the scroll limits. It will set the content size, where it would scroll horizontally. You can still go with this approach. Explained in conclusion. Approach 2: Now, lets go with the correct way to Code .  It includes the following hierarchy UIView -> UIScrollView -> UILabel. Step 1  : Add the UIView , set the margin according. Here I am using