json data not getting loaded into UITextView in swift - json

class ViewController:ViewController,UITextViewDelegate{
#IBOutlet weak var newTextView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
newTextView.delegate = self
dataFun()
}
func dataFun()
{
let url : String = "http:xyz/abc"
let request : NSMutableURLRequest = NSMutableURLRequest()
request.URL = NSURL(string: url)
request.HTTPMethod = "GET"
print("Start")
let session = NSURLSession.sharedSession()
session.dataTaskWithRequest(request) { (data, response, error) -> Void in
do {
let jsonResult: NSDictionary! = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as? NSDictionary
print("In method\(jsonResult)")
// let data = jsonResult["description"]
// print(data!)
if (jsonResult != nil)
{
// process jsonResult
print("Data added")
let test:String = jsonResult["description"] as! String
print(test)
self.newTextView.text = test
} else {
print("No Data")
// couldn't load JSON, look at error
}
}
catch {
print("Error Occured")
}
}
.resume()
}
In my app I am going to call services from API
I can see my json data in console.
that data is not show in textviewController
it shows fatal error:
unexpectedly found nil while unwrapping an Optional value
and then crash the app

Make sure #IBOutlet weak var newTextView: UITextView! is set up correctly.
Make sure let test:String = jsonResult["description"] as! String doesn't crash. JSON has field description and it's a string.

Related

JSON Parsing sometimes crashes Swift

I am parsing JSON in my iOS app and sometimes when the network connection is weak but isn’t gone, the app will crash while trying to parse the JSON, because it says it had an error while force unwrapping a nil.
The code I use for that is here.
//
// MessageModel.swift
// truthordare
//
// Created by Dustin Palmatier on 11/2/19.
// Copyright © 2019 Hexham Network. All rights reserved.
//
import UIKit
protocol MessageModelProtocol: class {
func itemsDownloaded(items: NSArray)
}
class MessageModel: NSObject, URLSessionDataDelegate {
//properties
weak var delegate: MessageModelProtocol!
let urlPath = "Redacted" //this will be changed to the path where service.php lives
let deleteUrl = "REDACTED"
func downloadItems(TYPE: String, IDENTIFIER: String) {
let url: URL = URL(string: urlPath)!
var request = URLRequest(url: url)
request.httpMethod = "POST"
let postString = "type=\(TYPE)&identifier=\(IDENTIFIER)";
request.httpBody = postString.data(using: String.Encoding.utf8);
let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)
let task = defaultSession.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if error != nil {
print("Failed to download data")
}else {
print("Data downloaded")
self.parseJSON(data!)
}
}
task.resume()
}
func deleteItems(TYPE: String, SKU: String) {
let myUrl = URL(string: "https://truthordare.hexhamnetwork.com/api/92fFDd93D/erase.php");
var request = URLRequest(url:myUrl!)
request.httpMethod = "POST"// Compose a query string
let postString = "type=\(TYPE)&sku=\(SKU)";
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if error != nil
{
print("error=\(error ?? "Empty" as! Error)")
return
}
}
task.resume()
}
func parseJSON(_ data:Data) {
var jsonResult = NSArray()
do{
jsonResult = try JSONSerialization.jsonObject(with: data, options: [.allowFragments, .mutableContainers]) as! NSArray
} catch let error as NSError {
print(error)
}
var jsonElement = NSDictionary()
let tables = NSMutableArray()
for i in 0 ..< jsonResult.count
{
jsonElement = jsonResult[i] as! NSDictionary
let table = TableModel()
//the following insures none of the JsonElement values are nil through optional binding
if let sku = jsonElement["SKU"] as? String,
let message = jsonElement["MESSAGE"] as? String
{
table.sku = sku
table.message = message
}
tables.add(table)
}
DispatchQueue.main.async(execute: { () -> Void in
self.delegate.itemsDownloaded(items: tables)
})
}
}
It crashes once it gets to
self.delegate.itemsDownloaded(items: tables)
It says that it received a nil while force unwrapping
To initialize the delegate I called this within the classes that were calling this.
messageModel.delegate = self
To avoid the crash reliably declare delegate as regular optional
weak var delegate: MessageModelProtocol?
and call it
self.delegate?.itemsDownloaded(items: tables)
In Swift 4+ it's highly recommended to use the Codable protocol and in any Swift version a completion handler rather than protocol / delegate.
And don't use NS... collection types in Swift at all. Use native types. And .mutableContainers / .allowFragments is pointless in Swift if the expected type is a collection type. Omit the parameter.

why is my json returning nil

