模糊地使用'下标'突然出现了,但代码才开始工作

时间:2016-04-01 05:14:28

标签: ios swift swift2 nsarray uidatepicker

突然间,我开始收到一个错误,说错误地使用了下标,但是如果我转到之前的git提交,那么我发现一切正常,我不知道会发生什么。以下是我的班级:

import Foundation
class NewScheduleViewController: UIViewController, UITableViewDataSource, UITableViewDelegate ,UIPickerViewDataSource,UIPickerViewDelegate {

    @IBOutlet weak var schedulesTable: UITableView!


    var x = 1
    var y = 0
    var z = 0
    var time1 = Int()
    var time2 = Int()
    var rowNumber = Int()
    var tagNumber = Int()
    var pickerView: UIPickerView!
    var pickerValues: NSMutableArray = []
    var startingTime = String()
    var endingTime = String()
    var daysArray = ["Monday" , "Tuesday" ,"Wednesday" ,"Thursday" ,"Friday" ,"Saturday" ,"Sunday" ];
    enum pickerComponent:Int{
        case weekday = 0
        case minutos = 1
        case meridiano = 2
    }

    var schedulesArray = NSMutableArray()
    override func viewDidLoad() {
        super.viewDidLoad()
        pickerValues = [["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"],["30","00"], ["AM", "PM"]]

        schedulesTable.delegate = self
        schedulesTable.dataSource = self
        addLeftBarButton ()

        pickerView = UIPickerView()
        pickerView.backgroundColor = UIColor.whiteColor()

        pickerView.dataSource = self
        pickerView.delegate = self

        let webServiceHandler = FetchScheduleData.init()
        webServiceHandler.fetchDataFromWebService(["doctor_id" : 7], closure: { (response:FetchScheduleListResponse) -> Void in
            if response.doctorschedule?.count == 0 {

                return;
            }
            self.schedulesArray.removeAllObjects()
            for appointmentListResponse in response.doctorschedule! {
                print( "Value of  index is \(appointmentListResponse)")
                self.schedulesArray.addObject(appointmentListResponse)
                print("Index is " ,appointmentListResponse.day_index)
            }
            self.schedulesTable.reloadData()
        })


    }

    func addLeftBarButton () {

        let myBackButton:UIButton = UIButton()
        myBackButton.addTarget(self, action: "segmentedAction:", forControlEvents: UIControlEvents.TouchUpInside)
        myBackButton.setImage(UIImage(named: "newBack.png"), forState: .Normal)
        myBackButton.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
        myBackButton.sizeToFit()
        let myCustomBackButtonItem:UIBarButtonItem = UIBarButtonItem(customView: myBackButton)
        self.navigationItem.leftBarButtonItem  = myCustomBackButtonItem
        let img = UIImage(named: "top-menu.jpg")
        navigationController?.navigationBar.setBackgroundImage(img, forBarMetrics: .Default)

    }

