Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages.
Syntax with Function
[code language=”obj-c”]
func yourFunctionName(parameter:Type, … , withCompletionHandler:(result:Type) -> Void)
[/code]
For more closure syntax : goshdarnclosuresyntax.com
Example
Function Definition:
[code language=”obj-c”]
func closureReturn(isTest:Bool, withCompletionHandler:(result:String) -> Void) {
if(isTest){
withCompletionHandler(result: "Yes")
}
else{
withCompletionHandler(result: "No")
}
}
[/code]
Calling Function:
[code language=”obj-c”]
closureReturn(true) { (result) -> Void in
print(result)
}
[/code]
Output should be respective to value true/false.
In next post I will write a tutorial on a wrapper class for Alamofire with use of SwiftyJSON by using closures.
CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. It has over ten thousand libraries and can help you scale your projects elegantly.
In Objective-C we are using a header file to create constant variables like as
[code lang=”obj-c”]
// Objective-C
#define APP_ALERT_TITLE "Objective-C Constant"
[/code]
Swift has new syntax to define the constant (#define)
[code lang=”obj-c”]
// Swift
let APP_ALERT_TITLE = "Swift Constants"
[/code]
Lets try
Create a swift file with the constants
[code lang=”obj-c”]
import Foundation
class Constants {
// MARK: List of Constants
static let APP_ALERT_TITLE = "Swift Constants"
static let SAMPLE_MESSAGE = "The alert is working !!"
}
[/code] Note : Here the MARK statement is also changed.
[code lang=”obj-c”]
// Objective-C
#pragma mark –
#pragma mark List of Constants
[/code]
[code lang=”obj-c”]
// Swift
// MARK: List of Constants
[/code]
3. Now we will create a Base UIViewController to use anywhere in the project which control the delegate of menu.
First we will create this 3 lines Drawer Icon via Code
To open a view controller by identifier :
Set the Restoration Identifier and Storyboard Identifier. If current view is open then we will not open it once again for that we have to check via Restoration Identifier.
By giving you a simple example that we can not change the corner radius from the design view. We have to change it run time. If you want to make possible it with design time then you can use the @IBDesignable and @IBInspectable.
So, We are taking an example as IBButtonExtender for this the functionality
Border Color
Border Width
Corner Radius
Create an Custom Class for UIButton with @IBDesignable
Swift
1
2
3
@IBDesignable
classButtonExtender: UIButton{
}
Create an @IBInspectable for Inspect the element. the Border Color property will be added to the list in the right panel when you create button with this class.
Presenting the ButtonExtender for Border Color, Border Width and Corner Radius function with the @IBDesignable and @IBInspectable in the Swift Language.
By making some corner radius and colors, You will get neat and clean output.
Github
Find IBButtonExtender on github 🙂
Check other articles on Swift Language.
All suggestions are acceptable. Put it in the comments!
Happy Coding 🙂
What is Realm?
Realm is a mobile database
Realm is a replacement for SQLite & Core Data.
It can save you thousands of lines of code & weeks of work,
and lets you craft amazing new user experiences.
Download the Realm framework for swift from https://realm.io/
[code language=”obj-c”]
import UIKit
import RealmSwift
class Person: Object {
dynamic var name = ""
dynamic var age = 0
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Initializing the object
let personObj1 = Person()
personObj1.name = "Ashish"
personObj1.age = 25
println("name of person : \(personObj1.name)")
// Writing it to realm
let realm = Realm()
realm.write {
realm.add(personObj1)
}
let personObj2 = Person()
personObj2.name = "Darshak"
personObj2.age = 27
realm.write {
realm.add(personObj2)
}
// Quering
let r = Realm().objects(Person).filter("age > 24")
println(r)
// Queries are chainable
let r2 = r.filter("name contains ‘Ashish’")
println(r2)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
[/code] Output :
[code language=”obj-c”]
name of person : Ashish
Results<Person> (
[0] Person {
name = Ashish;
age = 25;
},
[1] Person {
name = Darshak;
age = 27;
}
)
Results<Person> (
[0] Person {
name = Ashish;
age = 25;
}
)
[/code]
Happy Coding 🙂