Tuesday 27 December 2016

Enums - Structs - Classes in Swift

Enums:
  • An enumeration defines a common type for a group of related values.
  • Enums are value types that have a set of cases, where each case can have different associated values. 
  • Each value of an enum type represents a single case as defined by the enum. 
  • They can’t have any stored properties.(let, var)

Syntax:
enum SomeEnumeration {
  // enumeration definition goes here
}


Structs:


Common features for Classes and Structs:

  • Define properties to store values 
  • Define methods to provide functionality 
  • Define subscripts to provide access to their values using subscript syntax 
  • Define initializers to set up their initial state 
  • Be extended to expand their functionality beyond a default implementation 
  • Conform to protocols to provide standard functionality of a certain kind
Extra benefits of classes:
  • Inheritance enables one class to inherit the characteristics of another. 
  • Type casting enables you to check and interpret the type of a class instance at runtime. 
  • Deinitializers enable an instance of a class to free up any resources it has assigned. 
  • Reference counting allows more than one reference to a class instance.
Syntax:
struct SomeStructure { 
   // structure definition goes here
}


Classes:

Unlike value types, reference types are not copied when they are assigned to a variable or constant, or when they are passed to a function. Rather than a copy, a reference to the same existing instance is used instead.
Syntax:
class SomeClass {  
  // class definition goes here
}

Sunday 10 July 2016

Updating remote GIT branch list on local machine

If you are sure that remote server repository contains more branches and they are not shown in your IDE like, XCode., then try the below commands to refresh the local list of repositories.

Command 1:
$ git branch -a
OR
$ git branch -r
Then you have to update your remote list, by:

Command 2:
$ git remote update origin --prune

Monday 27 June 2016

Creating a UIColor from a hex string

Swift 2.0 version of solution which will handle alpha value of color and with perfect error handling is here:
    func RGBColor(hexColorStr : String) -> UIColor?{
   
        var red:CGFloat = 0.0
        var green:CGFloat = 0.0
        var blue:CGFloat = 0.0
        var alpha:CGFloat = 1.0
   
        if hexColorStr.hasPrefix("#"){
       
            let index   = hexColorStr.startIndex.advancedBy(1)
            let hex     = hexColorStr.substringFromIndex(index)
            let scanner = NSScanner(string: hex)
            var hexValue: CUnsignedLongLong = 0
       
            if scanner.scanHexLongLong(&hexValue)
            {
                if hex.characters.count == 6
                {
                    red   = CGFloat((hexValue & 0xFF0000) >> 16) / 255.0
                    green = CGFloat((hexValue & 0x00FF00) >> 8)  / 255.0
                    blue  = CGFloat(hexValue & 0x0000FF) / 255.0
                }
                else if hex.characters.count == 8
                {
                    red   = CGFloat((hexValue & 0xFF000000) >> 24) / 255.0
                    green = CGFloat((hexValue & 0x00FF0000) >> 16) / 255.0
                    blue  = CGFloat((hexValue & 0x0000FF00) >> 8)  / 255.0
                    alpha = CGFloat(hexValue & 0x000000FF)         / 255.0
                }
                else
                {
                    print("invalid hex code string, length should be 7 or 9", terminator: "")
                    return nil
                }
            }
            else
            {
                print("scan hex error")
           return nil
            }
        }
   
        let color: UIColor =  UIColor(red:CGFloat(red), green: CGFloat(green), blue:CGFloat(blue), alpha: alpha)
        return color
    }

Generating QR code image in iOS SWIFT

Code to generate QR image in Swift 2.0.
Add Apple's 'Core media' framework to project.

    let reqStr = “string to convert as QR code”
    let data = reqStr.dataUsingEncoding(NSISOLatin1StringEncoding, allowLossyConversion: false)
                
    let filter = CIFilter(name: "CIQRCodeGenerator")
    filter!.setValue(data, forKey: "inputMessage")
                
    let qrImage:CIImage = filter!.outputImage!
        
    //qrImageView is a IBOutlet of UIImageView        
    let scaleX = qrImageView.frame.size.width / qrImage.extent.size.width
    let scaleY = qrImageView.frame.size.height / qrImage.extent.size.height
                
    let resultQrImage = qrImage.imageByApplyingTransform(CGAffineTransformMakeScale(scaleX, scaleY))
    qrImageView.image = UIImage(CIImage: resultQrImage)