let myUrl = URL(string: "http://app.avatejaratsaba1.com/api/Values/GetPriceList?paymentType=1&goodType=102")
var request = URLRequest(url: myUrl!)
request.httpMethod = "GET" // compose a query string
request.addValue("application/json", forHTTPHeaderField: "content-type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = URLSession.shared.dataTask(with: request)
{
(data : Data? , response : URLResponse? , error : Error?) in
self.removeActivtyIndicator(activityIndicator: MyActivityIndicator)
if error != nil
{
self.DisplayMessage(UserMessage: "2Could not successfully perform this request , please try again later.")
print("error = \(String(describing : error))")
}
// let's convert response sent from a server side code to a NSDictionary object:
do { let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json
{
I have the exact code in another viewcontroller with another url and it works properly !!! it works properly in Postman!!
and i'm coding swift
UPDATED::::
do {
if let json = try JSONSerialization.jsonObject(with: data) as? [[String:Any]] {
var goodNameArray = [String].self
for i in 0..<json.count{
guard let goodName = json[i]["goodName"] as? String else{return}
Global.GlobalVariable.GoodName = goodNameArray.append(goodName)
}
print("GoodNames: \(goodNameArray)")
}
} catch let parseError {
print("parsing error: \(parseError)")
let responseString = String(data: data, encoding: .utf8)
print("raw response: \(String(describing: responseString))")
}
}
task.resume()
and the error it returns is :
Cannot invoke 'append' with an argument list of type '(String)'
global var code:::::
class Global: UIViewController
{
struct GlobalVariable
{
static var companyName = "Company"
static var bigName = ((0) , (""))
static var names = ["Loading..." , ""]
////////////
static var AgentInfo = "agentinfo"
////////////
static var genaral_goodID = 000
static var GoodName = [String]()
static var PriceVariableName = "PriceVariableName"
static var paymentType = "paymentType"
static var fee = "fee"
static var exipreDate = "exipreDate"
static var expireTime = "expireTime"
}
}
UPDATED::::::
uitable
class secondtable : TableViewController
{
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Global.GlobalVariable.names.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let content = Global.GlobalVariable.GoodName[indexPath.row]
cell.textLabel?.text = content
//cell.accessoryType = .disclosureIndicator
return cell
}
}
here in this par of my code , i'm supposed to populate a table with "goodName"
I tested in bellow way ,it is working for me.The response in array of dictionaries. Don't do force unwrap.
func viewDidLoad(){
downloadDataFromServer { (success, goodNamesArray) in
if success{
print("goodNamesArray: \(goodNamesArray)")
print("successfully updated tableview data")
self.tableView.reloadData()
}
}
}
func downloadDataFromServer(completionHandler: #escaping(Bool,[String])->()){
guard let url = URL(string: "http://app.avatejaratsaba1.com/api/Values/GetPriceList?paymentType=1&goodType=102") else {
return
}
let request = URLRequest(url: url)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print("request failed \(String(describing: error))")
return
}
do {
if let json = try JSONSerialization.jsonObject(with: data) as? [[String:Any]] {
var goodNameArray = [String]()
for i in 0..<json.count{
guard let goodName = json[i]["goodName"] as? String else{return}
self.goodNameArray.append(goodName)
}
print("GoodNames: \(self.goodNameArray)")
Global.GlobalVariable.GoodName = goodNameArray
}
} catch let parseError {
print("parsing error: \(parseError)")
let responseString = String(data: data, encoding: .utf8)
print("raw response: \(String(describing: responseString))")
}
}
task.resume()
}
Do yourself a favour and save some time by reading up on the Codable protocol. It will allow you to generate a pretty decent JSON-parser by basically just defining your structure. Using JSONDecoder.decode will provide you with much more valuable error information if something goes wrong.
Since your API is currently only providing an empty array using the URL you provide us with it is pretty hard to come up with any meaningful code. You should resort to a simple String-representation of your JSON, at least a minimalized form that shows us all about the structure. That way your question would not depend on the workings of a probably fairly complicated web service.

Xcode func used in another View Controller