    @IBAction func segmentedAction(sender: AnyObject) {        
        self.dismissViewControllerAnimated(true, completion: nil)
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

   //functions for table

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1

    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return daysArray.count

    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell : ScheduleCell! = tableView.dequeueReusableCellWithIdentifier("scheduleCell") as! ScheduleCell
        cell.selectionStyle = UITableViewCellSelectionStyle.None;        
        cell!.editButton.tag = indexPath.row;
        cell!.editButton.addTarget(self, action: "yourButtonClicked:", forControlEvents: UIControlEvents.TouchUpInside)        
        cell.startTime.inputView = pickerView
        cell.startTime.tag = (indexPath.row)*2 + 1
        cell.endTime.inputView = pickerView
        cell.endTime.tag = (indexPath.row)*2 + 2
        cell.startTime.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingDidBegin)
        cell.endTime.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingDidBegin)
        cell.dayLabel.text = daysArray[indexPath.row]        
        cell.startTime.userInteractionEnabled = false
        cell.endTime.userInteractionEnabled = false

        for (var i =  0 ; i < schedulesArray.count ; i++  ) {
            let scheduleDetails : FetchScheduleDetailResponse = self.schedulesArray.objectAtIndex(i) as! FetchScheduleDetailResponse
            print(scheduleDetails.day_index)
            if (Int(scheduleDetails.day_index!)  ==  indexPath.row+1 ) {
                cell.startTime.text = scheduleDetails.start_hour
                cell.endTime.text = scheduleDetails.end_hour
            }
        }                
        return cell as ScheduleCell
    }

    func textFieldDidChange (sender : UITextField) {         
        if(sender.tag % 2 == 0) {
        rowNumber = sender.tag / 2
            y = 3
        } else {
        rowNumber = (sender.tag / 2) + 1
            y = 2
        }        
         tagNumber = sender.tag        
    }

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return pickerValues.count
    }

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
        return pickerValues[component].count
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {        
        return pickerValues[component][row] as? String
    }    
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {        
        self.view.endEditing(true)        
        let indexPath = NSIndexPath(forRow: rowNumber - 1, inSection: 0)
        let  cell = self.schedulesTable.cellForRowAtIndexPath(indexPath) as! ScheduleCell                
        let weekDay2 = pickerComponent.weekday.rawValue
        let weekDay = pickerValues[weekDay2][pickerView.selectedRowInComponent(weekDay2)] as! String
        if y == 1 {
        }  else {
            let minutos2 = pickerComponent.minutos.rawValue
            let minutos = pickerValues[minutos2][pickerView.selectedRowInComponent(minutos2)] as! String
            let meridiano2 = pickerComponent.meridiano.rawValue
            let meridiano = pickerValues[meridiano2][pickerView.selectedRowInComponent(meridiano2)] as! String            
            if meridiano == "PM" {
                z = 2
            } else {
                z = 1
            }            
            if y == 2 {
                if cell.startTime.tag == tagNumber {
                    cell.startTime.text = "\(weekDay) : \(minutos)   \(meridiano)"
                        startingTime =  "\(weekDay) : \(minutos)   \(meridiano)"
                }

                if z == 2 {
                    time1 = Int(weekDay)!*60 + Int(minutos)! + 12*60
                }else{
                    time1 = Int(weekDay)!*60 + Int(minutos)!
                }
                print("time1 = \(time1)")
            }

            if y == 3 {
                if z == 2 {
                    time2 = Int(weekDay)!*60 + Int(minutos)! + 12*60
                }
                else{
                    time2 = Int(weekDay)!*60 + Int(minutos)!
                }
                if time1 >= time2 {
                }
                if ((time2 - time1) % DoctorInformation.call_duration) != 0 {
                }
                else{
                    if cell.endTime.tag == tagNumber {
                        cell.endTime.text = "\(weekDay) : \(minutos)   \(meridiano)"
                        endingTime = "\(weekDay) : \(minutos)   \(meridiano)"
                    }

                }                
                print(DoctorInformation.call_duration)
                let nd2 = time2 - time1
                let nd = (time2 - time1) % DoctorInformation.call_duration
                print("dif = \(nd2)")
                print("modulo = \(nd)")
                print("time2 = \(time2)")

            }

        }

    }


    func yourButtonClicked (sender : UIButton){
        let indexPath = NSIndexPath(forRow:  sender.tag, inSection: 0)
        let  cell = self.schedulesTable.cellForRowAtIndexPath(indexPath) as! ScheduleCell

        if sender.titleForState(UIControlState.Normal) == "Edit" {
            sender.setTitle("Done", forState: UIControlState.Normal)
            cell.startTime.userInteractionEnabled = true
            cell.endTime.userInteractionEnabled = true

        } else {
            sender.setTitle("Edit", forState: UIControlState.Normal)
            cell.startTime.userInteractionEnabled = false
            cell.endTime.userInteractionEnabled = false            
            let userDefaultData = NSUserDefaultData()
            let webServiceHandler = SetScheduleDataHandler.init()
            webServiceHandler.fetchDataFromWebServicePost(["doctor_id": userDefaultData.getDoctotId() , "start_time" : startingTime , "end_time" : endingTime , "day_index" : 1], closure: {(response:SetScheduleResponse) -> Void in
                 Alerts.showAlert("Done", alertTitle: response.Status!, viewController: self)
            })


        }

    }


    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

                return 75

        }    
}

This is where error has come

1 个答案:

答案 0 :(得分:-1)

所以花一些时间我能够解决问题如下

return(pickerValues [component] as!NSArray)[row] as!字符串