加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift3.1 Enumerations,Classes,Structures,Properties and meth

发布时间:2020-12-14 06:19:31 所属栏目:百科 来源:网络整理
导读:英文来自苹果官方文档或者部分英文社区 1 、 iOS8 新特性 IBDesignable/IBInspectable ,可以在 xib 上直接设置UI类的属性,例如,UIView.layer.borderWidth,borderColor,cornerRadius这些属xib上是不能直接设置的,但是IBDesigable/IBInspectable,利用运行

英文来自苹果官方文档或者部分英文社区

1iOS8新特性IBDesignable/IBInspectable,可以在xib上直接设置UI类的属性,例如,UIView.layer.borderWidth,borderColor,cornerRadius这些属xib上是不能直接设置的,但是IBDesigable/IBInspectable,利用运行时机制,就可以把这些属性映射到xib上了,同时我们UI类的自定义属性也可以映射上去。

2When I compile the following code I get an error "Cannot use instance member 'AddEployeeName' within property initializer,property initializers run before 'self'is available".

You can't call 'addEmployeeName.text!' within property initializers. You can however initialize 'employee' within another method such as viewDidLoad <摘自stack over flow>

3However,structure instances are always passed by value,and class instances are always passed by reference.

结构总是通过值来传递,二类实例总是通过索引来传递。

4Any properties stored by the structure are themselves value types,which would also be expected to be copied rather than referenced.The structure does not need to inherit properties or behavior from another existing type.

所有的属性被结构存取的的值是他们自己,这些一般是通过赋值而不是索引。而结构不需要继承来自其他存在类型的的属性和行为

5、Unlike Objective-C initializers,Swift initializers do not return a value

不想oc初始化方法,swift初始器并不需要一个返回值。

6You can set an initial value for a stored property within an initializer,or by assigning a default property value as part of the property’s definition.

你可以在初始器里面设置存取属性的初始值。或者通过设定一个默认值作为属性定义的一部分。

7、结果里面能写初始化方法

struct Fahrenheit {

var temperature: Double

  • init() {
  • temperature = 32.0
  • }
  • }
  • var f = Fahrenheit()

8

struct Fahrenheit {

  • var temperature = 32.0
  • }

可以设置默认值。

9、Computed properties are provided by classes,structures,and enumerations. Stored properties are provided only by classes and structures

计算属性被类,结构,以及枚举提供,存储属性被类和结构提供。

10you create an instance of a structure and assign that instance to a constant,you cannot modify the instance’s properties,even if they were declared as variable properties:

你创建一个结构常量,将这个实例赋值给另一个实例,你不能修改这个实例(结构)的属性,即使他们里面声明有变量属性。

11The same is not true for classes,which are reference types. If you assign an instance of a reference type to a constant,you can still change that instance’s variable properties.

相同的部分对于类是不正确的,类是引用类型,如果你将一个引用类型的实例赋值给一个常量,你仍能改变那个实例的变量属性。

12、you must always declare a lazy property as a variable (with the var keyword),because its initial value might not be retrieved until after instance initialization completes. Constant properties must always have a value before initialization completes,and therefore cannot be declared as lazy.

你必须总是声明一个懒加载属性作为一个变量(用var关键字),因为它的初始值也许要到实例初始化完成才被引用,常量属性在初始化之前总是有一个值,因此不需要被声明为懒加载。

13Lazy properties are also useful when the initial value for a property requires complex or computationally expensive setup that should not be performed unless or until it is needed.

懒加载属性对于那些需要复杂的或者高计算的设置的属性是有用的。这些设置会

14、in addition to properties,you can use instance variables as a backing store for the values stored in a property.Swift unifies these concepts into a single property declaration.

oc中,除了属性,你能使用作为一个支持存储的实例变量作,这些值存取在属性中,但是swift联合这些概念作为一个简单的属性声明。