I have this func in a Swift file, and it returns the value of the data in the database and prints it out in the counsel.
I want to use the value in the other View Controller but I can't get this to work, so I hope somebody can help me.
It is the nameUser, statusUser and pointUser I like to use in other View Controller.
import Foundation
import UIKit
var code = "100"
var getStatusUSer = ""
class getJSON: NSObject, URLSessionDataDelegate
{
//properties
var data : NSMutableData = NSMutableData()
func downloadItems()
{
let url = NSMutableURLRequest(url: NSURL(string: "http://www.hholm.dk/time_app/qrcode4.php")! as URL)
url.httpMethod = "POST"
let postString = "username=\(code)"
url.httpBody = postString.data(using: String.Encoding.utf8)
print(url.httpBody = postString.data(using: String.Encoding.utf8))
var session: URLSession!
let configuration = URLSessionConfiguration.default
session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
let task = session.dataTask(with: url as URLRequest)
task.resume()
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data)
{
self.data.append(data as Data);
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
{
if error != nil
{
print("Not Found", error)
}
else
{
print("Ok")
self.parseJSON()
}
}
func parseJSON()
{
var jsonResult: NSArray = NSArray()
do
{
jsonResult = try JSONSerialization.jsonObject(with: self.data as Data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray
print("jsonResult.count",jsonResult.count)
}
catch let error as NSError
{
print("jsonResult: ", error)
}
var jsonElement: NSDictionary = NSDictionary()
var contador = 0
for i in (0..<jsonResult.count)
{
jsonElement = jsonResult[i] as! NSDictionary
if let nameUser = jsonElement["name"] as? String,
let pointUser = jsonElement["point"] as? String,
let statusUser = jsonElement["status"] as? String
{
getStatusUSer = statusUser
print("Name: ", nameUser)
print("Status: ", statusUser)
print("Point: ", pointUser)
}
}
}
}
Hi Woof this is what i have in my viewcontroler:
import UIKit
class inputcodeViewController: UIViewController {
#IBOutlet weak var input: UITextField!
#IBAction func but(_ sender: Any) {
downloadItems()
}
func downloadItems(){
let getJson = GetJSON()
//setting the delegate
getJson.delegate = self
//starting download
getJson.downloadItems()
}
}
extension inputcodeViewController: GetJSONDelegate {
func didReceiveValues(name: String, status: String, point: String){
//now you can use values in your view controller
}
}
how can i print the values
You can use protocol to return those values:
import Foundation
import UIKit
var code = "100"
var getStatusUSer = ""
//define the protocol
protocol GetJSONDelegate {
func didReceiveValues(name: String, status: String, point: String)
}
//I've changed the first char of the class name to uppercase
class GetJSON: NSObject, URLSessionDataDelegate{
//properties
var data : NSMutableData = NSMutableData()
//delegate
var delegate: GetJSONDelegate?
func downloadItems(){
let url = NSMutableURLRequest(url: NSURL(string: "http://www.hholm.dk/time_app/qrcode4.php")! as URL)
url.httpMethod = "POST"
let postString = "username=\(code)"
url.httpBody = postString.data(using: String.Encoding.utf8)
print(url.httpBody = postString.data(using: String.Encoding.utf8))
var session: URLSession!
let configuration = URLSessionConfiguration.default
session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
let task = session.dataTask(with: url as URLRequest)
task.resume()
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data)
{
self.data.append(data as Data);
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
{
if error != nil
{
print("Not Found", error)
}
else
{
print("Ok")
self.parseJSON()
}
}
func parseJSON()
{
var jsonResult: NSArray = NSArray()
do
{
jsonResult = try JSONSerialization.jsonObject(with: self.data as Data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray
print("jsonResult.count",jsonResult.count)
}
catch let error as NSError
{
print("jsonResult: ", error)
}
var jsonElement: NSDictionary = NSDictionary()
var contador = 0
for i in (0..<jsonResult.count)
{
jsonElement = jsonResult[i] as! NSDictionary
if let nameUser = jsonElement["name"] as? String,
let pointUser = jsonElement["point"] as? String,
let statusUser = jsonElement["status"] as? String
{
getStatusUSer = statusUser
print("Name: ", nameUser)
print("Status: ", statusUser)
print("Point: ", pointUser)
//here we will return received data to the delegate
self.delegate?.didReceiveValues(name: nameUser, status: statusUser, point: pointUser)
}
}
}
}
Now we need to set your controller as a delegate for that protocol:
//this is an example, you need to add the methods described in your controller where you want to use those values
class YourViewController: UIViewController{
// the method that is called by you to get values
func downloadItems(){
let getJson = GetJSON()
//setting the delegate
getJson.delegate = self
//starting download
getJson.downloadItems()
}
}
//defining protocol methods in the extension of the view controller
extension YourViewController: GetJSONDelegate {
func didReceiveValues(name: String, status: String, point: String){
//now you can use values in your view controller
}
}

Error parsing JSON in Swift2

Hello I am having an error when parsing some JSON in Swift, my error is:
'Invalid conversion from throwing function of type '(_, _, _) throws -> Void' to non-throwing function type '(NSData?, NSURLResponse?, NSError?) -> Void'
I think this is something to do with catching an error somewhere but I cannot figure out where this would go could anyone help me please? Here is my source code:
import UIKit
import CoreData
class MasterViewController: UITableViewController {
var detailViewController: DetailViewController? = nil
override func viewDidLoad() {
super.viewDidLoad()
var appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context: NSManagedObjectContext = appDel.managedObjectContext
let url = NSURL(string: "https://www.googleapis.com/blogger/v3/blogs/10861780/posts?key=AIzaSyBwmI4AzMnBmr7oSVeL0EHdzMjXV1aATnQ")
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
if error != nil {
print(error)
} else {
//print(NSString(data: data!, encoding: NSUTF8StringEncoding))
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions(rawValue: 0)) as! NSDictionary
if jsonResult.count > 0 {
if let items = jsonResult["items"] as? NSArray {
for items in items {
print(items)
if let title = items["title"] as? String {
if let content = items["content"] as? String {
var newPost: NSManagedObject = NSEntityDescription.insertNewObjectForEntityForName("Posts", inManagedObjectContext: context)
newPost.setValue(title, forKey: "title")
newPost.setValue(content, forKey: "content")
do {
try context.save()
}
}
}
}
}
}
} catch let error as NSError {
print(error)
}
var request = NSFetchRequest(entityName: "Posts")
request.returnsObjectsAsFaults = false
var results = try context.executeFetchRequest(request)
self.tableView.reloadData()
}
})
task.resume()
}
Thanks!
It looks like you haven't taken care of all the throwing functions using do-try-catch method.
According to the Swift 3 Documentation at https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html
In your case, you seem to have forgotten to take care of the error thrown at var- results. Also, you haven't handled the throwing function session.dataTaskWithURL with a do-try-catch method.
You should not be getting this error if you modify your code as following:
override func viewDidLoad() {
super.viewDidLoad()
var appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context: NSManagedObjectContext = appDel.managedObjectContext
let url = NSURL(string: "https://www.googleapis.com/blogger/v3/blogs/10861780/posts?key=AIzaSyBwmI4AzMnBmr7oSVeL0EHdzMjXV1aATnQ")
let session = NSURLSession.sharedSession()
do {
let task = try session.dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
if error != nil {
print(error)
} else {
//print(NSString(data: data!, encoding: NSUTF8StringEncoding))
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions(rawValue: 0)) as! NSDictionary
if jsonResult.count > 0 {
if let items = jsonResult["items"] as? NSArray {
for items in items {
print(items)
if let title = items["title"] as? String {
if let content = items["content"] as? String {
var newPost: NSManagedObject = NSEntityDescription.insertNewObjectForEntityForName("Posts", inManagedObjectContext: context)
newPost.setValue(title, forKey: "title")
newPost.setValue(content, forKey: "content")
do {
try context.save()
} catch let error1 as NSError { // this is for context.save
print(error1)
}
}
}
}
}
}
}
var request = NSFetchRequest(entityName: "Posts")
request.returnsObjectsAsFaults = false
do{
var results = try context.executeFetchRequest(request)
} catch let error2 as NSError { // this is for context.executeFetchRequest
print(error2)
}
self.tableView.reloadData()
}
})
task.resume()
} catch let error3 as NSError { // this is for session.dataTaskWithURL
print(error3)
}
}
Hope this helps ! Cheers !

