How To Update A Label In Swift 3 With JSON Data Inside Of A Function? - swift

For some reason whenever I try to update my label with the current temperature using self.infoLabel.text = String(temp!) inside of the DispatchQueue code block, I get the following fatal error message:
unexpectedly found nil while unwrapping an Optional value.
I'd appreciate if someone could help me figure out why the code below isn't working. Thanks.
func getCurrentTemp(city: String){
let weatherRequestURL = URL(string: "\(openWeatherMapBaseURL)?APPID=\(openWeatherMapAPIKey)&q=\(city)")!
// The data task retrieves the data.
URLSession.shared.dataTask(with: weatherRequestURL) { (data, response, error) in
if let error = error {
// Case 1: Error
print("Error:\n\(error)")
}
else {
//print("Raw data:\n\(data!)\n")
//let dataString = String(data: data!, encoding: String.Encoding.utf8)
//print("Human-readable data:\n\(dataString!)")
do {
// Try to convert that data into a Swift dictionary
let weather = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String:AnyObject]
if let main = weather["main"] as? [String: Any] {
let temp = main["temp"] as? Double
print("temp\(temp!)")
DispatchQueue.main.sync(execute: {
self.infoLabel.text = String(temp!)
})
//return temp as? String
//let temp_max = main["temp_max"] as? Double
//print("temp\(temp_max!)")
//let temp_min = main["temp_min"] as? Double
//print("temp\(temp_min!)")
}
}
catch let jsonError as NSError {
// An error occurred while trying to convert the data into a Swift dictionary.
print("JSON error description: \(jsonError.description)")
}
}
}
.resume()
}

There are two possibilities here: 1) either temp is nil (and it shouldn't be because you already force unwrap it in the print statement above) 2) or infoLabel is nil which happens if you broke your outlet connection.
Its easy to check; make a breakpoint above your assignment and in the debug console you can type:
po self.infoLabel
to see if its nil. For good measure you an also check temp.
You can also add a print statement to check self.infoLabel or an assert.

Alright, so I found a makeshift solution to this issue (See Below). Rather than placing the code inside of the function I made, I placed it in the viewDidLoad() function. For whatever reason, self.infoLabel? would be nil anywhere inside of the function I made.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
print("Sucessful launched weather page.")
let weatherRequestURL = URL(string: "\(openWeatherMapBaseURL)?APPID=\(openWeatherMapAPIKey)&q=\(city)")!
// The data task retrieves the data.
URLSession.shared.dataTask(with: weatherRequestURL) { (data, response, error) in
if let error = error {
// Case 1: Error
print("Error:\n\(error)")
}
else {
//print("Raw data:\n\(data!)\n")
//let dataString = String(data: data!, encoding: String.Encoding.utf8)
//print("Human-readable data:\n\(dataString!)")
do {
// Try to convert that data into a Swift dictionary
let weather = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String:AnyObject]
if let main = weather["main"] as? [String: Any] {
let temp = main["temp"] as? Double
print("temp\(temp!)")
var tempInFarenheit = ((9/5)*((temp!)-273) + 32).rounded()
DispatchQueue.main.sync(execute: {
self.infoLabel.text = "\(tempInFarenheit) + °"
})
}
}
catch let jsonError as NSError {
// An error occurred while trying to convert the data into a Swift dictionary.
print("JSON error description: \(jsonError.description)")
}
}
}
.resume()
}
Although this isn't the most effective way of doing things, hopefully it can help others who are having the same problem. If I find a more effective way of doing this, I'll be sure to edit this post and include it.

Related

How to add or delete rule of safari content block list at run time

