How to create a wrapper for Alamofire and SwiftyJSON? Swift – iOS

This blogpost updated with Swift 3 – Xcode 8 – iOS 10 – Alamofire 4.0

AFWrapper
AFWrapper

I have posted a basic tutorial about Alamofire and SwiftyJSON How to use Alamofire and SwiftyJSON with Swift?

And some day before we have learned about Use of Blocks(Closures) or Completion Handlers with Function in Swift – iOS

Let’s combine both the topics to make a WRAPPER of Alamofire and SwiftyJSON.
Continue reading

Use of Blocks(Closures) or Completion Handlers with Function in Swift – iOS

Blocks in Objective-C

In Objective-C we are using the blocks(completion handlers) with functions as follows :

- (void)yourFunctionName:(NSString *)yourString withCompletionHandler:(void (^)(NSString *yourResult))block;

Closures in Swift

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

func yourFunctionName(parameter:Type, ... , withCompletionHandler:(result:Type) -> Void)

For more closure syntax : goshdarnclosuresyntax.com

Example

Function Definition:

func closureReturn(isTest:Bool, withCompletionHandler:(result:String) -> Void) {
    if(isTest){
        withCompletionHandler(result: "Yes")
    }
    else{
        withCompletionHandler(result: "No")
    }
}

Calling Function:

closureReturn(true) { (result) -> Void in
    print(result)
}

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.

It will be related to this post How to use Alamofire and SwiftyJSON with Swift? – Swift 2 – iOS 9 – Xcode 7

Happy Coding 🙂

Issue “This certificate has an invalid issuer” because of Apple Worldwide Developer Relations Intermediate Certificate Expiration

I am posting this article because many users are facing this issue like “This certificate has an invalid issuer”

This certificate has an invalid issuer
This certificate has an invalid issuer

As apple have written :

The Apple Worldwide Developer Relations Certification Intermediate Certificate expires soon and we’ve issued a renewed certificate that must be included when signing all new Apple Wallet Passes, push packages for Safari Push Notifications, and Safari Extensions starting February 14, 2016.

As I found an answer in post on the Stack Overflow : This certificate has an invalid issuer regrading Intermediate Certificate Expiration

Follow this steps :

  1. Open KeyChain access, Delete “Apple world wide Developer relations certification authority” (Which expires on 14th Feb 2016) from both “Login” and “System” sections. If you can’t find it, use “Show Expired Certificates” in the View menu
  2. Download AppleWWDRCA.cer and add it to Keychain access > certificates (which expires on 8th Feb 2023)
  3. Everything should be back to normal and working now (Check the certificate is valid and it should be display in green)

Related Articles:

How to use Alamofire and SwiftyJSON with Swift? – Swift 3 – iOS 10 – Xcode 8

Swift JSON
Swift JSON

Alamofire is an HTTP networking library written in Swift.
SwiftyJSON makes it easy to deal with JSON data in Swift.
Continue reading

Realm – mobile database with Swift

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/

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.
    }
}

Output :

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;
	}
)

Happy Coding 🙂