Ans :
Swift Extension :
Add a new swift file with File > New > File... > iOS > Source > Swift File, but you can call them what you want.
The general naming convention is to call it TypeName+NewFunctionality.swift
Make extension of Double
Double+Conversions.swift
import Swift // or Foundation
extension Double {
func celToFahren() -> Double {
return self * 9 / 5 + 32
}
func fahrenToCel() -> Double {
return (self - 32) * 5 / 9
}
}
How to make extension:
let boilingPointCel = 100.0
let boilingPointFaren = boilingPointCel.celToFahren()
print(boilingPointFaren) // 212.0
Make extension of UIColor
UIColor+CustomColor.swift
import UIKit
extension UIColor {
class var customGreen: UIColor {
let darkGreen = 0x008110
return UIColor.rgb(fromHex: darkGreen)
}
class func rgb(fromHex: Int) -> UIColor {
let red = CGFloat((fromHex & 0xFF0000) >> 16) / 0xFF
let green = CGFloat((fromHex & 0x00FF00) >> 8) / 0xFF
let blue = CGFloat(fromHex & 0x0000FF) / 0xFF
let alpha = CGFloat(1.0)
return UIColor(red: red, green: green, blue: blue, alpha: alpha)
}
}
See here also.
Using extension :
view.backgroundColor = UIColor.customGreen
Summary : Once you define an extension it can be used anywhere in your app just like the built in class functions. In Objective-C extensions are known as categories.
Objective C Extension :
In objective c, when you want to make behavior of some property private you use class extension.
-> it comes with .m file only.
-> mainly for properties.
The implementation of the extension must be in the main @implementation block of the file.
Extension can only be added to the classes whose source code is available because compiler compile the source code and extension at same time.
Swift Extension :
Add a new swift file with File > New > File... > iOS > Source > Swift File, but you can call them what you want.
The general naming convention is to call it TypeName+NewFunctionality.swift
Make extension of Double
Double+Conversions.swift
import Swift // or Foundation
extension Double {
func celToFahren() -> Double {
return self * 9 / 5 + 32
}
func fahrenToCel() -> Double {
return (self - 32) * 5 / 9
}
}
How to make extension:
let boilingPointCel = 100.0
let boilingPointFaren = boilingPointCel.celToFahren()
print(boilingPointFaren) // 212.0
Make extension of UIColor
UIColor+CustomColor.swift
import UIKit
extension UIColor {
class var customGreen: UIColor {
let darkGreen = 0x008110
return UIColor.rgb(fromHex: darkGreen)
}
class func rgb(fromHex: Int) -> UIColor {
let red = CGFloat((fromHex & 0xFF0000) >> 16) / 0xFF
let green = CGFloat((fromHex & 0x00FF00) >> 8) / 0xFF
let blue = CGFloat(fromHex & 0x0000FF) / 0xFF
let alpha = CGFloat(1.0)
return UIColor(red: red, green: green, blue: blue, alpha: alpha)
}
}
See here also.
Using extension :
view.backgroundColor = UIColor.customGreen
Summary : Once you define an extension it can be used anywhere in your app just like the built in class functions. In Objective-C extensions are known as categories.
Objective C Extension :
In objective c, when you want to make behavior of some property private you use class extension.
-> it comes with .m file only.
-> mainly for properties.
The implementation of the extension must be in the main @implementation block of the file.
Extension can only be added to the classes whose source code is available because compiler compile the source code and extension at same time.
if possible please leave youtube link also , it will be more helpful to us to understand more clear
ReplyDeleteOk. I will try to make video and upload on youtube. It will take some time. Keep visiting...
Delete