2016-07-14 19 views
0

Ich bin neu in der Welt von Swift und ich habe keine Ahnung, was ich mache! Ich entwickle eine App für die Arbeit, um eine Liste von E-Mails von einem Server sowie Anhänge zu ziehen, wenn die E-Mail eine hat. Ich stoße auf zwei Probleme. Das erste Problem ist, dass ich nicht von NS Dictionary zu NSArray konvertieren kann und ich habe tagelang nach StackOverflow gesucht, um nach einer Lösung zu suchen, die vergebens funktioniert hat. Vielleicht liegt es daran, wie ich den Code geschrieben habe? Das nächste Problem ist danach, dass ich die URLs der E-Mails basierend auf der ID aus dem NSDictionary ziehen muss. Die ID ist auch der letzte Teil der URL jeder E-Mail, aber ich habe keine Ahnung, dass ich überhaupt an dieses Konzept herangehen könnte.NSDictionary zu NSArray mit dynamischer URL-Erkennung

Es tut mir leid, dass ich bedürftig bin, aber ich bin kein Softwareentwickler, weder beruflich, noch in der Ausbildung (ich bin Senior in der Hochschule für Netzwerk/Systeme), also habe ich Probleme mit dieser Programmierlogik das verstehe ich nicht. Hier ist der Code, den ich habe es geschafft, zusammen zu stellen, so weit (Anmerkung: Es bricht in Zeile 72 self.Table = JsonWithDatos as! NSArray.

import Foundation 

import UIKit 

class TableViewControllerNews: UITableViewController { 

@IBOutlet weak var searchBar: UISearchBar! 

@IBOutlet weak var Submit: UIView! 

var Table:NSArray = [] 

override func viewDidLoad() { 
    super.viewDidLoad() 
    CallWebService() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 

} 

func CallWebService() 
{ 
    let UrlApi = "********" 
    let Url = NSURL(string: UrlApi) 
    let Session = NSURLSession.sharedSession() 
    let Work = Session.dataTaskWithURL(Url!, completionHandler: { dataTask, response, error -> Void in 
     if (error != nil) 
     { 
      print(error) 
     } 
     let datos:NSData = NSData(data: dataTask!) 
     print(datos) 
     print(response) 
     self.ParseoDataToJson(datos) 
    }) 

    Work.resume() 
} 

func ParseoDataToJson(datos:NSData) 
{ 
    do { 
     let JsonWithDatos:AnyObject! = try NSJSONSerialization.JSONObjectWithData(datos, options: NSJSONReadingOptions.MutableContainers) 

     print(JsonWithDatos) 

     struct AnnouncementList { 

      var ID: NSArray? 
      var Subject: NSArray? 

      init(announcementObject: AnyObject) { 

//     announcements = (announcementObject["Announcements"] as NSArray) 

       if let announcements = announcementObject["Announcements"] as? NSArray { 
        ID = (announcements.valueForKey("ID") as! NSArray) 
        Subject = (announcements.valueForKey("Subject") as! NSArray) 
       } 
      } 
     } 
     self.Table = JsonWithDatos as! NSArray 
     print(self.Table) 
     print(self.Table.count) 

     self.tableView.reloadData() 

    } 
    catch { 
     print("Error") 
    } 
} 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 

    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return self.Table.count 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 

    let item = self.Table[indexPath.row] as! [String : String] 
    cell.textLabel!.text = item["Subject"] 

    return cell 
} 

override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    return false 
} 
} 

URLs und E-Mail-Themen für die Sicherheit entfernt

Pro Anfrage: Die Json Antwort, wenn der Code ausgeführt wird:

