2016-04-28 89 views
0

我要添加此可选我应该为这个可选项添加什么标识符?

var mapRegion : MKCoordinateRegion 

它给我,我需要一个标识符旁边类错误:

class TableViewController: UITableViewController, NSFetchedResultsControllerDelegate, 

那是什么标识?

文件看起来是这样的:

import UIKit 
import CoreData 
import MapKit 

class TableViewController: UITableViewController, NSFetchedResultsControllerDelegate, { 

let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext 
var frc : NSFetchedResultsController = NSFetchedResultsController() 

var mapRegion : MKCoordinateRegion 


func fetchRequest() -> NSFetchRequest { 

    let fetchRequest = NSFetchRequest(entityName: "Item") 
    let sortDescriptor = NSSortDescriptor(key: "name", 
              ascending: true) 
    fetchRequest.sortDescriptors = [sortDescriptor] 
    return fetchRequest 
} 
func getFRC() -> NSFetchedResultsController { 

    frc = NSFetchedResultsController(fetchRequest: fetchRequest(), managedObjectContext: moc, sectionNameKeyPath: nil, cacheName: nil) 
    return frc 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    frc = getFRC() 
    frc.delegate = self 

    do { 
     try frc.performFetch() 
    } catch { 
     print("Failed to perform initial fetch.") 
     return 
    } 

    self.tableView.rowHeight = 480 
    self.tableView.backgroundView = UIImageView(image: UIImage(named: "flatgrey2")) 
    self .tableView.reloadData() 
} 

override func viewDidAppear(animated: Bool) { 
    frc = getFRC() 
    frc.delegate = self 

    do { 
     try frc.performFetch() 
    } catch { 
     print("Failed to perform initial fetch.") 
     return 
    } 
    self .tableView.reloadData() 
} 


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 Incomplete implementation, return the number of sections 
    let numberOfSections = frc.sections?.count 
    return numberOfSections! 

} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    let numberofRowsInSection = frc.sections?[section].numberOfObjects 

    return numberofRowsInSection! 

    //return 0 

} 



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

    if indexPath.row % 2 == 0 { 
     cell.backgroundColor = UIColor.clearColor() 
    } else { 
     cell.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.2) 
     cell.textLabel?.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.0) 
     cell.detailTextLabel?.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.0) 
    } 


    // Configure the cell... 
    cell.textLabel?.textColor = UIColor.darkGrayColor() 
    cell.detailTextLabel?.textColor = UIColor.darkGrayColor() 

    let item = frc.objectAtIndexPath(indexPath) as! Item 
    cell.textLabel?.text = item.name 
    cell.imageView?.image = UIImage(data: (item.image)!) 

    return cell 
} 





/* 
// Override to support conditional editing of the table view. 
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    // Return false 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) { 

    let managedObject : NSManagedObject = frc.objectAtIndexPath(indexPath) as! NSManagedObject 
    moc.deleteObject(managedObject) 

    do { 
     try moc.save() 
    } catch { 
     print ("Failed to save.") 
     return 
    } 
} 

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) { 
    tableView.reloadData() 
} 






/* 
// 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 false 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. 
    if segue.identifier == "edit" { 

     let cell = sender as! UITableViewCell 
     let indexPath = tableView.indexPathForCell(cell) 
     let itemController : AddEditVC = segue.destinationViewController as! AddEditVC 
     let item : Item = frc.objectAtIndexPath(indexPath!) as! Item 
     itemController.item = item 
    } 

} 

}

回答

0

您已经声明var mapRegion : MKCoordinateRegion,但没有任何初始化或默认值。为了使一个可选的,只需添加?它,所以声明:

var mapRegion : MKCoordinateRegion? 
+0

谢谢你的回答! –

1

变量应该有一个值,当它被声明,否则,它会显示这个错误。为您的变量var mapRegion : MKCoordinateRegion分配一个值。如果该值是在稍后阶段给出的,也就是说,如果它取决于任何计算,则将其设置为这样的可选项var mapRegion : MKCoordinateRegion?但是,只要您想要使用mapRegion值,就必须在变量末尾放置感叹号变量名称。

+0

谢谢你的回答! –

相关问题