func loadImageFromUrl() {
guard let urlString = self.urlString else {
return
}
let url = URL(string: urlString)!
let task = URLSession.shared.dataTask(with: url, completionHandler: self.getImageFromResponse(data:response:error:))
task.resume()
}
I keep on getting this fatal error (mentioned in title) for let url = URL(string: urlString)!, even if I have tried to guard it in previous lines. Also, when I move my cursor over urlString, it indeed shows a highlight of my intended url from the API.
url could still be nil, if urlString doesn't represent a valid URL. You haven't guarded against that; you've only guarded against urlString not being nil.
You need to guard against both possible outcomes:
func loadImageFromUrl() {
guard let url = URL(string: self.urlString) else {
print("Invalid url string: \(self.urlString)")
return
}
let task = URLSession.shared.dataTask(with: url, ....
// rest of your code
}
Of course, that doesn't solve the underlying problem that urlString is indeed broken. You need to see what's going on there.
Since you're visually seeing that it's correct, it's possible that it contains some whitespace at the beginning or the end. If it's not a urlString that you control, you can trim it:
let trimmedUrlStr = self.urlString.trimmingCharacters(in: .whitespacesAndNewlines)
This should work
if let url = URL(string: urlString) {
let task = URLSession.shared.dataTask(with: url,
completionHandler: self.getImageFromResponse(data:response:error:))
task.resume()
}
Related
In FirstViewController i'm fetching the response from JSON and want to pass that fetched response to another view controller.Below is the code which i have used so far for parsing and passing the response.
FirstViewController
var fn:String! //globally declared variable
code i have tried for parsing in FirstViewController
do {
let detailsDictionary:NSDictionary = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! Dictionary<String, AnyObject> as NSDictionary
print(detailsDictionary)
let details = detailsDictionary["Data"] as! [[String:AnyObject]]
print(details)
for dtl in details
{
self.fn = dtl["Father_Name"] as? String ?? "NA"
print(self.fn) //here i'm getting the exact value from JSON
}
}
}
SecondViewController
In SecondViewController there is a Label called profile_name and want to set that parsed string(fn) as Label's text. for that i declared another variable as global.
var pname:String!
below is the code i have used to fetch the value from FirstViewController.
viewDidLoad()
{
let othervc = FirstViewController()
self.pname = othervc.fn
self.profile_name.text = self.pname
}
Problem : I tried my best efforts to get the desired output but i'm getting nil response.
Please Help.
In Second ViewController
let strName:String!
In First ViewController
let strOne = "This is for testing"
let objstory = self.storyboard?.instantiateViewController(withIdentifier: "yout Secoond ViewController Storybord ID") as! YourSecondViewControllerName
objstory.strNam = strOne
self.navigationController?.pushViewController(objstory, animated: true)
Your updated code just won't work.
let othervc = FirstViewController()
creates a new instance of FirstViewController (not the one that got the JSON).
You should be handling it something like this:
In FirstViewController
let fn = dtl["Father_Name"] as? String ?? "NA"
let svc = SecondViewController() // Or maybe instantiate from Storyboard, or maybe you already have a reference to it
svc.pname = fn
present(svc, animated: true, completion: nil)
Then in SecondViewController
override func viewDidLoad() {
super.viewDidLoad()
profile_name.text = pname
}
I'd suggest you take some time out and re-read Apple's View Controller programming guide.
Original Answer
The problem you have here…
vcvalue.profile_name.text = fn
is that profile_name is nil as the view for the view controller hasn't been loaded at this point.
You should handle this by creating a property in LeftSideMenuViewController
var name: String?
Then set
vcvalue.name = fn
And then in LeftSideMenuViewController
override func viewDidLoad() {
super.viewDidLoad()
profile_name.text = name
}
Also, some basic tips…
Don't force unwrap (!) apart from IBOutlets. You may have to write a bit more code, but you will reduce crashes.
Make #IBOutlets private - this will prevent you accidentally assigning to them as you are now
If you're overriding any viewWill/DidDis/Appear methods, you must call super at some point.
You need to re-read the section on switch/case
So this…
let a = indexPath.row
switch(a)
{
case 0 :
if(a == 0)
{
return 45
}
break
etc
could just be…
switch indexPath.row {
case 0...4:
return 45
case 5:
return 50
default:
break
}
I have an endpoint which takes in a phone number and sends a code to the number, but also returns that same message to the data section of the session that called it.
All of that works, but the problem I'm having is that, after the session makes the call, I'm segueing to the next screen and i'm passing that code into the next controller. But i think the api is responding too slow, so by time the segue (and prep for segue) has happened the code has not been returned yet. How can i fix this?
let scriptURL = "https://---------------/api/verify/sms?"
let urlWithParams = scriptURL + "number=\(phone.text!)"
let myUrl = NSURL(string: urlWithParams)
let request = NSMutableURLRequest(url: myUrl! as URL)
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
//print(error?.localizedDescription)
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as AnyObject
self.currentCode = json["code"]!! as! String //-> This is the code the is returned from the api call
}catch{
print("error with serializing JSON: \(error)")
}
}
task.resume()
self.performSegue(withIdentifier: "toVerifyCode", sender: (Any?).self)
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "toVerifyCode"{
let newController = segue.destination as! verifyCodeController
newController.code = self.currentCode
}
}
The problem is that you placed self.performSegue(withIdentifier: "toVerifyCode", sender: (Any?).self) not in the closure.
So, you have to place it like this:
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
//print(error?.localizedDescription)
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as AnyObject
//on main thread
DispatchQueue.main.async {
self.currentCode = json["code"]!! as! String //-> This is the code the is returned from the api call
self.performSegue(withIdentifier: "toVerifyCode", sender: (Any?).self)
}
}catch{
print("error with serializing JSON: \(error)")
}
}
task.resume()
Also, please note that your closure is executed asynchronously, so I wrapped the call to be executed on main thread by using GCD.
I have a downloader class that downloads a file based on a given URL which then calls a completion passing it the contents of the file as NSData.
For the project that I'm using this in, the URL will be a JPEG image. The downloader works perfectly; I can use the result into NSImage and show it in a Image View Controller.
I would like to be able to save that NSData object to file.
After quite some time researching the internet on Google, StackOverflow, etc. and trying many suggestions, I cannot get the file to save.
Here is a playground of the Downloader class and my attempt to save the file:
//: Playground - noun: a place where people can play
import Cocoa
class NetworkService
{
lazy var configuration: URLSessionConfiguration = URLSessionConfiguration.default
lazy var session: URLSession = URLSession(configuration: self.configuration)
let url: NSURL
init(url: NSURL)
{
self.url = url
}
func downloadImage(completion: #escaping ((NSData) -> Void))
{
let request = NSURLRequest(url: self.url as URL)
let dataTask = session.dataTask(with: request as URLRequest) { (data, response, error) in
if error == nil {
if let httpResponse = response as? HTTPURLResponse {
switch (httpResponse.statusCode) {
case 200:
if let data = data {
completion(data as NSData)
}
default:
print(httpResponse.statusCode)
}
}
} else {
print("Error download data: \(error?.localizedDescription)")
}
}
dataTask.resume()
}
}
let IMAGE_URL = NSURL(string: "https://www.bing.com/az/hprichbg/rb/RossFountain_EN-AU11490955168_1920x1080.jpg")
let networkService = NetworkService(url: IMAGE_URL!)
networkService.downloadImage(completion: { (data) in
data.write(to: URL(string: "file://~/Pictures/image.jpg")!, atomically: false)
})
The playground console show nothing at all. Can anyone spot why its not working?
NOTE: The target is macOS, not iOS. Also, I'm a swift noob...
I did try this:
networkService.downloadImage(completion: { (imageData) in
let imageAsNSImage = NSImage(data: imageData as Data)
if let bits = imageAsNSImage?.representations.first as? NSBitmapImageRep {
let outputData = bits.representation(using: .JPEG, properties: [:])
do {
try outputData?.write(to: URL(string: "file://~/Pictures/myImage.jpg")!)
} catch {
print("ERROR!")
}
}
})
It could be a permission issue. You may try:
let picturesDirectory = FileManager.default.urls(for: .picturesDirectory, in: .userDomainMask)[0]
let imageUrl = picturesDirectory.appendingPathComponent("image.jpg", isDirectory: false)
try? data.write(to: imageUrl)
It does work for me:
Checking the documentation and the migration guide, I should be able to set a new image using this code:
imageView.kf.setImage(with:url ...)
but actually I cannot find this method in the library, I only see:
imageView.kf.setImage(with:Resource... )
I don't know exactly how this resource shoud work though since I cannot find anything in the documentation.
Resource is a protocol. URL has been extended to conform to this protocol. So you can do:
let url = URL(string: ...)!
imageView.kf.setImage(with: url)
If you want some control over what Kingfisher uses for the key in its cache, you can use ImageResource:
let identifier = "..."
let url = URL(string: "http://example.com/images/identifier=\(identifier)")!
let resource = ImageResource(downloadURL: url, cacheKey: identifier)
imageView.kf.setImage(with: resource)
For Swift 4.2
import Kingfisher
extension UIImageView {
func setImage(with urlString: String){
guard let url = URL.init(string: urlString) else {
return
}
let resource = ImageResource(downloadURL: url, cacheKey: urlString)
var kf = self.kf
kf.indicatorType = .activity
self.kf.setImage(with: resource)
}
}
How to use
self.imgVw.setImage(with: your image url)
I fixed that issue using this:
PhotoHelper.shared.imagePickedBlock = { [weak self] (image, url) in
self?.imageView.kf.setImage(with: url, placeholder: image, options: nil, progressBlock: nil, completionHandler: { imageResult, error, type, cache in
self?.imageView.image = image
})
}
PhotoHelper is wrapper on native Image Picker:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
currentVC.dismiss(animated: true, completion: {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
let url = info[UIImagePickerControllerReferenceURL] as! URL
self.imagePickedBlock?(image, url)
}
})
}
I am working from an old tutorial swift2, Alamofire 3, but I am using swift 3, Alamofire 4.
I have changed most things successfully, but I am running into a problem.
with this code area.
let url = NSURL(string: _pokemonUrl)!
Alamofire.request(url).responseJSON { response in
let result = response.result
I am getting an error that says:
Argument type NSURL does not conform to expected type
URLRequestConvertible.
it does give me the option of adding in as! URLRequestConvertible after the (url) but it crashes again after compile and when i press the button to get the info. it gives an error of:
Could not cast value of type 'NSURL' (0x117e99338) to
'Alamofire.URLRequestConvertible' (0x1189ab120).
if I change NSURL to Url then it moves forward in the code but when it gets to the print statement it crashes and gives the error:
fatal error: unexpectedly found nil while unwrapping an Optional value
here is that code below.
let url = URL(string: _pokemonUrl)!
Alamofire.request(url).responseJSON { response in
let result = response.result
if let dict = result.value as? Dictionary<String, AnyObject> {
if let weight = dict["weight"] as? String {
self._weight = weight
}
if let height = dict["height"] as? String {
self._height = height
}
if let baseAttack = dict["attack"] as? Int {
self._baseAttack = "\(baseAttack)"
}
if let defense = dict["defense"] as? Int {
self._defense = "\(defense)"
}
print(self._weight)
print(self._height)
print(self._baseAttack)
print(self._defense)
I have tried to change all to Int but i get the same error.
Can anyone shed any light on this for me.
if it helps I put a break point in after print("Here") in the next code and it gives me the following error.
let url = URL(string: _pokemonUrl)!
Alamofire.request(url).responseJSON { response in
let result = response.result
print(result.value.debugDescription)
print("Here")
error comes up:
Optional({
"error_message" = "Sorry, this request could not be processed. Please try again later.";
})
Here
Thanks in advance,
Tony
You almost did it guys, just a cast to URL was missing.
let nsurl = NSURL(string: _pokemonUrl)!
let request = URLRequest(url: nsurl as URL)
Alamofire.request(request).responseJSON { response in
let result = response.result
...
}
I tried folowing & it's helped
func downloadPokemonDetails(completed: DownloadComplete) {
let url = URL(string: _pokemonUrl)!
Alamofire.request(URLRequest(url: url)).responseJSON { response in
if let result = response.result.value as? [String: Any]{
print(result.debugDescription)
}
}
}