Swift return 为函数创建了结构实例

Swift return created instance of struct to a function

// Here in file: Struct.swift 是我定义结构布局的地方。

struct coin 
{
    var coinYear        = String("")
    var userID          = String("")
    var coinSellCoinID  = String("")
    var coinImageURL    = String("")
    var mintYear        = String("")
    var about           = String("")
    var allowBarter     = String("")
}

/*

  1. 在文件 FirstViewController.swift 中,我从输入框中获取硬币 ID 值,然后处理按钮单击并构建 URL 字符串。

  2. 然后我将 URL 发送到 fetchCoinJSON 函数以获取 JSON text

*/

@IBAction func btnSubmitCoinID(sender: AnyObject)
{
    let temp = coinID.text
    let theCoinURL = NSURL(string: MyVariables.url + "getCoinInfo.php?coinID=" + temp)
    fetchCoinJSON(theCoinURL!)
}

// 文件:fetchJson.swift 使用 URL 获取 JSON 字符串,然后获取 JSON 字符串并将该字符串传递给 parseCoinJson 函数。

       func fetchCoinJSON(theurl: NSURL)
        {
            var error: NSError?

var notWorking = coin()

MyVariables.rawCoinJSON =

NSString(contentsOfURL: theurl, encoding: NSUTF8StringEncoding, error: &error)!

var data: NSData =
MyVariables.rawCoinJSON.dataUsingEncoding(NSUTF8StringEncoding)!

    let anyObj: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error)
            println("Error: \(error)")

    notWorking = parseCoinJson(anyObj!) <--- var notWorking = nil
            NSLog("OoOoOoOoOoOoOoO")
            println(notWorking.coinWeight) <--- logs null
        }

//file ParseJSON.swift 在这里我解析了 JSON 并设置了相应的结构值。使用 NSLog 我知道 c.CoinYear 确实包含正确的值以及所有其他值。不过

returnc

给出错误:ParseJSON.swift:95:16: 'coin' 不可转换为 '()'

    func parseCoinJson(anyObj:AnyObject)
    {
        if  anyObj is Array<AnyObject>
        {
            var c = coin()
            for json in anyObj as! Array<AnyObject>
            {
                c.userID = (json["userid"] as AnyObject? as? String) ?? String("") ?? String ("")
                c.mintYear = (json["mintyear"] as AnyObject? as? String) ?? String("") ?? String ("")
                c.coinActiveStatus = (json["isActive"] as AnyObject? as? String) ?? String("") ?? String ("")
                c.coinSellCoinID = (json["sellcoinid"] as AnyObject? as? String) ?? String("") ?? String ("")
                c.coinImageURL = (json["coinImageURL"] as AnyObject? as? String) ?? String("") ?? String ("")
                c.about = (json["about"] as  AnyObject? as? String) ?? String("") ?? String ("")
                c.allowBarter = (json["allowBarter"] as  AnyObject? as? String) ?? String("") ?? String ("")

....

    }
            return c
        }
    }
func parseCoinJson(anyObj:AnyObject)

这个函数returnsVoid,也就是()。然后你returncoin。你的意思是你的函数是:

func parseCoinJson(anyObj:AnyObject) -> Coin

请注意,类型应始终为前导大写。所以这应该是 struct Coin.