In Objective C, you create separate interface (.h) and implementation (.m) files for classes. Swift no longer requires developers to do that. You can define classes in a single file (.swift) without separating the external interface and implementation.
To define a class, you use the class keyword. Here is a sample class in Swift:
class Recipe {
var name: String = ""
var duration: Int = 10
var ingredients: String[] = ["egg"]
}
Similar to Objective C, right? In the above example, we define a Recipe class with three properties including name duration and ingredients. Swift requires you to provide the default values of the properties. You’ll end up with a compilation error if the initial values are missing.
What if you don’t want to assign a default value? Swift allows you to write a question mark (?) after the type of a value to mark the value as optional.
class Recipe {
var name: String?
var duration: Int = 10
var ingredients: String[]?
}
var recipeItem = Recipe()
recipeItem.name = "Mushroom Risotto"
recipeItem.duration = 30
recipeItem.ingredients = ["1 tbsp dried porcini mushrooms", "2 tbsp olive oil", "1 onion,
chopped", "2 garlic cloves", "350g/12oz arborio rice", "1.2 litres/2 pints hot vegetable
stock", "salt and pepper", "25g/1oz butter"]
Swift allows you to subclass Objective-C classes and adopt Objective-C protocols. For example, if you have a SimpleTableViewController class that extends from UIViewController class and adopts both UITableViewDelegate and UITableViewDataSource protocols, you can still use the Objective C classes and protocols but the syntax is a bit different.
@interface SimpleTableViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
class SimpleTableViewController : UIViewController, UITableViewDelegate, UITableViewDataSource
To define a class, you use the class keyword. Here is a sample class in Swift:
class Recipe {
var name: String = ""
var duration: Int = 10
var ingredients: String[] = ["egg"]
}
Similar to Objective C, right? In the above example, we define a Recipe class with three properties including name duration and ingredients. Swift requires you to provide the default values of the properties. You’ll end up with a compilation error if the initial values are missing.
What if you don’t want to assign a default value? Swift allows you to write a question mark (?) after the type of a value to mark the value as optional.
class Recipe {
var name: String?
var duration: Int = 10
var ingredients: String[]?
}
In the above code, the name and ingredients properties are automatically assigned with a default value of nil. To create an instance of a class, just use the below syntax:
var recipeItem = Recipe()
You use the dot notation to access or change the property of an instance.
recipeItem.name = "Mushroom Risotto"
recipeItem.duration = 30
recipeItem.ingredients = ["1 tbsp dried porcini mushrooms", "2 tbsp olive oil", "1 onion,
chopped", "2 garlic cloves", "350g/12oz arborio rice", "1.2 litres/2 pints hot vegetable
stock", "salt and pepper", "25g/1oz butter"]
Swift allows you to subclass Objective-C classes and adopt Objective-C protocols. For example, if you have a SimpleTableViewController class that extends from UIViewController class and adopts both UITableViewDelegate and UITableViewDataSource protocols, you can still use the Objective C classes and protocols but the syntax is a bit different.
Objective C:
@interface SimpleTableViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>