简体   繁体   English

Swift将创建的struct实例返回给函数

[英]Swift return created instance of struct to a function

// Here in file: Struct.swift is where I define the layout of my structs. //在文件中: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. Here in file FirstViewController.swift I get a coin ID value from an input box and then process a button click and build a URL string. 在此文件FirstViewController.swift中,我从输入框中获取硬币ID值,然后处理按钮单击并构建URL字符串。

  2. I then send that URL to fetchCoinJSON function to get JSON text 然后,我将该URL发送到fetchCoinJSON函数以获取JSON文本

*/ * /

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

// file: fetchJson.swift With the URL to get the JSON string I then get the JSON string and pass that string onto parseCoinJson function. //文件: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 here I parse the JSON and set the corresponding struct values. //文件ParseJSON.swift在这里,我解析JSON并设置相应的struct值。 Using NSLog I know that c.CoinYear does contain the correct value along with all the other values. 使用NSLog我知道c.CoinYear确实包含正确的值以及所有其他值。 However 然而

return c 返回c

gives error: ParseJSON.swift:95:16: 'coin' is not convertible to '()' 给出错误: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)

This function returns Void , which is () . 此函数返回Void ,即() You then return coin . 然后,您退还coin You meant your function to be: 您的意思是:

func parseCoinJson(anyObj:AnyObject) -> Coin

Note that types should always be leading caps. 请注意,类型应始终为大写字母。 So this should be struct Coin . 因此,这应该是struct Coin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM