美文网首页
初始Swift

初始Swift

作者: LuckTime | 来源:发表于2016-06-23 13:36 被阅读70次

    Swift在2014开发者大会上提出后。几乎每年都会更新维护,现在已经更新到2.0.
    第一次使用swift。边学swift,边做一些笔记分析。希望能快速入门这门新语言吧~

    Swift语言特点

    作为一项苹果独立发布的支持型开发语言,Swift语言的语法内容混合了Objective-C、JS、Python,语法简单、使用方便、易学,大大降低了开发者入门的门槛。同时swift语言可以与Objective-C混合使用,对于用惯了高难度Objective C语言的程序猿来说,学会这个,更不在话下!


    使用条件
    Xcode版本>=6.0
    Mac系统版本>=10.9.3

    1.常量和变量

    let 和 val 声明一个常量和变量。

    2.OC和Swift混编,头文件导入
    当在swift系统文件中创建object文件时,需要导入object-C文件才可以使用oc类中的方法。
    当创建的时候,会提醒是否创建桥接。选择时,即可
    如图:将oc头文件放进去。


    桥接Swift.png

    swift与OC语法对比分析
    3.swift 控件的创建

    // 一个完整的btn
               let SwiftBtn =  UIButton ()
            SwiftBtn.frame = CGRectMake(0, 0, 100 ,100)
            SwiftBtn.setTitle("Swift大法好", forState: UIControlState.Normal)
            SwiftBtn.setTitleColor(UIColor .blackColor(), forState: UIControlState.Normal)
            SwiftBtn.backgroundColor = UIColor .redColor()
            SwiftBtn .addTarget(self, action: #selector(ViewController.ClickBtnSelect), forControlEvents: UIControlEvents.TouchUpInside)
            self.view .addSubview(SwiftBtn)
    
    // 执行点击事件
     func ClickBtnSelect()  {
            NSLog("点击了按钮")
            print("点击了按钮")
            
        }
    

    4.控件的懒加载

      // 懒加载
        let LazyBtn = UIButton()
        LazyBtn.frame = CGRectMake(120, 0, 100, 100)
        LazyBtn.setTitle("Swift懒加载", forState: UIControlState.Normal)
        LazyBtn.setTitleColor(UIColor .blackColor(), forState: UIControlState.Normal)
        LazyBtn.backgroundColor = UIColor .redColor()
        return LazyBtn
        }()
    
     // 调用部分
      self.view .addSubview(LazyBtn)
    

    综上:Swift真是一个自上而下,简洁,易学的代码。。

    开始试着找一款APP模仿着学习吧。
    我模仿的是维尼的小熊#小日子#,因为搜到了Swift源码。

    5.访问控制的认识
    苹果在发布了Xcode 6 Bate 4后为Swift添加了新的特性–访问控制(Access Control) 并为此写了文档“The Swift Programming Language
    具体内容来源
    什么是访问控制:
    访问控制可以限定你在源文件或模块中访问代码的级别,限定哪些代码允许被访问,哪些代码不允许被访问。
    适用范围:
    整个项目。

    You can assign specific access levels to individual types (classes, structures, and enumerations), as well as to properties, methods, initializers, and subscripts belonging to those types. Protocols can be restricted to a certain context, as can global constants, variables, and functions.
    可以明确的给类、结构体、枚举、设置访问级别,也可以给属性、函数、初始化方法、基本类型、下标索引等设置访问级别。协议也可以被限定在一定的范围内使用,包括协议里的全局常量、变量和函数。

    名词解释:模块和源文件

    Modules and Source Files
    Swift’s access control model is based on the concept of modules and source files.
    A module is a single unit of code distribution—a framework or application that is built and shipped as a single unit and that can be imported by another module with Swift’simport
    keyword.
    Each build target (such as an app bundle or framework) in Xcode is treated as a separate module in Swift. If you group together aspects of your app’s code as a stand-alone framework—perhaps to encapsulate and reuse that code across multiple applications—then everything you define within that framework will be part of a separate module when it is imported and used within an app, or when it is used within another framework.
    A source file is a single Swift source code file within a module (in effect, a single file within an app or framework). Although it is common to define individual types in separate source files, a single source file can contain definitions for multiple types, functions, and so on.
    Swift中的访问控制模型基于模块和源文件这两个概念。
    模块指的是Framework或App bundle。在Swift中,可以用import关键字引入自己的工程。
    在Swift中,Frameword或App bundle被作为模块处理。如果你是为了实现某个通用的功能,或者是为了封装一些常用方法而将代码打包成Framework,这个Framework在Swift中就被称为模块。不论它被引入到某个App工程或者其他的Framework,它里面的一切(属性、函数等)都属于这个模块。
    源文件指的是Swift中的Swift File,就是编写Swift代码的文件,它通常属于一个模块。通常一个源文件包含一个类,在类中又包含函数、属性等类型。

    访问级别
    <font color=red>内容</font>
    Access Levels
    Swift provides three different access levels for entities within your code. These access levels are relative to the source file in which an entity is defined, and also relative to the module that source file belongs to.
    Public access enables entities to be used within any source file from their defining module, and also in a source file from another module that imports the defining module. You typically use public access when specifying the public interface to a framework.
    Internal access enables entities to be used within any source file from their defining module, but not in any source file outside of that module. You typically use internal access when defining an app’s or a framework’s internal structure.
    Private access restricts the use of an entity to its own defining source file. Use private access to hide the implementation details of a specific piece of functionality.
    Swift提供了三种不同的访问级别。这些访问级别相对于源文件中定义的实体,同时也相对于这些源文件所属的模块。
    Public:可以访问自己模块或应用中源文件里的任何实体,别人也可以访问引入该模块中源文件里的所有实体。通常情况下,某个接口或Framework是可以被任何人使用时,你可以将其设置为public级别。
    Internal:可以访问自己模块或应用中源文件里的任何实体,但是别人不能访问该模块中源文件里的实体。通常情况下,某个接口或Framework作为内部结构使用时,你可以将其设置为internal级别。
    Private:只能在当前源文件中使用的实体,称为私有实体。使用private级别,可以用作隐藏某些功能的实现细节。
    Public为最高级访问级别,Private为最低级访问级别。

    1. Swift语言的改变
      “确保转换”和“可失败转换”的概念现在被分为两个操作符。可失败转换现在使用as!运算符,这个!感叹号可以让代码的读者更清晰的明白本次转换可能失败并触发一个运行时错误。“as”操作符会保持向上转换(比如“someDerivedValue转换为Base”)或者类型标注(“0 转换为Int8”),它保证了转换不会失败。
      结构体和类构造器中的let不可变属性现在被规范为更加标准的通用模型:lets类型初始化后将永不会被改变或重新赋值。以前的实现是,可以在构造器中任意修改,而现在它们只允许被初始化和提供值操作。如果一个属性在声明时已经赋值,那么它会被所有的构造器认为已经含有初始值。
      从桥接Objective-C类 (NSString/NSArray/NSDictionary)到它Swift中值类型的隐式转化被移除。这将是Swift的类型系统更加简单和可预测。这意味着:
    import Foundation  
    func log(s: String) { println(x) }  
    let ns: NSString = 
    "some NSString"
     // Okay  
    log(ns) 
    // 错误  
    // "'NSString' 不能转换为 'String'"
    为了完成桥接转换,需要用显式转化符标注:
    log(ns as String) 
    // succeeds
    从Swift类型到Objective-C类型的桥接隐式转换依然被允许,比如:
    func nsLog(ns: NSString) { println(ns) }  
    let s: String = “some String”  
    nsLog(s) 
    // okay: implicit conversion from String to NSString is still permitted
    

    6.自定义Tabber

    相关文章

      网友评论

          本文标题:初始Swift

          本文链接:https://www.haomeiwen.com/subject/dnwndttx.html