//事例1:
//some的用法
//彩蛋協(xié)議
protocol SurpriseEgg{
? ? associatedtype ContentType
? ? var content:ContentType{get}
}
//定義一個彩蛋類,
struct egg:SurpriseEgg{
? ? var content:String
}
//抽獎
//返回遵守SurpriseEgg的類,但是只能返回一種類型,下面注釋的返回是不對的.編譯時確定類型
/*
?if(條件){
? ? return 類型1;
?}else{
? ? return 類型2;
?}
?*/
//其實這里直接返回egg類型就行了,此處只是為了說明some
func pickEgg() -> some SurpriseEgg{
? ? var arr = [egg]();
? ? let n = 6;
? ? for i in 1...n{
? ? ? ? let e = egg(content:"\(i)號蛋");
? ? ? ? arr.append(e);
? ? }
? ? return arr[Int(arc4random())%n]
}
//let oneEgg =? pickEgg();
//print(oneEgg.content);
//事例2
//轉自:http://www.cocoachina.com/cms/wap.php?action=article&id=27048
func makeInt() ->some Equatable{
? ? return arc4random()%5;
}
print(makeInt());
//這是不行的 返回類型不統(tǒng)一
//func makeInt() -> some Equatable{
//? ? if(Bool.random()){
//? ? ? ? return 5
//? ? }else{
//? ? ? ? return "string"
//? ? }
//}