这里是我的代码;
func startConnection(){ let urlPath: String = "http://binaenaleyh.net/dusor/" var url: NSURL = NSURL(string: urlPath) var request: NSURLRequest = NSURLRequest(URL: url) var connection: NSURLConnection = NSURLConnection(request: request,delegate: self,startImmediately: false) connection.start() } func connection(connection: NSURLConnection!,dIDReceiveData data: NSData!){ self.data.appendData(data) } func buttonAction(sender: UIbutton!){ startConnection() } func connectionDIDFinishLoading(connection: NSURLConnection!) { var err: NSError // throwing an error on the line below (can't figure out where the error message is) var JsonResult: NSDictionary = NSJsONSerialization.JsONObjectWithData(data,options: NSJsONReadingOptions.MutableContainers,error: nil) as NSDictionary }
这是JsON的链接;
http://binaenaleyh.net/dusor/
我在这里做错了什么?
这两个函数为我工作:func getJsON(urlToRequest: String) -> NSData{ return NSData(contentsOfURL: NSURL(string: urlToRequest)) } func parseJsON(inputData: NSData) -> NSDictionary{ var error: NSError? var boardsDictionary: NSDictionary = NSJsONSerialization.JsONObjectWithData(inputData,error: &error) as NSDictionary return boardsDictionary }总结
以上是内存溢出为你收集整理的在swift中下载和解析json全部内容,希望文章能够帮你解决在swift中下载和解析json所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)