swift学习---长句部分六(第一次计划看一本完整的英文资料,你是最棒的,加油)

Class and structures are general-purpose flexible constructs that become the building blocks of your program’s code.

类和结构体是人们构造代码所用的一种通用且灵活的构造体。

Unlike other programming languages , Swift does require you to create separate interface and implementation files for custom classes and structures . In swift you define a class or a structure in a single file , and the external interface to that class or structures in automatically made for other code to use.

和其他编程语言不同,swift并不要求你为自定义类和结构体去创建独立的接口和实体文件。你所要做的是在一个单一文件中定义一个类或者结构体,系统将会自动生成面向其他代码的外部接口。

 

An instance of a class traditionally known as an object .However ,Swift classes and structures are much more close in functionality than in other languages , and much of this chapter describe functionality that can apply to instance of either a class or structure type . Because of this , the more general term instance is used.

通常一个类的实力称为对象。然而在swift中,类和结构体的关系比其他语言中更加密切,本章中所讨论的大部分功能都可以用在类和结构体上。因此,我们会主要使用实例而不是对象。

Type casting enables you to check and interpret the type of a class instance at runtime.

类型转换允许在运行时检查和解释一个类实例的类型。

brand

differentiate

Resolution

Bundled up

You can drill into sub-properties , such as the width property in the resolution property of a VideoMode:

Memberwise Initializers for Structure Types.

结构体类型的成员的逐一初始化器。

Structures and Enumerations Are Value Types

结构体和枚举是值类型

Classes Are Reference Types

类是引用类型

Interlace

Indentity Operators

恒等运算符

Indentical to (====)

Not identical to (!==)

Qualifies

You can use both classes and structures to define custom data types to use as the building of your program’s code.

在你的代码中,你可以使用类和结构体来定义你的自定义数据类型。

However, structures instances are always passed by value, and class instance are always passed by reference. This means that they are suited to different kinds of tasks. As you consider the data constructs and functionality that you need for a project, decide whether each data construct should be defined as class or as a structure.

然而,结构体实例总是通过值传递,类实例总是通过引用传递。这意味这两者适用不同的任务。当你在考虑一个工程项目的数据构造和功能的时候,你需要决定每个数据构造是定义成类还是结构体。

 

encapsulate 封装

The structure‘s primary purpose is to encapsulate a few relatively simple data values.

 

结构体的主要目的是用来封装少量相关简单数据值。

Assignment and Copy Behavior for Collection Types.

集合类型的赋值和拷贝行为

 

Implement 实现

Swift’s Array and Dictionary types are implemented as structures.

Swift中数组和字典类型均以结构体的形式实现。

preempt

 

Assignment and copy behavior for dictionaries.

字典类型的赋值和拷贝行为。

 

Whenever you assign a dictionary instance to a constant or variable , or pass a Dictionary instance as an argument to a function or method call , the dictionary is copied at the point that the assignment or call takes place.

无论何时將一个字典实例赋给一个常量或者变量,或者传递给一个函数或者方法,这个字典会在赋值或者调用的时候发生拷贝。

Assignment and Copy Behaviors for Arrays

数组的赋值和拷贝行为

 

For arrayscopy only takes place when you  perform an action that has the potential to modify the length of the array . This inclues appending , inserting ,or removing items ,or removing items ,or using a ranged subscript to replace of items in the array . If and when array copying does take place , the copy behavior for an array’s contents is the same as for a dictionary’s key and values ,as described in

Assignment and Copy Behavior for Dictionaries.

对于数据来说,拷贝行为仅仅当操作有可能修改数据长度时才会发生。这种行为包括了附加、插入、删除或者使用下标范围去替换这一范围内的元素。只有当拷贝行为确要发生时,数组内容的行为规则与字典中键值相同。

Ensure that an array is unique.

确保数组的唯一性

If multiple variables current refer to the same   array , and the unshared method on one of those  variables ,the array is copies ,so that the  variable has its own independent copy of the array . However , no copying takes place if the variable is already the only reference to the array.

