LoginSignup
7
7

More than 5 years have passed since last update.

swiftを使ってみた(2)

Last updated at Posted at 2014-07-03

はじめに

比較学習のためにこういうアプリを作ってみます。
プロパティとアクションが含まれた基本機能を確認出来る最小限のアプリのつもりです。

00_goal-image.png

画面のデザイン

基本的に今までのデザイナと変わりませんが、デザイナの表示サイズがデバイス依存ではなくなっています。
デバイス依存等は、レイアウト設定で対応してください、ということでしょう。
深くは追求せず、今まで通りラベルとボタンを設定します。

画面のデザインとソースとの関連づけ

ここも今までと全く同じでした。
各コントローラーをクリックしてからCtrl+ドラッグで、ソースにドロップすることでプロパティとアクションが設定できます。

swiftでの実装

すべて実装したコードはこちら(swiftファイル)。
javaのように1つのファイルですべて表現されています。

ViewController.swift
import UIKit

class ViewController: UIViewController {

    @IBOutlet var label_Hello : UILabel

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWaring() {
        super.func didReceiveMemoryWaring()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func pushButton(sender : AnyObject) {
        label_Hello.text = "Hello"
    }
}

Objective-Cでの実装

すべて実装したコードはこちら(h&mファイル)
全く同じ内容です。

ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UILabel *label_Hello;


@end
ViewController.m
#import "ViewController.h"

@interface ViewController ()

@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWaring {
    [super didReceiveMemoryWaring];
    // Dispose of any resources that can be recreated.
}

- (IBAction)pushButton:(id)sender {
    self.label_Hello.text = "Hello";
}

@end

swift と Objective-Cでの相違点

どちらも実装しての相違点を挙げてみると、、、

  • メソッドの処理が「[インスタンス メソッド] 」ではなくなった
    • これの意味が全く分からなくなってやめた人も多いと思います。。。
  • クラスの宣言、範囲指定が「{ }」になった
    • @interface@end」ではなくなった
    • 実際、「@interface@end」がhファイルやmファイルに何か所もあって戸惑ってた人も居たと思います。。。
  • hファイルが不要(swiftファイルのみで表現)
  • 行の区切りに「;」 が不要

かなりjavascript風にわかりやすい表現になったので使いやすくなったと思います。

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7