Swift:“ViewController.Type”没有名为“URL”的成员
我有我的viewController一些代码
var URL: NSURL = NSURL(string: "http://someurl.com")! var request : NSMutableURLRequest = NSMutableURLRequest(URL:URL) request.HTTPMethod = "POST" var bodyData = "data=something" NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) in println(NSString(data: data, encoding: NSUTF8StringEncoding)) }
但是我得到的错误:“ViewController.Type”没有一个名为“URL”的成员。 如果我把代码放在ViewDidLoad中,它可以在应用程序启动时使用。
如何让代码在点击button上运行? 我试图把@IBAction放在ViewDidLoad的function,但是这也不工作:/
编辑:全视图
// // ViewController.swift // postPhp // // Created by - on 01/12/2014. // Copyright (c) 2014 Dannie C. All rights reserved. // import UIKit class ViewController: UIViewController { var URL: NSURL = NSURL(string: "http://someurl.com")! var request : NSMutableURLRequest = NSMutableURLRequest(URL:URL) request.HTTPMethod = "POST" var bodyData = "data=something" NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) in println(NSString(data: data, encoding: NSUTF8StringEncoding)) } @IBOutlet var serverLabel: UILabel! @IBAction func serverButton(sender: AnyObject) { } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }
}
您的代码都不在方法中,将其包装在方法中,并在适当的时候调用方法。