6. いつもの構文 in Swift
• Controller class definition
• Super class / protocol method
• Protocol method’s parameter
• IBOutlet, IBAction
• AnyObject and Type Casting
7. Controller class definition
// Super Class
@interface MasterViewController : UITableViewController {}
!
// Protocol(s)
@interface LoveBeerListViewController : UIViewController
<UITableViewDataSource, UITableViewDelegate> {}
// Super Class
class MasterViewController: UITableViewController {}
!
// Protocol(s)
class LoveBeerListViewController: UIViewController,
UITableViewDelegate, UITableViewDataSource {}
Objective-C
Swift
8. Controller class definition
// Super Class
class MasterViewController: UITableViewController {}
!
// Protocol(s)
class LoveBeerListViewController: UIViewController,
UITableViewDelegate, UITableViewDataSource {}
Swift
•スーパークラス指定は : の後に。
•プロトコル定義はスーパークラス定義の後にカンマ
区切りで。
9. Super class method
- (void)viewDidLoad {}
- (void)viewWillAppear:(BOOL)animated {}
- (void)prepareForSegue:(UIStoryboardSegue *)segue
sender:(id)sender{}
override func viewDidLoad() {}
override func viewWillAppear(animated: Bool){}
override func prepareForSegue(segue: UIStoryboardSegue,
sender: AnyObject?) {}
Objective-C
Swift
• Super classのmethodは override で定義。
13. AnyObject and Type Casting
LBShopTableViewCell* cell = [tableView
dequeueReusableCellWithIdentifier:@"ShopCell" forIndexPath:indexPath];
let cell = tableView.dequeueReusableCellWithIdentifier("ShopCell",
forIndexPath: indexPath) as LBShopTableViewCell
Objective-C
Swift
• Objective-Cでは
dequeueReusableCellWithIdentifier:forIndexPath:の返
値はid型。
• SwiftではAnyObject型。→ as [型]でキャストする。