类型没有成员名称“objectForKey”并且使用未解析的标识符
Posted
技术标签:
【中文标题】类型没有成员名称“objectForKey”并且使用未解析的标识符【英文标题】:Type does not have a member names 'objectForKey' and use of unresolved identifiers 【发布时间】:2015-03-08 15:07:12 【问题描述】:使用 swift/parse 尝试在下表视图控制器中填充自定义单元格。 pfquery 代码似乎运行良好,但是当我尝试使用数据来填充 cell.something.text 应该返回的结果时,我收到错误,表明该类型没有名为“objectForKey”的成员并使用未解析的标识符。这些错误都具体发生在覆盖 func tableView(tableView..cellForRowAtIndexPath....
import UIKit
class TimeLineTableViewController: UITableViewController
var timelineData:NSMutableArray = NSMutableArray()
override init(style: UITableViewStyle)
super.init(style: style)
// Custom initialization
required init(coder aDecoder: NSCoder)
super.init(coder: aDecoder)
func loadData()
timelineData.removeAllObjects()
//let predicate = NSPredicate(format: PFuser = PFUser.current)
var findTimelineData:PFQuery = PFQuery(className: "event")
//findTimelineData.whereKey(PFUser.self, equalTo: PFUser.currentUser())
findTimelineData.findObjectsInBackgroundWithBlock
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil
// The find succeeded.
println("Successfully retrieved \(objects.count) scores.")
// Do something with the found objects
if let objects = objects as? [PFObject]
for object in objects
self.timelineData.addObject(object)
println(object.objectId)
let array:NSArray = self.timelineData.reverseObjectEnumerator().allObjects
self.timelineData = array as NSMutableArray
self.tableView.reloadData()
else
// Log details of the failure
println("Error: \(error) \(error.userInfo!)")
override func viewDidLoad()
super.viewDidLoad()
// 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 timelineData.count
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
let cell:TimeLineTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TimeLineTableViewCell
let event:PFObject = self.timelineData.objectAtIndex(indexPath.row) as PFObject
cell.eventLabel.alpha = 0
cell.dateLabel.alpha = 0
cell.minutesLabel.alpha = 0
cell.eventLabel.text = Category.objectForKey("content") as String
cell.minutesLabel.text = duration.objectForKey
var dataFormatter:NSDateFormatter = NSDateFormatter()
dataFormatter.dateFormat = "yyyy-MM-dd HH:mm"
cell.dateLabel.text = dataFormatter.stringFromDate(category.createdAt)
var findRecorder:PFQuery = PFUser.query()
findRecorder.whereKey("objectId", equalTo: event.objectForKey(user).objectId)
findRecorder.findObjectsInBackgroundWithBlock
(objects:[AnyObject]!, error:NSError!)->Void in
if error == nil
let user:PFUser = (objects as NSArray).lastObject as PFUser
UIView.animateWithDuration(0.5, animations:
cell.eventLabel.alpha = 1
cell.dateLabel.alpha = 1
cell.minutesLabel.alpha = 1
)
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.
*/
【问题讨论】:
您的代码似乎无效,未定义duration
并且在尝试使用 objectForKey
时未发送任何参数
【参考方案1】:
您的问题是您使用的变量没有在代码中的任何位置初始化:
cell.minutesLabel.text = duration.objectForKey
^
cell.dateLabel.text = dataFormatter.stringFromDate(category.createdAt)
^
您永远不会在代码中初始化 duration
或 category
。所以你不能访问它。您首先需要对其进行初始化。
我也不确定,但看起来你没有导入 Parse 框架(也许你做了,但它不在你提供的代码中)
所以你需要先导入它:
import Parse
【讨论】:
感谢您的回复并了解您的观点。我是 Xcode 的新手并使用解析,在解析中找不到关于如何启动我认为是 PFObject 的单个变量的文档。对此有什么建议,你会建议代码是什么样的? 我试过了:var category = PFObject(className:"Event") - 没有成功。以上是关于类型没有成员名称“objectForKey”并且使用未解析的标识符的主要内容,如果未能解决你的问题,请参考以下文章
“无法使用实例引用访问成员,而是使用类型名称来限定它”,并且我使用类型名称
NSDictionary objectForKey 随机回答