1. 程式人生 > >swift 第一個IOS應用程式

swift 第一個IOS應用程式

import UIKit

@UIApplicationMain

//class swift 中是宣告一個類,在IOS專案中AppDelegate原來oc中的AppDelegate,應用程式的入口物件

class AppDelegate:UIResponder, UIApplicationDelegate

 {

  /*

    var 宣告變數關鍵字

    window 是變數名

    UIWindow 變數型別

    ? 可選型別在這裡理解為空(nil)即可

   */

//宣告一個全域性變數

   var window: UIWindow?

   /*

    關於

swift 中變數和常量:

    變數

    var 宣告變數關鍵字

    var 宣告沒有型別,在變數的名字後面可以指定型別

    如:

    var i:Int = 3; //  宣告一個int型別的變數,變數名字為 i變數的值為 3

    常量:

    let 常量宣告關鍵字

    let 宣告沒有型別,在變數的名字後面可以指定型別,常量的值是不可以改變的

    如:

    let d:Double =3.1415926;

    d=3.5  //錯誤寫法,因為常量的值是不可以改變的

    */

   /*

    函式:

    swift 函式特點

1)函式的引數中有標籤(OC中的方法簽名)

2)函式的返回值在函式的尾部用指標符號(箭頭)指向返回值型別

3)函式宣告關鍵字:func

    */

//第一個執行的入口函式,IOS生命週期那幾個函式,可能會略有不同,你懂得,不懂後面說

   func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool

    {

//UIWindow() 建立一個UIWindow物件 引數為 這個UIWindowframe,下面我細說

       self.window =UIWindow(frame: UIScreen.mainScreen().bounds)

// Override point for customization after application launch.

// ! 的意思是允許window==nil 時候執行,但是window==nil程式執行崩潰

self.window!.makeKeyAndVisible()

// 宣告一個color 常量(color 是一個物件) UIColor 類呼叫redCorlor()類方法

       let color = UIColor.redColor();

//設定self.window的背景顏色

       self.window!.backgroundColor = color;

       //輸出

println("Hellowrold IOS第一個專案");

       /*

        關於輸出:

        swift 的輸出用 println 

        輸出一個字串Hellowrold 

        println("Hellowrold");

        輸出一個變數的值如:var f = 30.5

        var f = 30.5

        println("f=\(f)");

        */

       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:.

    }

}