I am trying to Create Block in Swift.
I have created class and declared block like this
typealias JSONParserBlock = (dict:NSDictionary?,error:NSError?) -> Void
Variable declaration:
var block: JSONParserBlock!
and Function:
func Getdata(WebService: String, RequsetedParameter Param:NSDictionary?, BLOCK: JSONParserBlock!) -> Void
{}
But my problem is when I call this function from another class, this function does not execute
Function call is like this:
var objJsonParser:JsonParser?
objJsonParser?.Getdata(str as String, RequsetedParameter:Dictionary, BLOCK:
{ (dict, error) in
print("success")
})
Custom class code
import UIKit
class JsonParser: NSObject,NSURLConnectionDelegate
{
typealias JSONParserBlock = (dict:NSDictionary?,error:NSError?) -> Void
var block: JSONParserBlock!
var Data:NSMutableData?=nil
var delegate:JsonParserDelegate?
var DICT:NSDictionary!
var error: NSErrorPointer=nil
func Getdata(WebService: String, RequsetedParameter Param:NSDictionary?, BLOCK: JSONParserBlock!) -> Void
{
self.block=BLOCK;
Data = NSMutableData()
let urlPath: String = "http://192.168.13.2/freshbakala/admin/json/GetCategory.php"
let url: NSURL = NSURL(string: urlPath)!
let request: NSURLRequest = NSURLRequest(URL: url)
let connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: true)!
connection.start()
}
func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!)
{
self.Data = NSMutableData()
}
func connection(connection: NSURLConnection!, didReceiveData data: NSData!)
{
self.Data?.appendData(data)
}
func connectionDidFinishLoading(connection: NSURLConnection!)
{
let error:NSError
let jsonresult:NSDictionary
print("Response is received")
do
{
jsonresult = try NSJSONSerialization.JSONObjectWithData(self.Data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
print(jsonresult)
block(dict: jsonresult,error: nil)
}
catch
{
}
}
}