我的应用程序没有在iPhone / iPad上运行,但在模拟器上运行正常

时间:2016-07-29 06:10:03

标签: ios debugging swift2

我从服务器获取所有数据并使用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)
                }
            }
        }
    }
}

Here is the picture of error in terminal

3 个答案:

答案 0 :(得分:0)

可能的原因是你使用的是String类型变量,它包含一些导致崩溃的长值。

您可以放置​​异常断点来识别导致崩溃的确切代码行并修复它。

enter image description here

enter image description here

答案 1 :(得分:0)

检查您获得的字典(用于创建MyStory对象的字典)。从它的外观来看,storyId作为String返回,您尝试将其保存到longstoryId应设置为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)!

它有效...谢谢大家:))