我在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'!
答案 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]
。您需要进行其他更改,因为类对象具有引用语义,并且您只有其中一个。