【问题标题】:Use of unresolved identifier 'object' Xcode 6 Swift使用未解析的标识符“对象”Xcode 6 Swift
【发布时间】:2015-03-25 12:39:05
【问题描述】:

我正在尝试开发一个基于解析的社交网络,并且在尝试为每个“组”创建投票系统时遇到了问题。在我的 TableViewController 中,使用 PFQueryTableViewController,我收到错误“使用未解析的标识符‘对象’”。这是我正在处理的部分

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


    group:PFObject = self.timelineData.objectAtIndex(indexPath.row) as PFObject
    cell.groupTextView.alpha = 0
    cell.timeStampLabel.alpha = 0
    cell.UserNameLabel.alpha = 0

    cell.groupTextView.text = group.objectForKey("content") as String

    var dataFormatter:NSDateFormatter = NSDateFormatter()
    dataFormatter.dateFormat = "MM/DD/yyyy HH:mm"
    cell.timeStampLabel.text = dataFormatter.stringFromDate(group.createdAt)
    let score = object.valueForKey("count") as? Int
    cell.count.text = "\(score)"
    cell.groupTextView.text = object.valueForKey("content") as? String
    var findUser:PFQuery = PFUser.query()

    findUser.whereKey("objectId", equalTo: group.objectForKey("User").objectId)

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

            let user:PFUser = (objects as NSArray).lastObject as PFUser
            cell.UserNameLabel.text = user.username

            UIView.animateWithDuration(0.5, animations: {
                cell.groupTextView.alpha = 1
                cell.timeStampLabel.alpha = 1
                cell.UserNameLabel.alpha = 1

如果有人知道这里的问题是什么,请帮助开发人员的初学者。

从评论中添加另一个方法以确保完整性和格式:

override func objectAtIndexPath(indexPath: NSIndexPath!) -> PFObject! {
    var object : PFObject? = nil
    if(indexPath.row < self.objects.count) {
        object = self.objects[indexPath.row] as? PFObject
    }
    return object
}

【问题讨论】:

  • 哪一行导致错误,该行上object 的声明在哪里?

标签: ios xcode swift ios8 xcode6


【解决方案1】:

行: let score = object.valueForKey("count") as? Int

使用object,它在哪里声明和设置?

object 似乎在另一个方法中将广告集声明为局部变量,因此调用函数不知道。

最好的猜测是object 应该被声明为类实例的属性。

【讨论】:

  • 在此声明之前,我有这个override func objectAtIndexPath(indexPath: NSIndexPath!) -&gt; PFObject! { var object : PFObject? = nil if(indexPath.row &lt; self.objects.count){ object = self.objects[indexPath.row] as? PFObject } return object }
  • 这没有回答问题,object 是该函数的局部变量。
【解决方案2】:

有两种类似的说法

cell.groupTextView.text = group.objectForKey("content") as String

cell.groupTextView.text = object.valueForKey("content") as? String

我相信第二个是错误的来源。删除它应该没问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 2016-11-28
    • 2015-05-25
    • 2017-01-28
    相关资源
    最近更新 更多