All, I'm very new to swift, and am running into an issue loading an image from a json payload and displaying the results in a table view cell.
This is the error I'm receiving. My labels load fine, there is even space allocated for the image container based on the constraints setup in the story board.
The error is telling me that something isn't configured correctly. I'm looking for some assitance interperting the error and what I might try to do in order to isolate the issue and get the images to display.
Console Output:
2020-07-11 16:23:01.467179-0400 MJM Homes[4806:1654145] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x280389180 UIImageView:0x14310f910.width == 1.87898*UIImageView:0x14310f910.height (active)>",
"<NSLayoutConstraint:0x280388eb0 UIImageView:0x14310f910.leading == UITableViewCellContentView:0x14310ecc0.leadingMargin + 24 (active)>",
"<NSLayoutConstraint:0x280388dc0 UIImageView:0x14310f910.top == UITableViewCellContentView:0x14310ecc0.topMargin + 15 (active)>",
"<NSLayoutConstraint:0x280388d70 UITableViewCellContentView:0x14310ecc0.bottomMargin == UIImageView:0x14310f910.bottom + 54 (active)>",
"<NSLayoutConstraint:0x280388d20 UIImageView:0x14310f910.centerX == UITableViewCellContentView:0x14310ecc0.centerX (active)>",
"<NSLayoutConstraint:0x280388a50 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x2819b42a0'UIViewLayoutMarginsGuide']-(11)-| (active, names: '|':UITableViewCellContentView:0x14310ecc0 )>",
"<NSLayoutConstraint:0x280388820 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x14310ecc0.height == 248.5 (active)>",
"<NSLayoutConstraint:0x2803888c0 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x14310ecc0.width == 375 (active)>",
"<NSLayoutConstraint:0x280388aa0 'UIView-leftMargin-guide-constraint' H:|-(16)-[UILayoutGuide:0x2819b42a0'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':UITableViewCellContentView:0x14310ecc0 )>",
"<NSLayoutConstraint:0x280388af0 'UIView-topMargin-guide-constraint' V:|-(11)-[UILayoutGuide:0x2819b42a0'UIViewLayoutMarginsGuide'] (active, names: '|':UITableViewCellContentView:0x14310ecc0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x280389180 UIImageView:0x14310f910.width == 1.87898*UIImageView:0x14310f910.height (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-07-11 16:23:01.475387-0400 MJM Homes[4806:1654145] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x2803884b0 UIImageView:0x143111b60.width == 1.87898*UIImageView:0x143111b60.height (active)>",
"<NSLayoutConstraint:0x280388370 UIImageView:0x143111b60.leading == UITableViewCellContentView:0x1431119c0.leadingMargin + 24 (active)>",
"<NSLayoutConstraint:0x2803882d0 UIImageView:0x143111b60.top == UITableViewCellContentView:0x1431119c0.topMargin + 15 (active)>",
"<NSLayoutConstraint:0x280388280 UITableViewCellContentView:0x1431119c0.bottomMargin == UIImageView:0x143111b60.bottom + 54 (active)>",
"<NSLayoutConstraint:0x280388230 UIImageView:0x143111b60.centerX == UITableViewCellContentView:0x1431119c0.centerX (active)>",
"<NSLayoutConstraint:0x28038bcf0 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x2819b41c0'UIViewLayoutMarginsGuide']-(11)-| (active, names: '|':UITableViewCellContentView:0x1431119c0 )>",
"<NSLayoutConstraint:0x2803a7840 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x1431119c0.height == 248.5 (active)>",
"<NSLayoutConstraint:0x2803a75c0 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x1431119c0.width == 375 (active)>",
"<NSLayoutConstraint:0x280388000 'UIView-leftMargin-guide-constraint' H:|-(16)-[UILayoutGuide:0x2819b41c0'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':UITableViewCellContentView:0x1431119c0 )>",
"<NSLayoutConstraint:0x280388050 'UIView-topMargin-guide-constraint' V:|-(11)-[UILayoutGuide:0x2819b41c0'UIViewLayoutMarginsGuide'] (active, names: '|':UITableViewCellContentView:0x1431119c0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x2803884b0 UIImageView:0x143111b60.width == 1.87898*UIImageView:0x143111b60.height (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
This is the code I created to handle the Table View Cell:
//
// ModelTableView.swift
import Foundation
import UIKit
import Alamofire
import SwiftKeychainWrapper
import SwiftyJSON
class ModelTableView: UITableViewController{
let defaults = UserDefaults.standard
lazy var url = URL(string:"http://serverIP/wp-json/wp/v2/posts?userid="+self.defaults.string(forKey: "username")! )
var models : JSON = [:]
override func viewDidLoad() {
super.viewDidLoad()
guard let username = defaults.string(forKey: "username") else { return }
let authToken: String? = KeychainWrapper.standard.string(forKey: "authToken")
print("table view controller activated")
print(username)
print(authToken)
print("BEFORE DOWNLOAD")
print(models)
newDownloadJSON()
print("AFTER DOWNLOAD")
print(models)
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}
func newDownloadJSON(){
let url: String = "http://serverIP/wp-json/wp/v2/posts?userid="+self.defaults.string(forKey: "username")!
var request = URLRequest(url: NSURL(string: url)! as URL)
let token: String? = KeychainWrapper.standard.string(forKey: "authToken")
// Your request method type Get or post etc according to your requirement
request.httpMethod = "GET"
request.setValue("Bearer "+token!, forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
Alamofire.request(request).responseJSON { response in
print("Downloaded Data")
// Your required functionality here
let modelJSON = JSON(response.result.value)
print("MODELSE AVAILABLE")
print(modelJSON.count)
print("BEFORE SETTING VALUE FUNCTION")
print(self.models)
self.models = modelJSON;
print(modelJSON[0]["better_featured_image"]["media_details"]["sizes"]["large"]["source_url"])
print(modelJSON[0]["title"]["rendered"]);
print("AFTER DOWNLOAD INSIDE ALAMO FUNCTION")
print(self.models)
DispatchQueue.main.async{
self.tableView.reloadData()
}
}
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("PRINTING NUMBER OF VALUES IN TABLE")
print(models.count)
return models.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier:"ModelCell") as? ModelCell else { return UITableViewCell() }
cell.modName.text = self.models[indexPath.row]["title"]["rendered"].stringValue
if let imageURL = URL(string: "http://serverIP/wp-content/uploads" + self.models[indexPath.row]["better_featured_image"]["media_details"]["sizes"]["large"]["source_url"].stringValue){
DispatchQueue.global().async{
let data = try? Data(contentsOf: imageURL)
if let data = data{
let image = UIImage(data: data)
DispatchQueue.main.async{
cell.modImg.image = image
}
}
}
}
return cell
}
}
Code above was fine, noob that had an improper path reference.