swift 첫 걸음


    Single View App 을 만들 때 자동으로 생성되는 swift 코드

    
    //
    //  AppDelegate.swift
    //  HelloWorld
    //
    //  Created by 컴퓨터소프트웨어학과 on 2016. 9. 29..
    //  Copyright © 2016년 sd. All rights reserved.
    //
     
    import UIKit
     
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
     
        var window: UIWindow?
     
     
        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
            // Override point for customization after application launch.
            return true
        }
     
        func applicationWillResignActive(application: UIApplication) {
            // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
            // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
        }
     
        func applicationDidEnterBackground(application: UIApplication) {
            // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
            // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
        }
     
        func applicationWillEnterForeground(application: UIApplication) {
            // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
        }
     
        func applicationDidBecomeActive(application: UIApplication) {
            // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
        }
     
        func applicationWillTerminate(application: UIApplication) {
            // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
        }
     
     
    }
     



    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    //
    //  ViewController.swift
    //  HelloWorld
    //
    //  Created by 컴퓨터소프트웨어학과 on 2016. 9. 29..
    //  Copyright © 2016년 sd. All rights reserved.
    //
     
    import UIKit
     
    class ViewController: UIViewController {
     
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }
     
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
     
     
    }
     
     
    cs


    역시 메인 함수가 없다.

    세미콜론을 안붙인다.

    자바와 같이 import

    메소드는 func 함수명() {}

    클래스의 상속은 자식: 부모 {}


    Objective-C

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    //
    //  main.m
    //  HelloWorld1
    //
    //  Created by 컴퓨터소프트웨어학과 on 2016. 9. 29..
    //  Copyright © 2016년 sd. All rights reserved.
    //
     
    #import <UIKit/UIKit.h>
    #import "AppDelegate.h"
     
    int main(int argc, char * argv[]) {
        @autoreleasepool {
            return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
        }
    }
     
    cs

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    //
    //  ViewController.h
    //  HelloWorld1
    //
    //  Created by 컴퓨터소프트웨어학과 on 2016. 9. 29..
    //  Copyright © 2016년 sd. All rights reserved.
    //
     
    #import <UIKit/UIKit.h>
     
    @interface ViewController : UIViewController
     
     
    @end
     
     
    cs

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    //
    //  ViewController.m
    //  HelloWorld1
    //
    //  Created by 컴퓨터소프트웨어학과 on 2016. 9. 29..
    //  Copyright © 2016년 sd. All rights reserved.
    //
     
    #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)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
     
    @end
     
    cs


    Objective-C와 swift 차이점

    3개의 파일 ViewController.h, ViewController.m, main.m  로 나뉜다

    Objective-C 기준으로 메인함수가 있다

    문장 끝에 세미콜론을 붙여야 한다

    자료형에 ()가 있다

    import시킬 때 #이 붙는다

    Posted by 플랑