iOS 10 Programming Fundamentals with Swift 学习笔记 0
1,a single statement can be broken into multiple lines ,For example, after an opening parenthesis is a good place:
print(
"world")
2,Swift is a compiled language, the syntax of message-sending is dot-notation. every noun is an object, and every verb is a message.
3,An object type can be extended in Swift, meaning that you can define your own messages on that type. For example, you can’t normally send the say- Hello message to a number. But you can change a number type so that you can:
extension Int {
func sayHello() {
print("Hello, I'm \(self)")
}
}
1.sayHello() // outputs: "Hello, I'm 1"
4, what “everything is an object” really means. ?
In Swift, then, 1 is an object. In some languages, such as Objective-C, it clearly is not; it is a “primitive” or scalar built-in data type. So the distinction being drawn here is between object types on the one hand and scalars on the other. In Swift, there are no scalars; all types are ultimately object types. That’s what “everything is an object” really means.
Swift has three kinds of object type: classes, structs, and enums.
5,Variables : A variable is a name for an object. Technically, it refers to an object;
let one =1
var two = 2
one = two //compile error
The two kinds of variable declaration differ in that a name declared with let cannot have its value replaced. A variable declared with let is a constant; its value is assigned once and stays.
Variables literally have a life of their own — more accurately, a lifetime of their own. As long as a variable exists, it keeps its value alive.
6,Functions : A function is a batch of code that can be told, as a batch, to run.
7,A namespace is a named region of a program.
8,The top-level namespaces are modules.
9. self. an instance needs a way of sending a message to itself. This is made possible by the magic word self.
It turns out that every use of the word self I’ve just illustrated is completely optional. You can omit it and all the same things will happen 。
The reason is that if you omit the message recipient and the message you’re sending can be sent to self, the compiler supplies self as the message’s recipient under the hood.
10,private: Supose I want a Dog instance itself to be able to change self.whatADogSays. Then whatADogSays has to be a var; otherwise, even the instance itself can’t change it. Also, suppose I don’t want any other object to know what this Dog says, except by calling bark or speak. Even when declared with let, other objects can still read the value of whatADogSays. Maybe I don’t like that. To solve this problem, Swift provides the private keyword.
11,If you’re ignoring a function call result deliberately, you can silence the compiler warning by assigning the function call to _ (a variable without a name) — for example, _ = sum(4,5). Alternatively, if the function being called is your own, you can prevent the warning by marking the function declaration with @discardableResult.
12,Function Signature :(Int, Int) -> Int 。A function’s signature is, in effect, its type — the type of the function. The signature of a function must include both the parameter list (without parameter names) and the return type, even if one or both of those is empty;
13,
func echo(string s:String, times n:Int) -> String {
var result = ""
for _ in 1...n { result += s}
return result
}
In the body of that function, there is now no times variable available; times is purely an external name, for use in the call. The internal name is n, and that’s the name the code refers to.
14, Default Parameter Values : To specify a default value in a function declaration, append = and the default value after the parameter type:
class Dog {
func say(_ s:String, times:Int = 1) {
for _ in 1...times {
print(s)
}}
}
15, To indicate in a function declaration that a parameter is variadic, follow it by three dots, like this:
func sayStrings(_ arrayOfStrings:String ...) {
for s in arrayOfStrings { print(s) }
}
16, The default separator: (for when you provide multiple values) is a space, and the default terminator: is a newline; you can change either or both:
print("Manny", "Moe", separator:", ", terminator:", ")
print("Jack")
// output is "Manny, Moe, Jack" on one line
17, Modi able Parameters ,In the body of a function, a parameter is essentially a local variable. By default, it’s a variable implicitly declared with let.
You can’t assign to it
func say(_ s:String, times:Int, loudly:Bool) {
loudly = true // compile error
}
If your code needs to assign to a parameter name within the body of a function, declare a var local variable inside the function body and assign the parameter value to it;
func removeCharacter(_ c:Character, from s:String) -> Int {
var s = s
var howMany = 0
while let ix = s.characters.index(of:c) {
s.remove(at:ix)
howMany += 1 }
return howMany
}
this change didn’t affect the original string
If we want our function to alter the original value of an argument passed to it, we must do the following:
The type of the parameter we intend to modify must be declared inout.
When we call the function, the variable holding the value we intend to tell it to
modify must be declared with var, not let.
Instead of passing the variable as an argument, we must pass its address. This is
done by preceding its name with an ampersand (&). Our removeCharacter(_:from:) now looks like this:
func removeCharacter(_ c:Character, from s: inout String) -> Int {
var howMany = 0while let ix = s.characters.index(of:c) {
s.remove(at:ix)howMany += 1 }
return howMany
}
let c = UIColor.purple
var r : CGFloat = 0
var g : CGFloat = 0
var b : CGFloat = 0
var a : CGFloat = 0
c.getRed(&r, green: &g, blue: &b, alpha: &a)
func popoverPresentationController(
_ popoverPresentationController: UIPopoverPresentationController,
willRepositionPopoverTo rect: UnsafeMutablePointer<CGRect>,
in view: AutoreleasingUnsafeMutablePointer<UIView>) {
view.pointee = self.button2
rect.pointee = self.button2.bounds
}
iOS 10 Programming Fundamentals with Swift 学习笔记 0的更多相关文章
- Swift学习笔记(一)搭配环境以及代码运行成功
原文:Swift学习笔记(一)搭配环境以及代码运行成功 1.Swift是啥? 百度去!度娘告诉你它是苹果最新推出的编程语言,比c,c++,objc要高效简单.能够开发ios,mac相关的app哦!是苹 ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第六章:在Direct3D中绘制
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第六章:在Direct3D中绘制 代码工程地址: https://gi ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十三章:角色动画
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十三章:角色动画 学习目标 熟悉蒙皮动画的术语: 学习网格层级变换 ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十二章:四元数(QUATERNIONS)
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十二章:四元数(QUATERNIONS) 学习目标 回顾复数,以及 ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十一章:环境光遮蔽(AMBIENT OCCLUSION)
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十一章:环境光遮蔽(AMBIENT OCCLUSION) 学习目标 ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十章:阴影贴图
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十章:阴影贴图 本章介绍一种在游戏和应用中,模拟动态阴影的基本阴影 ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十八章:立方体贴图
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十八章:立方体贴图 代码工程地址: https://github.c ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十五章:第一人称摄像机和动态索引
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十五章:第一人称摄像机和动态索引 代码工程地址: https://g ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十四章:曲面细分阶段
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十四章:曲面细分阶段 代码工程地址: https://github. ...
随机推荐
- max of 直线划平面
在一个无限延伸平面上有一个圆和n条直线,这些直线中每一条都在一个圆内,并且同其他所有的直线相交,假设没有3条直线相交于一点,试问这些直线最多将圆分成多少区域. Input 第一行包含一个整数T,(0& ...
- 基于Protostuff实现的Netty编解码器
在设计netty的编解码器过程中,有许多组件可以选择,这里由于咱对Protostuff比较熟悉,所以就用这个组件了.由于数据要在网络上传输,所以在发送方需要将类对象转换成二进制,接收方接收到数据后,需 ...
- fetch跨域浏览器请求头待研究
fetch('https://wwww.baidu.com', {headers: { "Access-Control-Allow-Origin": "*", ...
- (转)Windows10下的docker安装与入门 (一)使用docker toolbox安装docker
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相互之间不会有任何 ...
- SQL SEVER 开窗函数总结
作为一名刚刚入门的开发人员,要学的东西很多很多,有些无从下手.秉着“问题是病.技术是药.对症下药”的原则,将工作中遇到的问题所需的技术进行梳理.归纳和总结. 一.什么是开窗函数 首先,什么是开窗函数, ...
- 小程序一个大盒子里面的盒子内容居中对其显示wxss写法
对小程序研究感兴趣的可加(交流QQ群:604788754)入群联系群主可得到小程序教学资源. 这个案例只是想展示效果,内容部分未进行for循环绑定处理: WXML: <view class=&q ...
- c# 使用Renci.SshNet.dll操作SFTP总结
1.操作类 /// <summary> /// SFTP操作类 /// </summary> public class SFTPHelper { #region 字段或属性 p ...
- postman安装
安装包下载下来,解压缩到你喜欢的位置. 打开 Chrome 浏览器的「扩展程序」 点击「加载已解压的扩展程序...」按钮,找到你刚刚下载的安装包的位置,点击确定. 你去看看 Windows 的开始菜单 ...
- java无锁化编程一:目录
假设我们用netty做服务,当接受到网络传输的码流,我们通过某种手段将这种传输数据解析成了熟悉的pojo,那这些pojo该如何进一步处理? 比如游戏中的抢购.场景业务等,对处理那种高并发的业务场景,如 ...
- HTML的块级元素和行内元素
行内元素一般是内容的容器,而块级元素一般是其他容器的容器.一般情况下,行内元素只能包含内容或者其它行内元素,宽度和长度依据内容而定,不可以设置,可以和其它元素和平共处于一行:而块级元素可以包含行内元素 ...