ios - NSString 没有名为 bridgeToObjectiveC 的成员

标签 ios objective-c swift

我在下面的代码中遇到编译器错误。我已经导入了所需的 Foundation 库,所以我不确定到底出了什么问题,请有人建议我可能做错了什么,或者我可以做些什么来解决这个问题?

谢谢

import UIKit
import Foundation

class ViewController: UIViewController {


    @IBOutlet var areaTxt: UITextField!
    @IBOutlet var resultLbl: UILabel!

    @IBAction func goBtn(sender: AnyObject) {

        if(countElements(areaTxt.text) > 0){
            var removedSpaces = areaTxt.text.stringByReplacingOccurrencesOfString(" ", withString: "");
            var url = "http://weather-forecast.com/locations/" + removedSpaces + "/forecasts/latest";
            self.getWeatherReportForArea(url);
        }else{
            let alert = UIAlertView()
            alert.title = "Alert"
            alert.message = "The user location is empty"
            alert.addButtonWithTitle("Ok")
            alert.show()
        }
    }

    func getWeatherReportForArea(area: String){
        var url = NSURL(string: area);
        let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) in
            var urlContent = NSString(data: data, encoding: NSUTF8StringEncoding);

            if(urlContent.bridgeToObjectiveC().containsString("<span class=\"phrase\">")){

                var contentArray = urlContent.componentsSeparatedByString("<span class=\"phrase\">");
                var newContentArray = contentArray[1].componentsSeparatedByString("</span>");

                var value = newContentArray[0].stringByReplacingOccurrencesOfString("&deg;", withString: "º");

                dispatch_async(dispatch_get_main_queue()){

                    self.resultLbl.text = value;
                }

            }else{

            }
        }

        task.resume();
    }

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

最佳答案

bridgeToObjectiveC() 已在 Xcode 6 Beta 5 中移除。现在桥接是透明的,只需使用

if urlContent.containsString("<span class=\"phrase\">") {
    ...
}

关于ios - NSString 没有名为 bridgeToObjectiveC 的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26057900/

相关文章:

ios - scrollsToTop 和 UIRefreshControl 的错误

arrays - 在 Swift 中保留一组自定义结构

ios - CGColor 到 UIColor 转换

ios - Swift:使用带有多个参数的选择器

ios - 将应用程序上传到 testflight

ios - UITableView 重叠单元格(力)

ios - 应用程序自动部署到 Apple Store

ios - iPhone/iPad : How to make UITextField readonly (but not disabled)?

ios - 无法构建 Objective-C 模块 'CoreGraphics'

ios - 核心数据 : Insert and delete with To many relationship