![](/img/trans.png)
[英]Cannot assign value of type '()' to type '[customObject]' - while in a completion handler
[英]Cannot assign value of type while passing an object
模态课
class Trip: NSObject {
var tripTitle: String
var tripSummary: String
static var trips = [Trip]()
init(tripTitle: String, tripSummary: String) {
self.tripTitle = tripTitle
self.tripSummary = tripSummary
}
class func addTrip(tripTitle: String, tripSummary: String){
let t = Trip(tripTitle: tripTitle, tripSummary: tripSummary)
trips.append(t)
}
}
在控制器中
@IBAction func previewButtomPressed(sender: UIButton) {
let tripTitle = tripTitleTxt.text!
let tripSummary = tripSummaryTxt.text!
Trip.addTrip(tripTitle, tripSummary: tripSummary)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowTripPreviewTableViewController" {
let tripPreviewTableViewController = segue.destinationViewController as! TripPreviewTableViewController
tripPreviewTableViewController.trip = Trip //here I am getting an error
}
}
错误
Cannot assign value of type 'Trip.Type' to type 'Trip'
tripPreviewTableViewController
var trip : Trip!
我不知道您的tripPreviewTableViewController.trip
是什么类型,但对我来说更有意义:
tripPreviewTableViewController.trip = Trip.trips
编辑
有两种方法:
1)您将tripPreviewTableViewController.trip
的类型tripPreviewTableViewController.trip
为[Trip]?
。 分配值时,您可以执行tripPreviewTableViewController.trip = Trip.trips
2)您可以在tripPreviewTableViewController
内部访问Trip
类。
在你的情况,因为trips
是一个静态变量 ,你不需要通过SEGUE通过什么。
只需在目标视图中需要时查询Trip.trips
( TripPreviewTableViewController
)
正如其他人指出的那样,您不能将Class类型分配给表示该类实例的变量,因为Class不是实例。
但是,如果您的课程代表旅行,那么为什么要上这个课:
有一个静态类变量来保存旅行数组?
有一个创建新行程并将其附加到行程数组的类方法?
如果您的类旨在表示单个行程,则它应仅封装引用实例行程的属性和方法。
似乎您还试图滥用该类作为“全局”模型,该模型恰好包含一系列行程。 如果您想跟踪旅行集合,如果将Trip
数组存储在Trip
类之外,这将使您的代码(对您和其他人)更容易理解和维护。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.