Apple推出了基于Objective-C的新语言Swift. 通过实例, 我们可以很好的感受这门新语言
注意事项: 在XCode6_Beta中, 如果有中文, IDE的自动补全功能就会失效, 所以开始调试的时候可以先用英文, 后面再用中文替代.
1. 新建iOS -> Single View Application.
2. 修改AppDelegate.swift文件
1 // 2 // AppDelegate.swift 3 // UIByCode_Swift_1_HelloWorld 4 // 5 // Created by yao_yu on 14-6-18. 6 // Copyright (c) 2014 yao_yu. All rights reserved. 7 // 8 9 import UIKit 10 11 @UIApplicationMain 12 class AppDelegate: UIResponder, UIApplicationDelegate { 13 14 var window: UIWindow? 15 16 17 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { 18 self.window = UIWindow(frame: UIScreen.mainScreen().bounds) 19 // Override point for customization after application launch. 20 let window = self.window! 21 window.backgroundColor = UIColor.whiteColor() 22 window.makeKeyAndVisible() 23 24 let frame = UIScreen.mainScreen().bounds 25 let EDGE:CGFloat = 20 26 27 var labelHello = UILabel(frame: CGRectMake(EDGE, (frame.height - EDGE)/2, frame.width - EDGE * 2, EDGE)) 28 labelHello.textAlignment = NSTextAlignment.Center 29 labelHello.text = "你好,欢迎来到swift世界!" //加入中文后,XCode的自动补全功能就不出来了 30 window.addSubview(labelHello) 31 32 return true 33 } 34 35 func applicationWillResignActive(application: UIApplication) { 36 // 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. 37 // 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. 38 } 39 40 func applicationDidEnterBackground(application: UIApplication) { 41 // 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. 42 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 43 } 44 45 func applicationWillEnterForeground(application: UIApplication) { 46 // 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. 47 } 48 49 func applicationDidBecomeActive(application: UIApplication) { 50 // 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. 51 } 52 53 func applicationWillTerminate(application: UIApplication) { 54 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 55 } 56 57 58 }
3.运行