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

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.(非官方文档)

swift3.1 Enumerations,Classes,Structures,Properties and methods部分精华的更多相关文章

  1. ios – 如何从变量访问属性或方法?

    是否可以使用变量作为Swift中方法或属性的名称来访问方法或属性?在PHP中,您可以使用$object->{$variable}.例如编辑:这是我正在使用的实际代码:解决方法你可以做到,但不能使用“纯粹的”Swift.Swift的重点是防止这种危险的动态属性访问.你必须使用Cocoa的Key-ValueCoding功能:非常方便,它完全穿过你要穿过的字符串到属性名称的桥,但要注意:这里是龙.

  2. iOS &gt;&gt;块&gt;&gt;更改块外部的变量值

    我不是在处理一个Object并改变它,就像我的mString一样.我希望’center’属性的行为类似于myInt,因为它是直接访问的C结构,而不是指向对象的指针.我希望’backgroundColor’的行为类似于我的imstring,因为它是一个指向一个新对象的对象的指针,不是吗?

  3. ios – Xcode Bot:如何在post触发器脚本上获得.ipa路径?

    我正在使用机器人来存档iOS应用程序,我需要获取.ipa产品路径才能将其发布到我们的分发系统中.机器人设置:并使用脚本打印所有env变量,其中不包含ipa文件的路径.此外,一些变量指向不存在的目录,即:XCS_OUTPUT_DIR这里的env变量输出:除此之外,我还能够确认.ipa文件是在另一个文件夹中创建的(/IntegrationAssets//

  4. ios – 使用附加字符串本地化Info.plist变量

    我正在尝试本地化应用程序的名称,同时仍然能够根据构建配置追加字符串.所以目前它被设置为:该设置定义为:通过这种方式,我们可以为应用程序添加后缀以用于不同的beta版本.问题是,当我们尝试本地化本地化的InfoPlist.strings中的应用程序显示名称时,就像这样我们覆盖存储在Info.plist中的值,并丢失后缀字符.这有什么好办法吗?

  5. iOS – 开始iOS教程 – 变量之前的下划线?

    这是正确的还是我做错了什么?

  6. ios – 静态计算变量被多次实例化

    我有一个日期格式化程序,我试图在UITableViewCell子类中创建一个单例,所以我创建了一个这样的计算属性:问题是我不止一次看到print语句,这意味着它不止一次被创建.我已经找到了其他方法,但我很想知道这里发生了什么.有任何想法吗?解决方法您的代码段相当于只获取属性,基本上它与以下内容相同:如果你只想运行一次,你应该像定义一个惰性属性一样定义它:

  7. ios – UIApplication.delegate必须仅在主线程中使用[复制]

    我应该在主调度中的viewControllers中声明这些)变量位置声明定义了它的范围.您需要确定这些变量的范围.您可以将它们声明为项目或应用程序级别(全局),类级别或特定此功能级别.如果要在其他ViewControllers中使用这些变量,则使用公共/开放/内部访问控制将其声明为全局或类级别.

  8. ios – 无法理解Objective-C块文档

    为什么localVariable“按价值使用?”>如果我在第二个例子中将__block存储类型添加到localVariable,我错误地假设该块关闭了变量,所以它将它保留在堆中直到块被释放?解决方法Howexactlyisoneexample“accessedbyreference”whiletheotheroneisaccessedbyvariable?self是当前正在执行找到块的方法的对象.强引用只是意味着对象的保留计数增加.IfIaddthe__blockstoragetypetolocalVar

  9. ios – 为BOOL变量编写getter和setter

    显然,使用obj-c,通常没有理由编写getter和setter(感谢有用的mr@synthesize).所以现在,需要做到这一点,我遇到了一个我不知道如何编写它们的问题.:p我敢肯定我可能不会以正确的方式解决我的问题–只是将我的对象子类化得更容易–但我正在尝试编写类别代码以添加属性,因为(在开头)它更快,因为我想学习如何在我的应用程序中使用类别代码.我有这个:我在setter中没有if查询就试过

  10. ios – 为什么不保留__block变量(在非ARC环境中)?

    我正在阅读__blockvariables上的文档,并考虑我使用__block的情况.对我来说,似乎我需要两种情况:>在块中使用时将变量标记为读写>在块内引用self时避免保留周期从表面上看,这两件事似乎并不相关.我认为__block变量没有被保留为更多的技巧我需要记住避免保留周期的特定用例.我想知道,为什么不能保留它们是否有更重要的建筑理由?

随机推荐

  1. Swift UITextField,UITextView,UISegmentedControl,UISwitch

    下面我们通过一个demo来简单的实现下这些控件的功能.首先,我们拖将这几个控件拖到storyboard,并关联上相应的属性和动作.如图:关联上属性和动作后,看看实现的代码:

  2. swift UISlider,UIStepper

    我们用两个label来显示slider和stepper的值.再用张图片来显示改变stepper值的效果.首先,这三个控件需要全局变量声明如下然后,我们对所有的控件做个简单的布局:最后,当slider的值改变时,我们用一个label来显示值的变化,同样,用另一个label来显示stepper值的变化,并改变图片的大小:实现效果如下:

  3. preferredFontForTextStyle字体设置之更改

    即:

  4. Swift没有异常处理,遇到功能性错误怎么办?

    本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请发送邮件至dio@foxmail.com举报,一经查实,本站将立刻删除。

  5. 字典实战和UIKit初探

    ios中数组和字典的应用Applicationschedule类别子项类别名称优先级数据包contactsentertainment接触UIKit学习用Swift调用CocoaTouchimportUIKitletcolors=[]varbackView=UIView(frame:CGRectMake(0.0,0.0,320.0,CGFloat(colors.count*50)))backView

  6. swift语言IOS8开发战记21 Core Data2

    上一话中我们简单地介绍了一些coredata的基本知识,这一话我们通过编程来实现coredata的使用。还记得我们在coredata中定义的那个Model么,上面这段代码会加载这个Model。定义完方法之后,我们对coredata的准备都已经完成了。最后强调一点,coredata并不是数据库,它只是一个框架,协助我们进行数据库操作,它并不关心我们把数据存到哪里。

  7. swift语言IOS8开发战记22 Core Data3

    上一话我们定义了与coredata有关的变量和方法,做足了准备工作,这一话我们来试试能不能成功。首先打开上一话中生成的Info类,在其中引用头文件的地方添加一个@objc,不然后面会报错,我也不知道为什么。

  8. swift实战小程序1天气预报

    在有一定swift基础的情况下,让我们来做一些小程序练练手,今天来试试做一个简单地天气预报。然后在btnpressed方法中依旧增加loadWeather方法.在loadWeather方法中加上信息的显示语句:运行一下看看效果,如图:虽然显示出来了,但是我们的text是可编辑状态的,在storyboard中勾选Editable,再次运行:大功告成,而且现在每次单击按钮,就会重新请求天气情况,大家也来试试吧。

  9. 【iOS学习01】swift ? and !  的学习

    如果不初始化就会报错。

  10. swift语言IOS8开发战记23 Core Data4

    接着我们需要把我们的Rest类变成一个被coredata管理的类,点开Rest类,作如下修改:关键字@NSManaged的作用是与实体中对应的属性通信,BinaryData对应的类型是NSData,CoreData没有布尔属性,只能用0和1来区分。进行如下操作,输入类名:建立好之后因为我们之前写的代码有些地方并不适用于coredata,所以编译器会报错,现在来一一解决。

返回
顶部