Ich habe zwei Aufrufe, die an Web-Services, die ich versuche, in einem Etikett auf einer sekundären Ansicht angezeigt werden sollen.Datensuche von Web-Service wird nicht korrekt in Label angezeigt
Erste-Lookup oder Anruf erfolgt während der Last des Haupt-View-Controller wie folgt:
override func viewDidLoad() {
super.viewDidLoad()
getAddress()
}
func getAddress() {
let URL_GET_ADDR:String = "http://www.webserver.com/apps/getmailingaddr.php"
let requestURL = NSURL(string: URL_GET_ADDR)
let request = NSMutableURLRequest(url: requestURL! as URL)
request.httpMethod = "POST"
let task = URLSession.shared.dataTask(with: request as URLRequest){
data, response, error in
//exiting if there is some error
if error != nil{
print("error is \(error)")
return;
}
//parsing the response
do {
//converting resonse to NSDictionary
//var teamJSON: NSDictionary!
self.teamJSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
//getting the JSON array teams from the response
let address: NSArray = self.teamJSON["mailing"] as! NSArray
//looping through all the json objects in the array teams
for i in 0 ..< address.count{
//getting the data at each index
let addrName:String = (address[i] as! NSDictionary)["name"] as! String!
let addrAddr1:String = (address[i] as! NSDictionary) ["address1"] as! String!
let addrAddr2:String = (address[i] as! NSDictionary)["address2"] as! String!
let addrCity:String = (address[i] as! NSDictionary)["city"] as! String!
let addrState:String = (address[i] as! NSDictionary)["state"] as! String!
let addrZipcode:Int = (address[i] as! NSDictionary)["zipcode"] as! Int!
}
} catch {
print(error)
}
}
task.resume()
}
ich diese Daten an die Kontakte mit den folgenden Modus Controller bin vorbei:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let ContactsViewController = segue.destination as? ContactsViewController {
ContactsViewController.myAddress = teamJSON
}
Alles ist gut soweit. Jetzt in der Kontakte-View-Controller ich einen Nachschlag der Kontakte an Durchführung mit dem folgenden:
override func viewDidLoad() {
super.viewDidLoad()
myLabel = ""
// Do any additional setup after loading the view, typically from a nib.
contactsLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
//created NSURL
let requestURL = NSURL(string: URL_GET_TEAMS)
//creating NSMutableURLRequest
let request = NSMutableURLRequest(url: requestURL! as URL)
//setting the method to post
request.httpMethod = "POST"
//creating a task to send the post request
let task = URLSession.shared.dataTask(with: request as URLRequest){
data, response, error in
//exiting if there is some error
if error != nil{
print("error is \(error)")
return;
}
//parsing the response
do {
//converting resonse to NSDictionary
var teamJSON: NSDictionary!
teamJSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
//getting the JSON array teams from the response
let teams: NSArray = teamJSON["contacts"] as! NSArray
//looping through all the json objects in the array teams
for i in 0 ..< teams.count{
//getting the data at each index
let teamId:String = (teams[i] as! NSDictionary)["title"] as! String!
let teamName:String = (teams[i] as! NSDictionary) ["name"] as! String!
//displaying the data
//print("id -> ", teamId)
//print("name -> ", teamName)
//print("===================")
//print("")
self.myLabel = self.myLabel + "<font size='5'><b>" + teamId + "</font>:</b> " + "<font size='5'>" + teamName + "</font><br /><br />"
}
} catch {
print(error)
}
DispatchQueue.main.async {
let attrStr = try! NSAttributedString(data: self.myLabel.data(using: String.Encoding.unicode,allowLossyConversion: true)!, options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
self.contactsLabel.attributedText = attrStr
self.contactsLabel.sizeToFit()
}
}
//executing the task
task.resume()
let attrStr = try! NSAttributedString(data: myLabel.data(using: String.Encoding.unicode,allowLossyConversion: true)!, options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
contactsLabel.attributedText = attrStr
contactsLabel.sizeToFit()
getAddress()
}
Der getAddress Funktionsaufruf hier nimmt die Daten, die weitergegeben wurden und fügt sich das Label:
func getAddress() {
//getting the JSON array teams from the response
let address: NSArray = myAddress["mailing"] as! NSArray
//looping through all the json objects in the array teams
for i in 0 ..< address.count{
//getting the data at each index
let addrName:String = (address[i] as! NSDictionary)["name"] as! String!
let addrAddr1:String = (address[i] as! NSDictionary) ["address1"] as! String!
let addrAddr2:String = (address[i] as! NSDictionary)["address2"] as! String!
let addrCity:String = (address[i] as! NSDictionary)["city"] as! String!
let addrState:String = (address[i] as! NSDictionary)["state"] as! String!
let addrZipcode:Int = (address[i] as! NSDictionary)["zipcode"] as! Int!
let myCityStateZip : String = addrCity + ", " + addrState + " " + String(addrZipcode)
let myName : String = addrName + "<br />" + addrAddr1 + "<br />" + addrAddr2
let myDescription : String = "*All Correspondence for the Board/Assoc.<br /> should be directed to the following address:<br /><br />"
self.myLabel = self.myLabel + "<b><font size='5'>" + myDescription + "</font></b>"
self.myLabel = self.myLabel + "<font size='4'>" + myName + "</font>"
self.myLabel = self.myLabel + "<font size='4'>" + myCityStateZip + "</font>"
self.myLabel = self.myLabel + "<br /><br />"
let attrStr = try! NSAttributedString(data: myLabel.data(using: String.Encoding.unicode,allowLossyConversion: true)!, options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
contactsLabel.attributedText = attrStr
contactsLabel.sizeToFit()
}
}
Hier ist das Problem, beim ersten Mal funktioniert einwandfrei zeigt die Daten korrekt und in der richtigen Reihenfolge. Wenn Sie wieder auf die Kontaktseite zurückkehren, wird die Adresse oben auf dem Etikett mit den folgenden Kontakten angezeigt. Ich kann nicht herausfinden, warum das passiert. Irgendwelche Ideen?
Wer hat eine Erklärung warum? –
Ich warte immer noch darauf, ob jemand eine vernünftige Antwort auf diese oder eine Lösung des Problems geben kann. –
Wer da draußen hat eine Antwort darauf? Es ergibt keinen Sinn für mich. –