我正在尝试使用Json中的详细信息元素对View进行编码。然而,编译器因准备segue而崩溃。
@IBOutlet weak var menuButton:UIBarButtonItem!
let yourJsonFormat: String = "JSONFile" // set text JSONFile : json data from file
// set text JSONUrl : json data from web url
var arrDict :NSMutableArray=[]
// @IBOutlet weak var eventCalendar:UITableView!
override func viewDidLoad()
{
super.viewDidLoad()
if self.revealViewController() != nil {
menuButton.target = self.revealViewController()
menuButton.action = "revealToggle:"
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
if yourJsonFormat == "JSONFile" {
jsonParsingFromFile()
} else {
jsonParsingFromURL()
}
}
func jsonParsingFromURL () {
let url = NSURL(string: "http://theappguruz.in//Apps/iOS/Temp/json.php")
let request = NSURLRequest(URL: url!)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
self.startParsing(data!)
}
}
func jsonParsingFromFile()
{
let path: NSString = NSBundle.mainBundle().pathForResource("days", ofType: "json")!
let data : NSData = try! NSData(contentsOfFile: path as String, options: NSDataReadingOptions.DataReadingMapped)
self.startParsing(data)
}
func startParsing(data :NSData)
{
let dict: NSDictionary!=(try! NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary
for var i = 0 ; i < (dict.valueForKey("objects") as! NSArray).count ; i++ {
arrDict.addObject((dict.valueForKey("objects") as! NSArray) .objectAtIndex(i))
}
}
// eventCalendar .reloadData()
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return arrDict.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! TableViewCell
let strName : NSString=arrDict[indexPath.row] .valueForKey("name") as! NSString
let strSubtitle : NSString=arrDict[indexPath.row] .valueForKey("subtitle") as! NSString
let strLocation : NSString=arrDict[indexPath.row] .valueForKey("location") as! NSString
let strStart : NSString=arrDict[indexPath.row] .valueForKey("start") as! NSString
if let strImage = arrDict[indexPath.row] .valueForKey("image") as? String {
if let data = NSData(contentsOfURL: NSURL(string:strImage )!) {
cell.lbImage.image = UIImage(data: data)
}
}
cell.lbName.text=strName as String
cell.lbSubtitle.text=strSubtitle as String
cell.lbStart.text=strStart as String
cell.lbLocation.text=strLocation as String
return cell as TableViewCell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var detailView = segue.destinationViewController as! EventDetail
if segue.identifier == EventDetail.identifier() {
if let indexPath = self.tableView.indexPathForSelectedRow {
let selectedRow = indexPath.row
let selected = self.arrDict[selectedRow]
let vc = segue.destinationViewController as! EventDetail
vc.ARRDICT = selected as! NSMutableArray
}
}
}
错误信息是:
由于未捕获的异常'NSRangeException'而终止应用程序,原因:'*** - [__ NSArrayM objectAtIndex:]:索引0超出空数组的边界'
答案 0 :(得分:0)
仅用于测试,您应该删除故事板中的自动segue并添加手动segue(类型显示),使用标识符并在self.performSegueWithIdentifier("yourIdentifier", sender: sender)
中调用didSelectRowAtIndexPath
此方法indexPath
使用此indexpath用于获取数组中的位置,并尝试将该对象作为sender参数发送到该位置。并在prepareForSegue
。
请将startParsing
循环更改为。
let arrayObjects = dict.valueForKey("objects") as! NSArray
for eachDict in arrayObjects {
let dict = eachDict as! NSDictionary
arrDict.addObject(dict)
}
更好level of complexity