Я пытаюсь кодировать представление с элементами деталей из Json. Однако сбой компилятора с подготовкой к segue.Json - Подготовьтесь к segue to Details View
@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
}
}
}
И сообщение об ошибке:
толькоTerminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
Пожалуйста, добавьте полный код. Например, структура для 'arrDict'. Почему бы не отправить информацию в отправителе? – jose920405
Я поставил код ... Почему, по-вашему, отправитель лучше? Спасибо. – user3623737
Добавили ли вы 'showEventDetail' идентификатор segue в раскадровку? Если нет, откройте раскадровку, найдите свой segue и определите его с идентификатором showEventDetail. –