[英]Cannot assign value of type while passing an object
Modal class 模态课
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)
}
}
In Controller 在控制器中
@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
}
}
error 错误
Cannot assign value of type 'Trip.Type' to type 'Trip'
tripPreviewTableViewController tripPreviewTableViewController
var trip : Trip!
I don't know what is the type of your tripPreviewTableViewController.trip
but it makes more sense for me to do: 我不知道您的tripPreviewTableViewController.trip
是什么类型,但对我来说更有意义:
tripPreviewTableViewController.trip = Trip.trips
Edit 编辑
Two ways to do it: 有两种方法:
1) You change the type of tripPreviewTableViewController.trip
to [Trip]?
1)您将tripPreviewTableViewController.trip
的类型tripPreviewTableViewController.trip
为[Trip]?
. 。 When you assign the value you can do tripPreviewTableViewController.trip = Trip.trips
分配值时,您可以执行tripPreviewTableViewController.trip = Trip.trips
2) You can access Trip
class inside your tripPreviewTableViewController
. 2)您可以在tripPreviewTableViewController
内部访问Trip
类。
In your case, as trips
is an static variable , you do NOT need to pass anything via segue. 在你的情况,因为trips
是一个静态变量 ,你不需要通过SEGUE通过什么。
Just ask for Trip.trips
when you needed in your destination view ( TripPreviewTableViewController
) 只需在目标视图中需要时查询Trip.trips
( TripPreviewTableViewController
)
As others have pointed out, you can't assign a Class type to a variable which represents an instance of that class, since the Class is not an instance. 正如其他人指出的那样,您不能将Class类型分配给表示该类实例的变量,因为Class不是实例。
However, if your class represents a trip, why does this class: 但是,如果您的课程代表旅行,那么为什么要上这个课:
have a static class variable to hold an array of trips? 有一个静态类变量来保存旅行数组?
have a class method that creates a new trip and appends it to the array of trips? 有一个创建新行程并将其附加到行程数组的类方法?
If your class is meant to represent a single trip, it should only encapsulate properties and methods that refer to an instance's trip. 如果您的类旨在表示单个行程,则它应仅封装引用实例行程的属性和方法。
It seems like you're trying to also misuse the class to serve as a "global" model which happens to hold an array of trips. 似乎您还试图滥用该类作为“全局”模型,该模型恰好包含一系列行程。 If you want to keep track of a collection of trips, it would make your code easier (for you and others) to understand and maintain, if you stored that array of trips outside the Trip
class. 如果您想跟踪旅行集合,如果将Trip
数组存储在Trip
类之外,这将使您的代码(对您和其他人)更容易理解和维护。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.