2016-03-01 187 views
-1

我有一个称为EXC错误指令的错误。有人可以帮我弄清楚。插座面板上显示:EXC错误指令错误

由于传输安全性不安全,因此已阻止明文HTTP(http://)资源加载。临时例外可以通过您的应用程序的Info.plist文件进行配置。 致命错误:意外发现零,同时展开一个可选值

这是代码。

import Foundation 

let kNotificationStocksUpdated = "stocksUpdated" 

class StockManagerSingleton { 

//Singleton Init 
class var sharedInstance : StockManagerSingleton { 
    struct Static { 
     static let instance : StockManagerSingleton = StockManagerSingleton() 
    } 
    return Static.instance 
} 

/*! 
* @discussion Function that given an array of symbols, get their stock prizes from yahoo and send them inside a NSNotification UserInfo 
* @param stocks An Array of tuples with the symbols in position 0 of each tuple 
*/ 
func updateListOfSymbols(stocks:Array<(String,Double)>) ->() { 

    //1: YAHOO Finance API: Request for a list of symbols example: 
    //http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quotes where symbol IN ("AAPL","GOOG","FB")&format=json&env=http://datatables.org/alltables.env 

    //2: Build the URL as above with our array of symbols 
    var stringQuotes = "("; 
    for quoteTuple in stocks { 
     stringQuotes = stringQuotes+"\""+quoteTuple.0+"\"," 
    } 
    stringQuotes = stringQuotes.substringToIndex(stringQuotes.endIndex.predecessor()) 
    stringQuotes = stringQuotes + ")" 

    let urlString:String = ("http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quotes where symbol IN "+stringQuotes+"&format=json&env=http://datatables.org/alltables.env").stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)! 
    let url : NSURL = NSURL(string: urlString)! 

    let request: NSURLRequest = NSURLRequest(URL:url) 
    let config = NSURLSessionConfiguration.defaultSessionConfiguration() 
    let session = NSURLSession(configuration: config) 

    //3: Completion block/Clousure for the NSURLSessionDataTask 
    let task : NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in 

     do { 
      if let jsonDict = try NSJSONSerialization.JSONObjectWithData(data!, options:.MutableContainers) as? NSDictionary { 
       // Success block... 
       //5: Extract the Quotes and Values and send them inside a NSNotification 
       let quotes:NSArray = ((jsonDict.objectForKey("query") as! NSDictionary).objectForKey("results") as! NSDictionary).objectForKey("quote") as! NSArray 
       dispatch_async(dispatch_get_main_queue(), { 
        NSNotificationCenter.defaultCenter().postNotificationName(kNotificationStocksUpdated, object: nil, userInfo: [kNotificationStocksUpdated:quotes]) 
       }) 
      } 
     } catch { 
      print(error) 
     } 

    }) 


    //6: DONT FORGET to LAUNCH the NSURLSessionDataTask!!!!!! 
    task.resume() 
} 
} 

与它的问题的线,

if let jsonDict = try NSJSONSerialization.JSONObjectWithData(data!, options:.MutableContainers) as? NSDictionary 
+0

你什么行就不好了指令? –

+0

问题底部的那一行。 –

+1

可能的重复[如何在iOS 9中启用启用了App Transport Security的HTTP URL?](http://stackoverflow.com/questions/30731785/how-do-i-load-an-http-url-with -app-transport-security-enabled-in-ios-9) – Moritz

回答

0

应用交通运输安全造成您的请求失败,因为你正在请求不使用HTTPS的URL。然后在完成处理程序中,数据为零,这可能是导致异常的原因,因为您正在强制解包它。

在尝试使用它来停止异常之前,您需要添加一个检查以确保数据不为零。

为了让网络请求的工作,您可能需要使用HTTPS URL或禁用App Transport Security

+0

你可以在代码中显示答案,我是Swift新手。 @Clever错误 –