2015-01-02 2 views
-2

이 자습서 (https://www.youtube.com/watch?v=Q6qcrO8uNzU&feature=youtu.be)를 수행했습니다. 아래는 마지막에 생성되는 코드입니다. 그러나 getShifts.findObjectsInBackgroundWithBlock { (objects:AnyObject[]!, error:NSError!) -> Void in에 대한UITableViewController 및 셀을 사용하여 구문 분석

나는 AnyObject[]![AnyObject]!과 같이 넣어야한다고 말하는 오류가 발생하지만 그저 오류를 만듭니다. 어떤 아이디어?

import UIKit 

class AvailableShifts: UITableViewController { 

var shiftData: NSMutableArray! 

override func viewDidAppear(animated: Bool) { 
    super.viewDidAppear(true) 

    loadData() 

    // Uncomment the following line to preserve selection between presentations 
    // self.clearsSelectionOnViewWillAppear = false 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem() 
} 

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

// MARK: - Table view data source 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Potentially incomplete method implementation. 
    // Return the number of sections. 
    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    return 2//shiftData.count 
} 


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell:AvailableShiftsCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as AvailableShiftsCell 

    let shift: PFObject = shiftData.objectAtIndex(indexPath.row) as PFObject 

    cell.shiftLabel.text = shift.objectForKey("Shift") as String 

    // Configure the cell... 

    return cell 
} 


/* 
// Override to support conditional editing of the table view. 
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    // Return NO if you do not want the specified item to be editable. 
    return true 
} 
*/ 

/* 
// Override to support editing the table view. 
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
    if editingStyle == .Delete { 
     // Delete the row from the data source 
     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
    } else if editingStyle == .Insert { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    }  
} 
*/ 

/* 
// Override to support rearranging the table view. 
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) { 

} 
*/ 

/* 
// Override to support conditional rearranging of the table view. 
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    // Return NO if you do not want the item to be re-orderable. 
    return true 
} 
*/ 

/* 
// 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. 
} 
*/ 

func loadData() { 

    shiftData.removeAllObjects() 

    var getShifts = PFQuery(className: "Shifts") 

    getShifts.findObjectsInBackgroundWithBlock { 
     (objects:AnyObject[]!, error:NSError!) -> Void in 

     if (error == nil) { 

      for object: PFObject! in objects { 

       self.shiftData.addObject(object) 
      } 

      let array: NSArray = self.shiftData.reverseObjectEnumerator().allObjects 

      self.shiftData = array as NSMutableArray 

      self.tableView.reloadData() 
     } 

    } 

} 

} 
+0

정확한 질문이 무엇인지 모르겠지만 xcode가 구현을 기반으로 제안을 생성 한 경우 제시된 나머지 오류를 통해 제안 및 문제를주의 깊게 관찰하는 것이 좋습니다. AnyObject에 대한 올바른 구문을 삽입 한 후 오류가 정확히 무엇입니까? [AnyObject]가 실제로 무엇을하고 있는지 [HERE] (https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html)를 검토하십시오. NSArray 검색 – soulshined

답변

0

도움을 준 다음, var shiftData = [PFObject]()

에 최고 var shiftData: NSMutableArray!에서 먼저 변경하고 FUNC의 loadData()

func loadData() { 

    var getShifts = PFQuery(className: "Shifts") 

    getShifts.findObjectsInBackgroundWithBlock { 
    (objects: [AnyObject]!, error: NSError!) -> Void in 
    if error == nil { 

    var shiftData = objects as [PFObject] 
    self.shiftData = shiftData 
    var lastIndex = NSIndexPath(forRow: self.shiftData.count - 1, inSection: 0) 

    } 
} 
} 

및 cellForRowAtIndexPath에서

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
let cell = tableView.dequeueReusableCellWithIdentifier("Cell" as AvailableShiftsCell 
    let shift = self.shiftData[indexPath.row] 
     cell.shiftLabel.text = shift as String 

    return cell 
} 

희망을 봅니다 .