-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
958a9d6
commit 198cbda
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# 第一章:介绍 | ||
|
||
### 一本书的第一章都是一些博大精深的东西,哈哈反正是讲了很多swift这门语言的一些基础概念和特点。这里先不说。 在以后的章节里会对应一一讲解。略略略。。。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# 内建集合类型 | ||
|
||
## 2.1数组 | ||
### 数组的可变性: | ||
#### 知识点1:数组和其他的集合一样,具有值语义,数组赋值时,这个数组的内容会被复制,如下: | ||
|
||
swift: | ||
var x = [6,6,6] | ||
var y = x | ||
y.append(6) | ||
y // [6,6,6,6] | ||
x // [6,6,6] | ||
|
||
OC: | ||
// NSMutableArray *x = [NSMutableArray arrayWithArray:@[@"1",@"2",@"3"]]; | ||
// NSMutableArray *y = x; | ||
// [y addObject:@"4"]; | ||
// NSLog(@"x=%@",x); //1,2,3,4 | ||
// NSLog(@"y=%@",y); //1,2,3,4 | ||
|
||
|
||
注:这里为嘛和OC里面不太一样,因为在swift中 Array是以struct的形式存在的。并非OC里面的class | ||
|
||
可能你会觉得这么多的复制会不会有性能上的缺点,实际上swift集合类型都使用的“写时复制”技术。 | ||
只有在复制的时候复制出来,其他时候都共享一个内部存储。后面的笔记也会告诉你如何去实现自定义类的写时复制方法。这个知识点可自行百度。 | ||
|
||
|
||
|
||
### 数组和可选值 | ||
#### 知识点1:swift中不建议我们直接使用下标去访问一个数组,如array[3] (虽然我现在的项目感觉好多都是 ̄□ ̄|| ,cell的数据源这样取应该是可以的。。) | ||
|
||
#### 知识点2: 我们可以通过swift中一些奇淫的操作去操作一个数组,如下: | ||
|
||
1.迭代除第1个元素外的数组元素:for x in array.dropFirst() | ||
2.迭代除最后5个元素外的数组元素:for x in array.dropLast(5) | ||
3.所有元素和其下标: for (idx, obj) in array.enumerated() | ||
|
||
|