Как это исправить, чтобы восстановить мой код, используя парсер xml? вызвать ошибку

func postRestore(params : Dictionary<String, AnyObject!>, viewController: UIViewController, postCompleted: (status: String, genSettingsArray: NSArray, tmplArray: NSArray, tileArray: NSArray) -> ()) {

let url = NSURL(string: postURL)
var xmlParse:NSString  = ""var data : NSData!
let request = NSMutableURLRequest(URL: url!)
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.HTTPMethod = "POST"let param = params
var error: NSError?
if let body = NSJSONSerialization.dataWithJSONObject(param, options: nil, error: &error) {
request.HTTPBody = body
} else {
println("JSON error: \(error)")
}

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in
println(response)

var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
println(strData!)

if data == nil {
println("dataTaskWithRequest error: \(error)")
return
}

let parser = NSXMLParser(data: data)
parser.delegate = self
parser.parse()

postCompleted(status: strData! as String, genSettingsArray: self.generalSettingsArray, tmplArray: self.tmplArray, tileArray: self.tileArray)

var page1 = self.generalSettingsArray.valueForKey("page1") as! NSArray
var page2 = self.generalSettingsArray.valueForKey("page2") as! NSArray
var page3 = self.generalSettingsArray.valueForKey("page3") as! NSArray
var page4 = self.generalSettingsArray.valueForKey("page4") as! NSArray
var page5 = self.generalSettingsArray.valueForKey("page5") as! NSArray

for p1 in page1 {
self.pagesArray.append(p1 as! String)
}
for p2 in page2 {
self.pagesArray.append(p2 as! String)
}
for p3 in page3 {
self.pagesArray.append(p3 as! String)
}
for p4 in page4 {
self.pagesArray.append(p4 as! String)
}
for p5 in page5 {
self.pagesArray.append(p5 as! String)
}



for pages in self.pagesArray {
self.pageId = self.pageId + 1
let realm = Realm()
let page = Pages()
page.id = self.pageId
page.pageTitle = pages

realm.write {

realm.add(page, update: true)

for settings in self.generalSettingsArray {
realm.create(GeneralSettings.self, value: settings, update: true)
}
for tmpl in self.tmplArray {
realm.create(Templates.self, value: tmpl, update: true)
}
for tile in self.tileArray {
realm.create(Tile.self, value: tile, update: true)
}

}
}

println(Realm.defaultPath)
}
task.resume()

}

потому что у меня была такая ошибка, когда она отвечала мне:

<NSHTTPURLResponse: 0x7fc541cb2f10> { URL: http://www.cloudstaff.com/sendmail/csa_restore.php } { status code: 200, headers {
"Cache-Control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
Connection = "Keep-Alive";
"Content-Encoding" = gzip;
"Content-Length" = 20;
"Content-Type" = "text/xml";
Date = "Thu, 20 Aug 2015 02:23:11 GMT";
Expires = "Thu, 19 Nov 1981 08:52:00 GMT";
"Keep-Alive" = "timeout=15, max=100";
Pragma = "no-cache";
Server = "Apache/2.2.14 (Ubuntu)";
"Set-Cookie" = "pamail=5b7dbglbh194e2um10u0ps0944; path=/";
Vary = "Accept-Encoding";
"X-Powered-By" = "PHP/5.3.2-1ubuntu4.19";
} }

/Users/rhoverf/Library/Developer/CoreSimulator/Devices/B234847F-7F5A-40FC-A510-B2667158A5DE/data/Containers/Data/Application/D1A6F8BD-508B-42A5-A338-C1BCC82042B3/Documents/default.realm
/Users/rhoverf/Library/Developer/CoreSimulator/Devices/B234847F-7F5A-40FC-A510-B2667158A5DE/data/Containers/Data/Application/D1A6F8BD-508B-42A5-A338-C1BCC82042B3/Documents/default.realm
fatal error: unexpectedly found nil while unwrapping an Optional value

-1

Решение

Так как единственное, что вам кажется принудительным, — это когда вы вызываете println(strData!)я думаю, что strData на самом деле nil,

0

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]