Error in JSON downloaded data as NSDictionary in Swift

#IBOutlet weak var weather: UILabel!
#IBOutlet weak var city: UITextField!
#IBAction func button(sender: AnyObject) {
let urlpath = "api.worldweatheronline.com/free/v2/weather.ashx?q=\(city.text)&format=json&num_of_days=5&key=c7fc4c9444ae2ddcee02a0893d5f0"
let url = NSURL(string: urlpath)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
self.weather.text="error"
}else{
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
println(jsonResult)
}
})
task.resume()
}
I receive errors in the console when running the application. I am correctly serializing the data I believe but, I can not get the JSON serialized data to show up in an NSDictionary. I have tried to print the Dictionary to the console and it still just shows up as error. Please help me understand whats wrong here.
Try some thing like this.
func fetchWeatherData(latLong: String, completion: WeatherDataCompletionBlock) {
let baseUrl = NSURL(string: "http://api.worldweatheronline.com/free/v2/weather.ashx?q=\(city.text)&format=json&num_of_days=5&key=c7fc4c9444ae2ddcee02a0893d5f0")
let request = NSURLRequest(URL: baseUrl!)
println(request)
let task = session.dataTaskWithRequest(request) {[unowned self] data, response, error in
if error == nil {
var jsonError: NSError?
if (jsonError == nil) {
let weatherDictionary = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.AllowFragments, error: &jsonError) as NSDictionary
let data = WeatherData(weatherDictionary: weatherDictionary)
completion(data: data, error: nil)
} else {
completion(data: nil, error: jsonError)
}
} else {
completion(data: nil, error: error)
}
}
task.resume()
}