如何从可选项中获取值

时间:2018-05-09 06:46:05

标签: ios swift optional

如何从可选项中获取值?

我的模型(SearchModel

var restaurantsearchname:String?
var offerModel:OfferModel?

init(withmodel  newrestaurantModel:OfferModel) {
     offerModel = newrestaurantModel
     print(offerModel?.name)
     print(offerModel?.id)
     restaurantsearchname = offerModel?.name
     print(restaurantsearchname)
}

在我的viewController

let searchmodel = searchViewModel.datafordisplay(atindex: indexPath)
print(searchmodel)
let dataSourceModel:DataSource = DataSource(array: nil)
let viewModel:ViewModel = ViewModel(withdatasource: dataSourceModel)
let oo:SearchModel = SearchModel(withmodel: viewModel.datafordisplay(atindex: indexPath))
print(oo)

我需要打印searchmodel.offerModel.id。打印时显示为nil

但是当我在模型中打印print(offerModel?.id)时,我得到了这个值。但是不能从print(searchmodel.offerModel?.id)进入viewcontroller

如何获得价值?

0 个答案:

没有答案