zoukankan      html  css  js  c++  java
  • iOS8开发~Swift(一)入门

    一、概论及Swift介绍

    iOS7刚公布多时候,苹果引入了JavaScriptCore.framework用来处理JavaScript,看到了能够接触其它编程语言的契机,使iOS程序猿不用吊死在OC这一颗树上。当但iOS8公布的时候。简直Hold不住了。新的开发语言《Swift》出现了!Swift是一种新的编程语言,基于C和OC。可用于Cocoa和Cocoa Touch编程。编写代码中充满互动性和乐趣,且语法简洁而传神,能够使应用程序执行飞快。Swift是以后iOS和OS X的项目开发语言的选择之中的一个,或在您当前的应用程序中使用,由于Swift与Objective-C能够并驾的使用,实际上Swift能够与C和OC混编。


    Xcode6下载地址:http://pan.baidu.com/s/1D3Z0i


    Swift的特点:

    1、安全性:增强了类型安全与类型判断。限制指针的直接訪问,而且自己主动管理内存。能够轻松地创建安全,稳定的软件。

    func configureLabels(labels: UILabel[]) {
        let labelTextColor = UIColor.greenColor()
        for label in labels {
            // label inferred to be UILabel
            label.textColor = labelTextColor
        }
    }

    当中:

    funckeyword是定义一个函数;

    labels: UILabel[] 的意思是labels是形參名字,UILabel[]是形參类型,表明是一个数组,数组里边元素是UILabel类型都对象。

    let labelTextColor = UIColor.greenColor() 是定义一个常量labelTextColor,曾经的[UIColor greenColor] 变成了如今的UIColor.greenColor()实现方式

    for label in labels { } 这个是一个for循环,OC的实现方式是这种  for(UILabel *label in (NSArray *)labels) {},但如今但方式更简洁了
    label.textColor = labelTextColor UILabel对象的textColor属性设置,写法上和OC没差别


    2、模块化:包含自选,泛型。元组,以及其它现代语言特性。

    通过Objective-C的启示并在此基础上改进,Swift 代码更easy读和写。

    let cities = ["London", "San Francisco", "Tokyo", "Barcelona", "Sydney"]
    let sortedCities = sort(cities) { $0 < $1 }
    if let indexOfLondon = find(sortedCities, "London") {
        println("London is city number (indexOfLondon + 1) in the list")
    }

    当中:

    let cities = ["London", "San Francisco", "Tokyo", "Barcelona", "Sydney"]  这样就定义了一个常量数组,当中元素上字符串类型,OC的字符串上@"string"。但Swift字符串和C字符串写法一样,如 "string",OC快捷方式定义数组能够使用@ ["London", "San Francisco", "Tokyo", "Barcelona", "Sydney"],但如今Swift数组实现相对更简洁了,和C数组写法一样。

    if let indexOfLondon = find(sortedCities, "London") { }就是查找一个数组中是否存在某元素了。假设返回但Index存在,就打印

    println()与NSLog但作用一样,仅仅只是NSLog(@"%d")换成了了如今println("London is city number (indexOfLondon + 1) in the list"),用 (indexOfLondon + 1) 这样的形式替换了@"%d"这样的格式化输出。



    3、功能强大:利用强大的模式匹配能够编写简单且表达性好的代码。格式化字符串清晰自然。通过Swift能够直接使用Foundation和UIKit框架。

    let cities = ["London", "San Francisco", "Tokyo", "Barcelona", "Sydney"]
    let sortedCities = sort(cities) { $0 < $1 }
    if let indexOfLondon = find(sortedCities, "London") {
        println("London is city number (indexOfLondon + 1) in the list")
    }


    4、代码互动:使用playgrounds尝试新的​​技术,分析问题。和原型用户界面。

    Playgrounds make writing Swift code incredibly simple and fun. Type a line of code and the result appears immediately.


    5、更快

    以上參考  https://developer.apple.com/swift/



    二、编写HelloWorld

    一般学不论什么一门语言,第一个项目都会写个HelloWorld。那这里也依照规矩来一个:须要下载Xcode6,能够去官网下载https://developer.apple.com/cn/。稍后我会上传到网盘。

    下载并安装完Xcode6之后。打开Xcode,而且新建一个项目(选择Single View Application模版):注意选择Language为Swift



    打开项目后,文件夹结构如图:



    看到这里,我们并不陌生。和OC项目结构一样,Main.storyboard还在。仅仅只是曾经到 .h和.m文件没有了,替换成了选择到 .wift文件。但main.m文件没有了。那曾经OC项目中UIApplicationMain哪去了 ?别着急,打开AppDelegate.swift



    愿开在这里。尽管写法变了,只是意并没有改变。对号OC代码,不难理解AppDelegate但定义形式,假设写过JAVA和JS代码。看到这里一定兴奋了,太熟悉了,但只了解OC也不是必需操心。由于非常easy理解。

    class定义一个类。当中有成员变量和方法,成员变量用var定义,方法用func定义。遮掩一个.swift文件替代了OC的 .h和.m文件,看起来非常简洁,也非常easy理解。

    项目大体结构了解了。那如今向世界问好吧:

    打开ViewController.swift

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

    OC代码中的#import <UIKit/UIKit.h>如今被更简洁的import UIKit替代,override func viewDidLoad() 重写父类方法。

    在viewDidLoad()方法中加入例如以下代码:

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

    然后编译和执行,控制台打印HelloSwift,发现了没有,连一句代码结束分号都能够不用写了。但这个iOS开发离不开UI,所以这个HelloSwift太简单了,加入一个UILabel到页面,然后展示HelloSwift吧:

    代码例如以下:

    import UIKit
    
    class ViewController: UIViewController {
                                
        override func viewDidLoad() {
            super.viewDidLoad()
            
            let label = UILabel()
            label.textColor = UIColor.blackColor();
            label.backgroundColor = UIColor.redColor();
            label.text = "HelloSwift";
            label.frame = CGRect(x:0, y:100, 320, height:44)
            
            self.view.addSubview(label)
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }

    执行效果:



    到这里,学习Swift到第一个程序算结束了!很多其它内容请点击这里

    欢迎增加群共同学习和进步:QQ群:170549973

  • 相关阅读:
    complete完成量——实例分析
    worker线程的创建与使用
    SDIO接口
    Linux中V4L2分析
    Linux系统调用分析
    ppm图片显示
    应用层与内核层错误打印函数总结
    高通Sensor驱动学习笔记
    linux中新增一个shell命令的方法
    RTC测试程序
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5176934.html
Copyright © 2011-2022 走看看