Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift中创建块不起作用_Ios_Swift_Closures - Fatal编程技术网

Ios 在Swift中创建块不起作用

Ios 在Swift中创建块不起作用,ios,swift,closures,Ios,Swift,Closures,我试图在Swift中创建块 我已经像这样创建了类并声明了块 typealias JSONParserBlock=(dict:NSDictionary?,错误:NSError?)->Void 变量声明: var块:JSONParserBlock 和功能: func Getdata(WebService: String, RequsetedParameter Param:NSDictionary?, BLOCK: JSONParserBlock!) -> Void {} 但我的问题是,当我从

我试图在Swift中创建块

我已经像这样创建了类并声明了块

typealias JSONParserBlock=(dict:NSDictionary?,错误:NSError?)->Void

变量声明:

var块:JSONParserBlock

和功能:

func Getdata(WebService: String, RequsetedParameter Param:NSDictionary?, BLOCK: JSONParserBlock!) -> Void
{}
但我的问题是,当我从另一个类调用这个函数时,这个函数不会执行

函数调用如下:

var objJsonParser:JsonParser?

objJsonParser?.Getdata(str as String, RequsetedParameter:Dictionary, BLOCK:
    { (dict, error) in
        print("success")
})
自定义类代码

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
        {

        }

    }
}

您需要在
GetData
函数中调用闭包。这样做应该可以:

func Getdata(WebService: String, RequsetedParameter Param:NSDictionary?, BLOCK: JSONParserBlock!) -> Void
{
  let dict: NSDictionary? = nil    // set these to something meaningful
  let error: NSError? = nil
  BLOCK(dict: dict, error: error)  // call the closure
}

除了作者指出的问题外,另一个问题是该代码:

var objJsonParser:JsonParser?

objJsonParser?.Getdata(str as String, RequsetedParameter:Dictionary, BLOCK: {
    (dict, error) in
    print("success")
})

实际上不做任何事情,也不会抱怨,因为objJsonParser是nil,除非你给它赋值,调用中的
意味着
GetData
不会做任何事情,因为对象是nil。

你能显示这两个类的全部代码吗?是的,
只是函数的一个参数。函数体为空,因此不会执行该块。