<7b224765 74507265 53686966 74735265 73756c74 223a5b7b 22494422 3a223238 3634222c 22537562 6a656374 223a2230 355c2f30 395c2f32 30313620 2d20416e 6e75616c 204d616d 6d6f6772 616d204d 6f62696c 65227d2c 7b224944 223a2232 38383122 2c225375 626a6563 74223a22 30355c2f 32375c2f 32303136 202d2043 6f6e7374 72756374 696f6e20 696e2047 65617220 44657061 72746d65 6e74227d 2c7b2249 44223a22 32383834 222c2253 75626a65 6374223a 2230365c 2f30315c 2f323031 36202d20 32303136 2046616d 696c7920 46657374 6976616c 20354b20 52756e20 5c2f2057 616c6b20 284b696e 67732049 736c616e 6429227d 2c7b2249 44223a22 32383936 222c2253 75626a65 6374223a 2230365c 2f30385c 2f323031 36202d20 486f6e64 61204772 65656e20 436f6e66 6572656e 6365227d 2c7b2249 44223a22 32383839 222c2253 75626a65 6374223a 2230365c 2f32385c 2f323031 36202d20 5a6f6f5c 2f205a6f 6f6d6265 7a692042 61792045 76656e74 20496e66 6f726d61 74696f6e 20262053 69676e20 5570227d 2c7b2249 44223a22 32393037 222c2253 75626a65 6374223a 2230365c 2f33305c 2f323031 36202d20 41646563 636f2057 61676573 20616e64 2042656e 65666974 73207769 6c6c2062 6520696d 70726f76 65642069 6e204a75 6c79227d 2c7b2249 44223a22 32393039 222c2253 75626a65 6374223a 2230365c 2f33305c 2f323031 36202d20 53706563 69616c20 4173736f 63696174 65205361 76696e67 73206f6e 2043522d 56227d2c 7b224944 223a2232 39313022 2c225375 626a6563 74223a22 30375c2f 31305c2f 32303136 202d2048 544d2773 20326f74 6820416e 6e697665 72736172 79206f66 20547261 6e736d69 7373696f 6e204d61 6e756661 63747572 696e6722 7d2c7b22 4944223a 22323931 31222c22 5375626a 65637422 3a223037 5c2f3130 5c2f3230 3136202d 20506172 6b696e67 204c6f74 20436861 6e676573 227d2c7b 22494422 3a223239 3132222c 22537562 6a656374 223a2230 375c2f31 315c2f32 30313620 2d204d69 642d4f68 696f2052 61636520 5469636b 65747322 7d2c7b22 4944223a 22323931 33222c22 5375626a 65637422 3a223037 5c2f3131 5c2f3230 3136202d 2057696e 6e657273 206f6620 43656461 7220506f 696e745c 2f4b696e 67732049 736c616e 64205469 636b6574 73227d2c 7b224944 223a2232 39313522 2c225375 626a6563 74223a22 30375c2f 31335c2f 32303136 202d204e 6f6e2d45 78656d70 74204a6f 6220506f 7374696e 673a2047 65617222 7d2c7b22 4944223a 22323931 36222c22 5375626a 65637422 3a223037 5c2f3133 5c2f3230 3136202d 20457267 6f204375 70204163 74697669 7479227d 5d7d> 
Optional(<NSHTTPURLResponse: 0x7b63c800> { URL: ******** } { status code: 200, headers { 
"Content-Length" = 1006; 
"Content-Type" = "application/json; charset=utf-8"; 
Date = "Thu, 14 Jul 2016 13:50:40 GMT"; 
Server = "Microsoft-IIS/8.5"; 
"X-Powered-By" = "ASP.NET"; 
} }) 
{ 
GetPreShiftsResult =  (
      { 
     ID = 2864; 
     Subject = "05/09/2016 - ********"; 
    }, 
      { 
     ID = 2881; 
     Subject = "05/27/2016 - ********"; 
    }, 
      { 
     ID = 2884; 
     Subject = "06/01/2016 - ********"; 
    }, 
      { 
     ID = 2896; 
     Subject = "06/08/2016 - ********"; 
    }, 
      { 
     ID = 2889; 
     Subject = "06/28/2016 - ********"; 
    }, 
      { 
     ID = 2907; 
     Subject = "06/30/2016 - ********"; 
    }, 
      { 
     ID = 2909; 
     Subject = "06/30/2016 - ********"; 
    }, 
      { 
     ID = 2910; 
     Subject = "07/10/2016 - ********"; 
    }, 
      { 
     ID = 2911; 
     Subject = "07/10/2016 - ********"; 
    }, 
      { 
     ID = 2912; 
     Subject = "07/11/2016 - ********"; 
    }, 
      { 
     ID = 2913; 
     Subject = "07/11/2016 - ********"; 
    }, 
      { 
     ID = 2915; 
     Subject = "07/13/2016 - ********"; 
    }, 
      { 
     ID = 2916; 
     Subject = "07/13/2016 - ********"; 
    } 
); 
} 
+0

Bitte fügen Sie die JSON-Antwort hinzu, die Sie erhalten. –

+0

Die Json-Antwort zum ursprünglichen Beitrag hinzugefügt. –

Antwort

0

Wenn ich mich nicht täusche, man einfach nicht ein Array erhalte zurück (in JSON-Form), sondern ein Wörterbuch (eine Hash-Tabelle) Versuchen

self.Table = JsonWithDatos["GetPreShiftsResult"] as! NSArray 
.

Sie brauchen nicht einmal die explizite Besetzung, die ich denke (vorausgesetzt, self.Table ist bereits eine NSArray).

Verwandte Themen