I am using in my project safari content blocker extension. when i set the rule in blockerList.json file statically and run the project every thing is working fine. Now i want to set my rule dynamically using the technic as it describes in below.
Guys please help me out to set the rule dynamically at run time.
I try this but i am getting an error when
load from viewcontroller class
fileprivate func saveRuleFile() {
let ruleList = [["trigger":["url-filter": ".*"],"action":["type": "block"]]]
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
if let encoded = try? encoder.encode(ruleList) {
let sharedContainerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.****.***")
print("sharedContainerURL = \(String(describing: sharedContainerURL))")
if let json = String(data: encoded, encoding: .utf8) {
print(json)
}
if let destinationURL = sharedContainerURL?.appendingPathComponent("Rules.json") {
do {
try encoded.write(to: destinationURL)
} catch {
print (error)
}
}
}
}
And write this in ContentBlockerRequestHandler class
func beginRequest(with context: NSExtensionContext) {
let sharedContainerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.****.***")
let sourceURL = sharedContainerURL?.appendingPathComponent("Rules.json")
let ruleAttachment = NSItemProvider(contentsOf: sourceURL)
let item = NSExtensionItem()
item.attachments = ([ruleAttachment] as! [NSItemProvider])
context.completeRequest(returningItems: [item], completionHandler: nil)
}
i try to load using
SFContentBlockerManager.reloadContentBlocker(withIdentifier: "com.app.*****", completionHandler: {(error) in
if error != nil{
print("error: \(error.debugDescription)")
}
})
when try to execute 3rd number block at run time i'm getting an error. But i go to the file path and checked the json is absolutely fine, its a valid json there.
Error Domain=WKErrorDomain Code=2 "(null)" UserInfo={NSHelpAnchor=Rule list compilation failed: Failed to parse the JSON String.}
Try to use JSONSerialization. It work great for me :)
fileprivate func saveRuleFile() {
let ruleList = [["trigger":["url-filter": ".*"],"action":["type": "block"]]]
let jsonAsData = try! JSONSerialization.data(withJSONObject: ruleList)
let sharedContainerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.****.***")
print("sharedContainerURL = \(String(describing: sharedContainerURL))")
if let destinationURL = sharedContainerURL?.appendingPathComponent("Rules.json") {
do {
try jsonAsData.write(to: destinationURL)
} catch {
print (error)
}
}
}

Use of unresolved identifier 'self' in Swift 5

