![](/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.