Ich mache eine einfache News-App, und versuche zu speichern (Titel, Beschreibung, Datum, Bild mit seinen Daten) in NSUserDefaults für offline lesen. Ich möchte, wenn es Daten in NSUserDefaults speichert, es offline zeigt und auch wenn neue Daten verfügbar sind, überschreibt es oder ersetzt durch vorherige Daten. Ich kann String-Arrays in NSUserDefaults speichern, wusste aber nicht genau, wie das Image in NSUserDefaults gespeichert wurde. Ich versuche, Logik des Speicherns von Daten zu machen und es zu überprüfen, wenn neue Daten verfügbar sind, aber Erfolg nicht haben, auch Splash-Bildschirm braucht mehr Zeit, um zu verschwinden, ist es aufgrund des Ladens von Daten vom Server oder wegen langsamer Internetverbindung? Kann jemand bitte überprüfen Sie meinen Code für die Reparatur. DankXML-Daten speichern in NSUserDefaults oder im Dokumentenverzeichnis
class ViewController2: UIViewController ,NSXMLParserDelegate {
let newsDefaults = NSUserDefaults.standardUserDefaults()
@IBOutlet weak var tableView: UITableView!
var parser = NSXMLParser()
var posts = NSMutableArray()
var elements = NSMutableDictionary()
var element = NSString()
var title1 = NSMutableString()
var date = NSMutableString()
var link = NSMutableString()
var des = NSMutableString()
var img2 = NSMutableString()
var NSUserDefaultsTitle : [NSString] = []
var NSUserDefaultsDate : [NSString] = []
var NSUserDefaultsDes : [NSString] = []
var NSUserDefaultsImage : [UIImage] = []
typealias CompletionHandler = (image: UIImage) -> Void
var attrsUrl : [String : NSString]!
var urlPic : NSString?
var postLink: String = String()
override func viewDidLoad() {
super.viewDidLoad()
self.configure()
self.beginParsing()
}
override func viewWillAppear(animated: Bool) {
// if let HaveTitle = newsDefaults.objectForKey("t"){
// NSUserDefaultsTitle = HaveTitle.mutableCopy() as! [NSString]
// }
// if let HaveDate = newsDefaults.objectForKey("d"){
// NSUserDefaultsDate = HaveDate.mutableCopy() as! [NSString]
// }
// if let HaveDes = newsDefaults.objectForKey("des"){
// NSUserDefaultsDes = HaveDes.mutableCopy() as! [NSString]
// }
// if let imageData = newsDefaults.objectForKey("imgData"){
// NSUserDefaultsImage = imageData.mutableCopy() as! [UIImage]
// }
//
// print(newsDefaults.objectForKey("d"))
}
func beginParsing()
{
posts = []
parser = NSXMLParser(contentsOfURL:(NSURL(string: "http://www.voanews.com/api/zq$omekvi_"))!)!
parser.delegate = self
parser.parse()
tableView!.reloadData()
}
//XMLParser Methods
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String])
{
element = elementName
if (elementName as NSString).isEqualToString("item")
{
elements = NSMutableDictionary()
elements = [:]
title1 = NSMutableString()
title1 = ""
date = NSMutableString()
date = ""
link = NSMutableString()
link = ""
des = NSMutableString()
des = ""
img2 = NSMutableString()
img2 = ""
}
if elementName == "enclosure" {
attrsUrl = attributeDict as [String: NSString]
urlPic = attrsUrl["url"]
print(urlPic!, terminator: "")
}
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?)
{
if (elementName as NSString).isEqualToString("item") {
if !title1.isEqual(nil) {
elements.setObject(title1, forKey: "title")
}
if !date.isEqual(nil) {
elements.setObject(date, forKey: "pubDate")
}
if !link.isEqual(nil) {
elements.setObject(link, forKey: "link")
}
if !des.isEqual(nil){
elements.setObject(des, forKey: "description")
}
if !img2.isEqual(nil){
elements.setObject(urlPic!, forKey: "enclosure")
}
posts.addObject(elements)
//
// if let HaveData = newsDefaults.objectForKey("post"){
//
// }else{
//
//// newsDefaults.setObject(self.posts.valueForKey("title"), forKey: "t")
//// newsDefaults.setObject(self.posts.valueForKey("pubDate"), forKey: "d")
//// newsDefaults.setObject(self.posts.valueForKey("description"), forKey: "des")
//
// newsDefaults.setObject(posts, forKey: "post")
//
print("elementName")
// }
}
print("didEndElement")
}
func parser(parser: NSXMLParser, foundCharacters string: String)
{
if element.isEqualToString("title") {
title1.appendString(string)
} else if element.isEqualToString("pubDate") {
date.appendString(string)
print(date)
}
else if element.isEqualToString("link"){
link.appendString(string)
}else if element.isEqualToString("description"){
des.appendString(string)
}else if element.isEqualToString("enclosure"){
img2.appendString(string)
}
print("foundCharacter")
}
private func configure() {
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont.boldSystemFontOfSize(20.0), NSForegroundColorAttributeName: UIColor.whiteColor()]
self.tableView.registerNib(UINib(nibName: "2ImageCell", bundle: nil), forCellReuseIdentifier: "imageCell")
self.tableView.delegate = self
self.tableView.dataSource = self
self.fillNavigationBar(color: UIColor(red: 252.0/255.0, green: 0, blue: 0, alpha: 1.0))
}
private func fillNavigationBar(color color: UIColor) {
if let nav = self.navigationController {
nav.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
nav.navigationBar.shadowImage = UIImage()
for view in nav.navigationBar.subviews {
if view.isKindOfClass(NSClassFromString("_UINavigationBarBackground")!) {
if view.isKindOfClass(UIView) {
(view as UIView).backgroundColor = color
}
}
}
}
}
}
extension ViewController2: UITableViewDelegate, UITableViewDataSource {
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
let view = ImageModalView2.instantiateFromNib()
view.des.text = posts.objectAtIndex(indexPath.row).valueForKey("description") as? String
downloadFileFromURL(NSURL(string: self.posts.objectAtIndex(indexPath.row).valueForKey("enclosure") as! String)!, completionHandler:{(img) in
dispatch_async(dispatch_get_main_queue(), {() -> Void in
view.image = img
})
})
let window = UIApplication.sharedApplication().delegate?.window!
let modal = PathDynamicModal.show(modalView: view, inView: window!)
view.closeButtonHandler = {[weak modal] in
modal?.closeWithLeansRandom()
return
}
}
@IBAction func printData(sender: AnyObject) {
print(NSUserDefaultsImage)
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 80.0
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("imageCell", forIndexPath: indexPath) as! ImageCell2
// if let picURL = user["picture"].string, url = NSURL(string: picURL) {
// if let data = NSData(contentsOfURL: url) {
// cell!.imageView?.image = UIImage(data: data)
// }
cell.titleLabel.text = posts.objectAtIndex(indexPath.row).valueForKey("title") as! NSString as String
// if NSUserDefaultsImage {
// cell.sideImageView.image = NSUserDefaultsImage[indexPath.row]
// }else{
downloadFileFromURL(NSURL(string: self.posts.objectAtIndex(indexPath.row).valueForKey("enclosure") as! String)!, completionHandler:{(img) in
dispatch_async(dispatch_get_main_queue(), {() -> Void in
cell.sideImageView.image = img
// self.NSUserDefaultsImage.append(img)
// print(img)
// self.newsDefaults.setObject(self.NSUserDefaultsImage, forKey: "imgData")
})
})
// }
//cell.date.text = posts.objectAtIndex(indexPath.row).valueForKey("pubDate") as? String
//cell.sideImageView?.contentMode = UIViewContentMode.ScaleAspectFit
//cell.sideImageView?.image = image
cell.titleLabel.userInteractionEnabled = false
cell.titleLabel.editable = false
cell.titleLabel.selectable = false
return cell
}
func downloadFileFromURL(url1: NSURL?,completionHandler: CompletionHandler) {
// download code.
if let url = url1{
let priority = DISPATCH_QUEUE_PRIORITY_HIGH
dispatch_async(dispatch_get_global_queue(priority, 0)) {
let data = NSData(contentsOfURL: url)
if data != nil {
print("image downloaded")
completionHandler(image: UIImage(data: data!)!)
}
}
}
}
}