我们说我有
struct cat {
var paws: int
var name: string
var breed: string
}
我如何将实例转换为新的目标控制器?特别是,这个实例从一个数组到一个新的DC?
prepare for segue
{
if segue.identifier == "segue"
var nextVC = segue.desitnationviewcontroller as ...
nextvc.instance = ?
}
答案 0 :(得分:1)
您可以像这样分配:
let someCat = cat(paws: 4, name: "Kitty", breed: "Unknown")
let arrayCat = [cat(paws: 5, name: "Mutant", breed: "Unknown"),
cat(paws: 4, name: "John", breed: "Doe")]
var nextVC = segue.desitnationviewcontroller as SomeViewController
nextVC.somePropertyName = someCat // or arrayCat if you're using an array
在SomeViewController
中,您必须拥有类型为cat
的媒体资源,您才能分配它。例如:
class SomeViewController: UIViewController {
var somePropertyName: cat? // [cat]() if it's an array of type cat
}
另外,为方便起见,我添加了Apple的Swift文档链接。链接为here。