我应该为这个可选添加啥标识符?
Posted
技术标签:
【中文标题】我应该为这个可选添加啥标识符?【英文标题】:What identifier should I add for this optional?我应该为这个可选添加什么标识符? 【发布时间】:2016-04-28 06:54:02 【问题描述】:我想添加这个可选的
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
【问题讨论】:
【参考方案1】:变量在声明时应该有一个值,否则会显示此错误。为变量var mapRegion : MKCoordinateRegion
赋值。如果该值是在稍后阶段给出的,即,如果它取决于任何计算,则将其设置为像这样的可选 var mapRegion : MKCoordinateRegion?
但是,只要您想使用 mapRegion 值,就必须通过放置变量名末尾的感叹号。
【讨论】:
【参考方案2】:您已声明var mapRegion : MKCoordinateRegion
,但没有任何初始化程序或默认值。要使其成为可选项,只需添加 ?
即可,因此声明:
var mapRegion : MKCoordinateRegion?
【讨论】:
以上是关于我应该为这个可选添加啥标识符?的主要内容,如果未能解决你的问题,请参考以下文章
serclet容器为每一个httpsession 对象分配一个唯一标识符,叫做啥