Swift:“ViewController.Type"没有名为“URL"的成员 [英] Swift: 'ViewController.Type' does not have a member named 'URL'

查看:46
本文介绍了Swift:“ViewController.Type"没有名为“URL"的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 viewController 中有一些代码

I have some code in my 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 中,它可以在应用程序启动时运行.

However I get the error: 'ViewController.Type' does not have a member named 'URL'. If I put the code in the ViewDidLoad it works on app launch.

如何让代码在单击按钮时运行?我尝试将 @IBAction 放在 ViewDidLoad func 中,但这也不起作用:/

How do I get the code to run on the click of a button? I tried putting the @IBAction in the ViewDidLoad func but that doesnt work either :/

完整视图

 //
 //  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.
}

}

推荐答案

你的代码都不在方法中,把它包装在一个方法中,并在适当的时候调用这个方法.

None of your code is in a method, wrap it up in a method and call the method when appropriate.

这篇关于Swift:“ViewController.Type"没有名为“URL"的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