15、A Swift property does not have a corresponding instance variable,and the backing store for a property is not accessed directly.

一个swift属性没有一个对应的实例变量

16、This approach avoids confusion about how the value is accessed in different contexts and simplifies the property’s declaration into a single,definitive statement.

17All information about the property—including its name,type,and memory management characteristics—is defined in a single location as part of the type’s definition.

18In addition to stored properties,classes,and enumerations can define computed properties,which do not actually store a value.

19Instead,they provide a getter and an optional setter to retrieve and set other properties and values indirectly.

20Property observers are called every time a property’s value is set,even if the new value is the same as the property’s current value.

21You don’t need to define property observers for nonoverridden computed properties,because you can observe and respond to changes to their value in the computed property’s setter

You have the option to define either or both of these observers on a property:

  • willSet is called just before the value is stored.
  • didSet is called immediately after the new value is stored.

22

  • class StepCounter {
  • var totalSteps: Int = 0 {
  • willSet(newTotalSteps) {
  • print("About to set totalSteps to (newTotalSteps)")
  • }
  • didSet {
  • if totalSteps > oldValue {
  • print("Added (totalSteps - oldValue) steps")
  • }
  • }
  • }
  • }

This is a stored property with willSet and didSet observers.

23、Global variables are variables that are defined outside of any function,method,closure,or type context. Local variables are variables that are defined within a function,or closure context.

全部变量是定义在任何函数,方法,闭包,或者类型上下文的外面。而局部变量是定义在函数,方法,闭包上下文的变量。

23、Global constants and variables are always computed lazily,in a similar manner to Lazy Stored Properties. Unlike lazy stored properties,global constants and variables do not need to be marked with the lazy modifier.

Local constants and variables are never computed lazily.

全局的属性和变量总是被懒计算的,这个有点类似懒加载的存取属性,懒加载存取属性,全局实例和变量不需要被lazy标识符标志,本地的常量和变量从没懒加载计算的。

24You can also define properties that belong to the type itself,not to any one instance of that type. There will only ever be one copy of these properties,no matter how many instances of that type you create. These kinds of properties are called type properties.

你也能通过类型自己来定义属性,不是通过这种类型的任何一个实例,不管你创建了多少个实例,这些属性仅有一份,这些就是类型属性。

25You can also define properties that belong to the type itself,no matter how many instances of that type you create. These kinds of properties are called type properties.

我试了,在swift中声明不行的。

26You define type properties with the static keyword. For computed type properties for class types,you can use the class keyword instead to allow subclasses to override the superclass’s implementation. The example below shows the syntax for stored and computed type properties。

你可以通过static关键字来定义类型属性,对于类类型(区别于结构体和枚举)的计算的类型属性,你能使用class关键字来允许子类继承父类的实现。AAAAAA

27Classes,and enumerations can also define type methods,which are associated with the type itself. Type methods are similar to class methods in Objective-C。

类结构以及枚举能定义类方法,这个跟她自己有关,类型方法有点像oc中的类方法。

28In Swift,properties are values that belong to an instance of a class,struct,or enum. They are like instance variables or member variables in other languages. (There are also class properties,which are like static variables.)(非官方文档)

在swift里,属性是属于类,结构体,枚举实例的值,他们就像其他语言的实例变量或者成员变量(也有类属性,这个像静态变量。)。

29、Variables are not associated with an object. Most variables are local variables,which are declared inside of a function and are visible only from within that function. Swift also has global variables,which are defined outside of functions and can be marked private,internal,or public.

29For this reason,in many languages,it's bad practice to mark instance variables as public(非官方文档)

由于这个原因,在许多语言,将实例变量标记为公共的是一个坏习惯。

30Swift makes properties an indirection for this reason. Swift properties can be treated as dumb values for the most part,but if you ever need to change from a stored value to a computed value or something,you can do it without changing your class's interface. That way,you don't break existing code that relies on the property.(非官方文档)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读