This example here demonstrates the use of classes and structures and how failable intializers work
use a simple playground file to run this....
use a simple playground file to run this....
import UIKit
enum BreadType {
case thincrust
case doublecheese
}
enum Topping{
case doublecheese
case cheesepatty
case margerita
}
enum Size
{
case sevenInch
case twelveInch
case fifteenInch
}
struct Location
{
let latitude:Double;
let longitude:Double;
mutating func isValidLocation()->Bool
{
return true;
}
}
struct Contact
{
var location :Location;
let name : String;
let phoneNumber : NSInteger;
let address : String;
}
class Order
{
var contact : Contact
let size : Size
let bread : BreadType
let toppings : [Topping]
let amount : NSInteger = 0
init?(ContactDetails:Contact, size:Size, bread:BreadType, toppings:[Topping])
{
self.contact = ContactDetails;
self.size = size;
self.bread = bread;
self.toppings = toppings;
if contact.location.isValidLocation()
{
}
else
{
return nil;
}
}
var isValidLocation:Bool{
return true;
}
}
class PizzaOrder
{
func order(){
let currentlocation = Location(latitude: 21.923, longitude: 43.234)
let contact = Contact(location: currentlocation, name: "tesss", phoneNumber: 3343434334, address: "bangalore")
print(contact)
if let pizzaOrder = Order(ContactDetails: contact, size: Size.sevenInch, bread: BreadType.thincrust, toppings: [Topping.cheesepatty,Topping.margerita]){
print(pizzaOrder.bread)
print(pizzaOrder.isValidLocation)
}
else{
print("pizza intialization failed");
}
}
}
var pizza = PizzaOrder();
pizza.order();
No comments:
Post a Comment