我从服务器获取所有数据并使用JSON解析它。在收到每个故事后,我创建了一个名为MyStory
的课程。
class MyStory: NSObject
{
var location = ""
var storyId = 0
var title = ""
var story = ""
var AllImages = [Image]()
var images = NSArray() {
willSet(newValue){
// print("newvalue \(newValue)")
for obj in newValue {
let img = Image(dictionary: obj as! [String : AnyObject])
AllImages.append(img)
//imagePaths.append(img.imagePath)
let helper = HelperClass()
var imageDetail = "http://www.sleeksolutions.net/saad/" + img.imagePath
helper.imagepath.append(imageDetail)
//print("counter: \(helper.imagepath.count)")
}
}
}
init(dictionary: [String:AnyObject]) {
super.init()
self.setValuesForKeysWithDictionary(dictionary)
}
override func setValue(value: AnyObject?, forKey key: String) {
if key == "images"
{
// print("key = \(key) , value : \(value)")
}
//error occurs here.
super.setValue(value, forKey: key)
}
}
此类函数由类SeeStories
调用。这是代码:
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: url!)
request.timeoutInterval = 10
request.HTTPMethod = "GET"
let task = session.dataTaskWithRequest(request)
{
(dat, res, er) in
if er == nil
{
do
{
print("m hereeee")
self.serverStatus = true
let jsonObject = try NSJSONSerialization.JSONObjectWithData(dat!, options: .MutableContainers)
let arr = jsonObject as! NSDictionary
//print(jsonObject)
let storiees = arr["stories"] as! NSArray
for obj in storiees{
//Calling to MyStory Class here
let s = MyStory(dictionary: obj as! [String : AnyObject])
self.stories.append(s)
let stat = self.fetch(s)
if stat == false
{
self.saveInCoreData(s)
}
}
}
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
检查您获得的字典(用于创建MyStory
对象的字典)。从它的外观来看,storyId
作为String
返回,您尝试将其保存到long
。 storyId
应设置为String
变量
class MyStory: NSObject
{
var location = ""
var storyId = "" //THIS instead of = 0
var title = ""
var story = ""...
我可能错了,但这是我在你的问题中显示的错误日志中最好的猜测。
答案 2 :(得分:0)
我找到了答案。我的字典传递给我字符串值,我在Int值中保存。所以在init方法中我确实喜欢这个:
imgid = dictionary [" imageId"]! imageId = String(imgid as!String)!
它有效...谢谢大家:))