I wanted to write a weather app using OpenWeatherMap Api. Everything looks fine for me. The app was written using an tutorial on the web and everything looks 1:1.
let weatherURL = URL(string: "http://api.openweathermap.org/data/2.5/weather?q=Warsaw,pl?&units=imperial&APPID=cb9325925ed023b38a64c3d8da3c345c")!
let dataTask = session.dataTask(with: weatherURL) {
(data: Data?, response: URLResponse?, error: Error?) in
if let error = error {
print("Error:\n\(error)")
} else {
if let data = data {
let dataString = String(data: data, encoding: String.Encoding.utf8)
print("All the weather data:\n\(dataString!)")
if let jsonObj = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary {
if let mainDictionary = jsonObj.value(forKey: "main") as? NSDictionary {
if let temperature = mainDictionary.value(forKey: "temp") {
DispatchQueue.main.async {
self.weatherLabel.text = "Temperatura w Warszawie: \(temperature)°C"
}
}
} else {
print("Error: unable to find temperature in dictionary")
}
} else {
print("Error: unable to convert json data")
}
} else {
print("Error: did not receive data")
}
}
}```
The problem is that your code
let weatherURL = ...
let dataTask = ...
...needs to be inside some method of some struct or class. If you look at the original tutorial, you'll see that that's the case. Your code is just hanging out loose in the open, in a place where executable code is not permitted.

Refer to variable from do block in catch

I need to access a variable inside a do statement. Will it behave like the if-else statement in the sense that you con't use variables outside of the if statement?
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func loadView() {
do {
//let url = URL?("https://www.hackingwithswift.com")
let TECIONEXContent = try String(contentsOf: URL("https://www.hackingwithswift.com"))
} catch { print("error")}
//I need to access TECIONEXContent variable outside the do statement
// Error: Use of unresolved identifier 'TECIONEXContent'
var TECGrid = TECIONEXContent.components(separatedBy: "\n")
}
}
The error is on the last line, 'unresolved identifier'.
Will it behave like the if-else statement in the sense that you con't use variables outside of the if statement?
Yes. But just like an if-else statement, you can define the variable before the do-catch:
E.g. in an if-else statement:
let foo: String
if bar > 1 {
foo = "bigger than one"
} else {
foo = "one or smaller"
}
Or, in your case:
let url = URL(string: "https://www.hackingwithswift.com")!
let contents: String
do {
contents = try String(contentsOf: url)
} catch {
print(error)
return
}
let grid = contents.components(separatedBy: "\n")
Or, you aren’t really doing anything with the error message, you can eliminate the do-catch altogether:
guard let contents = try? String(contentsOf: url) else {
print("error")
return
}
let grid = contents.components(separatedBy: "\n")
Frankly, all of that having been said, using String(contentsOf:) is probably not the best pattern, anyway, because that performs a synchronous network request, which risks having the OS “watchdog” process kill your app unceremoniously if the main thread is blocked; and even if that doesn’t happen, it’s not a good user experience to freeze the app while the network request is in progress. Usually we’d use URLSession:
let url = URL(string: "https://www.hackingwithswift.com")!
URLSession.shared.dataTask(with: url) { data, response, error in
guard
let data = data,
let httpResponse = response as? HTTPURLResponse,
let string = String(data: data, encoding: .utf8) else {
print(error ?? "Unknown error")
return
}
guard 200 ..< 300 ~= httpResponse.statusCode else {
print("Expected 2xx response, but got \(httpResponse.statusCode)")
return
}
let grid = string.components(separatedBy: "\n")
DispatchQueue.main.async {
// use `grid` here
}
}.resume()
Unrelated, but:
The convention is to start variable names with lowercase letters.
You implemented loadView. It’s rare that we do that, and instead we implement viewDidLoad, making sure to call super.viewDidLoad(), too.
If you’re doing this in a playground, you’d obviously also set needsIndefiniteExecution, if you haven’t already.
Thus:
import UIKit
import PlaygroundSupport
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
performRequest()
}
func performRequest() {
let url = URL(string: "https://www.hackingwithswift.com")!
URLSession.shared.dataTask(with: url) { data, response, error in
guard
let data = data,
let httpResponse = response as? HTTPURLResponse,
let string = String(data: data, encoding: .utf8) else {
print(error ?? "Unknown error")
return
}
guard 200 ..< 300 ~= httpResponse.statusCode else {
print("Expected 2xx response, but got \(httpResponse.statusCode)")
return
}
let grid = string.components(separatedBy: "\n")
DispatchQueue.main.async {
print(grid)
// use `grid` here
}
}.resume()
}
}
PlaygroundPage.current.liveView = ViewController()
PlaygroundPage.current.needsIndefiniteExecution = true
In your code are four(!) very bad practices.
Never load data synchronously with API like String(contentsOf from a remote URL. Use asynchronous API like URLSession.
Never print a meaningless literal string in a catch block. Print the error instance.
According to the naming convention variable names should be lowerCamelCased.
In a do - catch block put always all good code in the do scope. This solves your issue.
do {
let tecionexContent = try String(contentsOf: URL("https://www.hackingwithswift.com")!)
let tecGrid = tecionexContent.components(separatedBy: "\n")
} catch { print(error) }
recommended
URLSession.shared.dataTask(with: URL("https://www.hackingwithswift.com")!) { data, _ , error in
if let error = error { print(error); return }
let tecionexContent = String(data: data!, encoding: .utf8)!
let tecGrid = tecionexContent.components(separatedBy: "\n")
}.resume()
The Issue
The problem with your code is that you are defining the variable inside a block/closure, which is a local scope. Your variable should be at a scope where both the blocks are able to see it. This means that in the catch block, the variable does not exist. Specifically you had attempted to reference the string variable you called TECIONEXContent from outside of the closure in which it was instantiated.
On some general style points: please stick with the swift naming convention for variables (i.e. camel case) whereas your classes etc should be capitalized. (For the purposes of running the below code in the playground I've used an arbitrary function name, but you could employ it from your lifecycle methods).
Basic Playground Demo Code
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
private var tecGrid: [String]? {
didSet {
// Update some UI Here (insuring your on the main thread)
print(self.tecGrid)
}
}
func test() {
do {
var texionicContent = try String(contentsOf:URL(string: "https://www.hackingwithswift.com")!)
tecGrid = texionicContent.components(separatedBy: "\n")
}
catch let error {
print("Catch the error")
}
}
}
let play = MyViewController(nibName: nil, bundle: nil)
play.test()

Swift: Setting the text of a label in a URLSessionTask

So I am downloading a JSON file using a URLRequest().
I parse through it in order to get a specific string and I want to set the text of a label I have in my ViewController to that specific string.
I use a CompletionHandler in order to retrieve the function that gets the JSON file from another Swift file.
Here is the code of calling the function and setting the label:
class SecondViewController: UIViewController {
tr = TransportServices()
tr.getLyftData(origin: originstring, destination: destinationstring){ json in
//Parsing JSON in order to get specific data
self.lyftlabel.text = stringexample
}
}
and here is the code of getting the JSON
func getLyftData(origin: String, destination: String, completionHandler: #escaping ([String: Any]) -> ()){
let urlrequest = URLRequest(url: URL(string: urlstring)!)
let config = URLSessionConfiguration.default
let sessions = URLSession(configuration: config)
let task = sessions.dataTask(with: urlrequest) {(data, response, error) in
guard error == nil else {
print(error!)
return
}
guard let responseData = data else {
print("error, did not receive data")
return
}
do {
if let json = try JSONSerialization.jsonObject(with: responseData, options: []) as? [String: Any]{
completionHandler(json)
}
}
catch {
print("Error with URL Request")
}
}
task.resume()
}
This does the job, but in a very slow manner. I know that there is a runtime issue because UILabel.text must be set from main thread only, but I don't know any other way to fix it. Please help.
If you want to set label text in main thread use this:
DispatchQueue.main.async {
self.lyftlabel.text = stringexample
}

swift uploadTaskWithRequest with didReceiveData

I am pretty new to swift i have the following code
var data : AnyObject
let dict = jsonObject as NSDictionary
do
{
data = try NSJSONSerialization.dataWithJSONObject(dict, options:.PrettyPrinted)
let strData = NSString(data: data as! NSData, encoding: NSUTF8StringEncoding)! as String
data = strData.dataUsingEncoding(NSUTF8StringEncoding)!
let task = defaultSession.uploadTaskWithRequest(request, fromData: data as? NSData,
completionHandler:
{(data,response,error) in
guard let _:NSData = data, let _:NSURLResponse = response where error == nil
else {
return
}
});
task.resume()
}catch{
return resultJson
}
the resultJson object returns a empty array as there are more date to be downloaded and it takes time.I am wondering weather i can use didReceiveData option to return data after it is downloaded. I searched for code online but couldn't find any. Any help with code is much appreciated.
Thanks
You're right in that the return will run before the upload is finished, and therefore you won't get the desired result. I assume this code is in a function. You need to change it to take a closure as a parameter. Then when the upload is finished, you call that closure. Something like:
func doTheUpload(completion completionHandler: ((AnyObject?) -> Void)) {
var data : AnyObject
let dict = jsonObject as NSDictionary
do
{
data = try NSJSONSerialization.dataWithJSONObject(dict, options:.PrettyPrinted)
let strData = NSString(data: data as! NSData, encoding: NSUTF8StringEncoding)! as String
data = strData.dataUsingEncoding(NSUTF8StringEncoding)!
let task = defaultSession.uploadTaskWithRequest(request, fromData: data as? NSData,
completionHandler:
{(data,response,error) in
guard let _:NSData = data, let _:NSURLResponse = response where error == nil
completionhandler(resultJson)
else {
return
}
});
task.resume()
}catch{
// Do something for error
}
}
Then you would call it as:
doTheUpload(completion: {
resultJson in
// use the result
})
Note that I don't know where you're getting resultJson from or what type it is, so you will have to make some changes.