2016-04-28 87 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

謝謝你的回答! –

相關問題