尝试从URL打开JSON文件时出现致命错误:在解包可选值

时间:2016-09-16 08:07:18

标签: json swift parsing url

这是我的代码:

func Ivan()
{

    if !timer.valid{ //prevent more than one timer on the thread
        //  timerLabel.text = timeString(60 - storedTime)
        timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
                                                       target: self,
                                                       selector: "timerDidEnd:",
                                                       userInfo: "Офертата е изтекла",
                                                       repeats: true) //repeating timer in the second iteration
    }

}
func marseJSON(Person: String)
{
   // let path: String = NSBundle.mainBundle().pathForResource("document1", ofType: "json") as String!
    let jsonData1 = NSData(contentsOfURL: NSURL(string: "http://www.maslinko.com/document1.json")!) as NSData!
    //let jsonData = NSData(contentsOfFile: path) as NSData!
    let ReadableJSON1 = JSON ( data:jsonData1, options: NSJSONReadingOptions.MutableContainers, error: nil )


    // var Person = Person
    //  Person += "\(RandomWordGen)"
    //   NumberRows = ReadableJSON ["People"].count

    let NameLabel1 = ReadableJSON1 ["People"] [Person]["A1"].string as String!

    let NameImeNaObekt = ReadableJSON1 ["People"] [Person] ["B1"].string as String!

    let Picture = ReadableJSON1 ["People"] [Person] ["E1"].string as String!



    let url = NSURL(string: Picture)

    let data = NSData(contentsOfURL: url!)


    LabelText1.text = NameLabel1
    LabelText2.text = NameImeNaObekt
    ImageView.image = UIImage (data: data!)

}
func timerDidEnd(timer:NSTimer)
{
    //timerLabel.text = timer.userInfo as? String

    let storedTime: Double = NSUserDefaults.standardUserDefaults().doubleForKey("storedTime")
    let currentTime = NSDate().timeIntervalSince1970

    //timer that counts down
    timeCount = timeCount - timeInterval


    if (60 - (currentTime - storedTime)) <= 0    {  //test for target time reached.
        TimerLabel.text = "Офертата е изтекла"
        timer.invalidate()
    } else { //update the time on the clock if not reached



        TimerLabel.text = timeString(60 - (currentTime - storedTime))
    }
}
func timeString(time:NSTimeInterval) -> String {
    let hours = Int(time) / 3600
    let minutes = Int(time) / 60 % 60
    let seconds = Int(time) % 60
    return String(format:"%02i:%02i:%02i", hours, minutes, seconds)
}
func displayQuote(){
    let RandomWordGen = arc4random_uniform(5)
    // get the quote stored in NSUserDefaults for the key "storedPerson"
    let storedPerson: String? = NSUserDefaults.standardUserDefaults().stringForKey("storedPerson")
    // get the time stored in NSUserDefaults for the key "storedTime"
    let storedTime: Double = NSUserDefaults.standardUserDefaults().doubleForKey("storedTime")



    // get the current time interval since 1970
    let currentTime = NSDate().timeIntervalSince1970
    var Person: String = "Person"

    // if the stored quote doesn't exist, or it has been more than
    // a day (60 * 60 * 24 seconds) since the quote was stored

    //let dosiTime: Double=currentTime - storedTime
    Ivan()



    if(storedPerson == nil || currentTime - storedTime >= 60) {


        // generate a new person
        Person+="\(RandomWordGen)"



        // store the newly generated quote for the key "storedQuote"
        NSUserDefaults.standardUserDefaults().setObject(Person, forKey: "storedPerson")
        // store the current time for the key "storedTime"
        NSUserDefaults.standardUserDefaults().setObject(currentTime, forKey: "storedTime")
        NSUserDefaults.standardUserDefaults().synchronize()
    }
    else{
        // otherwise, the quote != nil and was generated less than a day ago
        // so this one should be displayed
        Person = storedPerson!



    }

    //display quoteToDisplay
    marseJSON(Person)

}





/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/
@IBAction func Back(sender: AnyObject) {
     self.dismissViewControllerAnimated(true, completion: nil)

}
@IBAction func Igrai(sender: AnyObject) {
    displayQuote()
}

}

当我尝试打开JSON文件时导致问题,我收到此错误: 致命错误:在展开Optional值时意外发现nil 问题不在我的JSON文件中,因为我有2个JSON文件,它们是相同的document2.json和document1.json 如果我写document2.json应用程序工作正常,但是当我写document1.json时它会向我显示错误。你能否告诉我可能导致问题的原因,因为文件是相同的而且主机是相同的只有名字是不同的1 / 2.也许我已经在代码中的其他地方保存了document2,但我不知道在哪里,请你告诉我如何修复它,因为我需要从它们两个读取。

0 个答案:

没有答案