无法转换类型' AnyObject的值?'指定类型' BookingObject!'

时间:2016-07-03 03:17:38

标签: swift uitableview

我在swift中创建了预订对象,

import Foundation

class BookingObject {
    var bookingTitle: String = ""
    var bookingDesc: String = ""
}

然后添加数据如下

private var bookingData:NSMutableArray?
for country in countryCode  {
    bookingObj.bookingTitle = country.name
    bookingObj.bookingDesc = country.name
    bookingData?.addObject(bookingObj)
}

并且在检索值

时出现问题
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    /* error found that line */
    let bookingObj:BookingObject! = bookingData?.objectAtIndex(indexPath.row)
    let cell = bookingTable.dequeueReusableCellWithIdentifier("BookCell", forIndexPath: indexPath) as! BookCell
    cell.lbAppointmentTitle.text = bookingObj.bookingTitle
    return cell   
}

请让我知道我错误地泄露了这个错误

  

无法转换类型' AnyObject的值?'到指定的类型   ' BookingObject'!

2 个答案:

答案 0 :(得分:0)

使用结构和CustomStringConvertible协议:

struct Booking : CustomStringConvertible {
    let title : String
    var description : String { return title }
}

声明bookings属性,如下所示:

private var bookings : [BookingObject] = []

添加以下内容:

for country in countryCode {
    bookings(Booking(title: country.name))
}

获取您的商品:

let booking = bookings[indexPath.row]

答案 1 :(得分:0)

Kametrixom的回答将起作用,而且你应该完整地做。您的具体问题是数组的声明,该声明应为[BookingObject]。您需要进行其他更改,因为类对象具有引用语义,并且您只有其中一个。