如果一个数组被多个变量引用,在其中的一个变量上调用unshare方法,则会拷贝此数组,此时这个变量将会有术语自己独立的数组拷贝,当数组仅被一个变量引用时,则不会有拷贝发生。

Alternatively , use the identity operator to check whether two subarrays share the same elements.

此外,我们还可以使用恒等运算符号来判断两个子数组是否共用相同的元素。

Stored properties of constant structures instance.

常量和存储属性。

Lazy stored properties

延迟存储属性

Non-trivial

Stored properties and instance variables

存储性质和实例变量

Point encapsulates an(x,y) coordinate.

点封装了一个xy坐标。

Shorthand setter Declaration

便捷setter声明

You must declare computed properties – including read-only computed properties –as variable properties with the var keyword , because their values is not fixed. The let keyword is only used for constant properties , to indicate that their values cannot be changed once they are set as part of instance initialization.

注意:必须使用var关键字定义计算属性,包括只读计算属性,因为他们的值不是固定的,let关键字只用来声明常量属性,表示初始化之后再无法修改的值。

 

Property Observers 属性监视器

 

This class might be used with input data from a pedometer or other step counter to keep track of a person’s exercise during their daily routine.

这个类用来统计人步行时的总步数,可以跟计步器或者其他日常锻炼的统计装置的输入数据配合使用。

Type property syntax

类型属性语法

Querying and setting type properties

获取和设置类型属性的值

They support the functionality of those instance , either by providing ways to access and modify instance properties , or by providing functionality related to the instance’s purpose. Instance method have exactly the same syntax as functions, as described in functions.

实例方法提供访问和修改实例属性的方法或者提供与实例目的相关的功能,并以此来支撑实例的功能。实例的方法的语法与眼熟完全一致,详情常见函数。

 

You write an instance method within the opening and closing braces of the type it belongs to.

 

实例方法要写在它所属的类型的前后大括号之间。

 

It cannot be called in isolation without an existing instance.

实例方法不能脱离于现存的实例而被调用。

Local and external parameter names for  methods.

方法的局部名称和外部参数名称

qualified   合格的、有资格的、限定的。

You don‘t need to define an external parameter

name for the first argument value , because its purpose is clear from the function name incrementBy . The second argument , however , is qualified by an external parameter name to make its purpose clear when the method is called.

你不必为第一个参数再定义一个外部变量:因为从函数名increamentby 已经能很清楚地看出它的作用。但是第二个参数,就要被一个外部参数名称所限定,以便在方法被调用时明确它的作用。

The default behavior described above mean that method definitions in Swift are written with the same grammatical style as Object-c , and are called in a natural , expressive way.

这种默认行为使上面代码意味这:在swift中定义方法使用了与object-c同样的语法风格,并且方法將以自然表达式的方式被调用。

 

You use this implicit self property to refer to the current instance within its own instance method .

你可以在一个实例的实例方法中使用这个隐藏的self属性来引用当前实例。

 

If you don’t explicitly write self , Swift assumes

that you are referring to a property or method of the current instance whenever you use a known property or method name within a method.

不论何时,只要使用一个已知的属性或者方法名称,如果你没有明确的写selfSwift假定你是指当前实例的属性或者方法。

 

In this situation , the parameter name takes precedence , and it becomes necessary to refer to the property in a more qualified way.

在这种情况下,参数名称享有优先权,并且在引用属性时必须使用一种更严格的方式。

Modifying value types from within instance method

在实例方法中修改值类型

However , if you need to modify the properties of your structure or enumeration within a particular method , you can opt in to muatating behavior for that method . The method can then mutate (that is ,change)its properties from within the method , and any changes that it makes are written back to the original structure when the method ends . The method can also assign a completely new instance to its implicit  self  property ,and this new instance will replace the existing one when the method ends.

但是,如果你确实需要在某个具体的方法中修改结构体或者枚举的属性,你可以选择变异这个方法,然后方法就可以从方法内部该改变它的属性;并且它做的任何改变在方法结束时还保留在原始结构中。方法还可以给它隐含的self属性一个全新的实例,这个新的实例在方法结束之后会替换原来的实例。

Assigning to self within a mutating method

在变异方法中给self赋值

Type Method

 类型方法

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值