Skip to content

Commit df23f51

Browse files
author
HaRi
committed
fix: 2017.7.12 今日更新: 1.Swift 语法补充; 2.Swift4.0 KVO, KVC;
1 parent 7aba567 commit df23f51

File tree

5 files changed

+282
-5
lines changed

5 files changed

+282
-5
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ Swift基础知识大全; Swift学习从简单到复杂; 不断地完善与更新
4242
- 协议
4343
- 扩展
4444
- 泛型
45+
- Swift语法补充
46+
- Swift4.0KVC和KVO
4547

4648
### 今日更新
47-
- 协议补充
48-
- 扩展
49-
- 泛型
49+
- Swift语法补充
50+
- Swift4.0KVC和KVO
5051

5152
## 更新中...
52-
基础语法补充: Swift 语法补充
53+
接下来开始解读 Swift项目之UIKit和Foundation相关框架!
5354

5455

5556
## Star

Swift4.0KVC和KVO/main.swift

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//
2+
// main.swift
3+
// Swift4.0KVC和KVO
4+
//
5+
// Created by 韩俊强 on 2017/7/12.
6+
// Copyright © 2017年 HaRi. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
12+
/*
13+
Swift4语法 需要在xcode9 iOS11运行
14+
*/
15+
16+
17+
/*
18+
struct 也支持 KVC
19+
*/
20+
21+
/*
22+
struct ValueType {
23+
var name:String
24+
}
25+
var object = ValueType(name: "小韩哥")
26+
let name = \ValueType.name
27+
28+
//set
29+
object[keyPath: name] = "Swift4"
30+
//get
31+
let valueOfName = object[keyPath:name]
32+
*/
33+
34+
35+
36+
/*
37+
KVO: 目前依然只有 NSObject 才支持KVO
38+
*/
39+
//注意:被观察的属性需要用dynamic修饰,否则也无法观察到。
40+
//一个好消息是不需要在对象被回收时手动 remove observer。但是这也带来了另外一个容易被忽略的事情:观察的闭包没有被强引用,需要我们自己添加引用,否则当前函数离开后这个观察闭包就会被回收了。
41+
/*
42+
class OCClass: NSObject {
43+
dynamic var name:String
44+
init(name : String) {
45+
self.name = name
46+
}
47+
}
48+
49+
50+
func TestKVO() {
51+
var swiftClass : OCClass!
52+
var ob: NSKeyValueOperator!
53+
54+
swiftClass = OCClass(name: "OC")
55+
ob = swiftClass.observe(\.name){
56+
(ob, changed) in
57+
let
58+
new = ob.name
59+
print(new)
60+
}
61+
swiftClass.name = "Swift4"
62+
}
63+
TestKVO()
64+
*/
65+
66+
67+

Swift基础语法大全.xcodeproj/project.pbxproj

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
470A721A1EFA472C00858E5A /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 470A72191EFA472C00858E5A /* main.swift */; };
1111
472594291EF8BEF60036F064 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 472594281EF8BEF60036F064 /* main.swift */; };
1212
472594341EF8C6CD0036F064 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 472594331EF8C6CD0036F064 /* main.swift */; };
13+
47359D471F15A60C0066598D /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47359D461F15A60C0066598D /* main.swift */; };
14+
47359D521F15A63A0066598D /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47359D511F15A63A0066598D /* main.swift */; };
1315
47370BD81EEE7C6800C6617B /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47370BD71EEE7C6800C6617B /* main.swift */; };
1416
474236B31EE7950700D1C9F7 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 474236B21EE7950700D1C9F7 /* main.swift */; };
1517
474236BE1EE79D9100D1C9F7 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 474236BD1EE79D9100D1C9F7 /* main.swift */; };
@@ -75,6 +77,24 @@
7577
);
7678
runOnlyForDeploymentPostprocessing = 1;
7779
};
80+
47359D421F15A60C0066598D /* CopyFiles */ = {
81+
isa = PBXCopyFilesBuildPhase;
82+
buildActionMask = 2147483647;
83+
dstPath = /usr/share/man/man1/;
84+
dstSubfolderSpec = 0;
85+
files = (
86+
);
87+
runOnlyForDeploymentPostprocessing = 1;
88+
};
89+
47359D4D1F15A63A0066598D /* CopyFiles */ = {
90+
isa = PBXCopyFilesBuildPhase;
91+
buildActionMask = 2147483647;
92+
dstPath = /usr/share/man/man1/;
93+
dstSubfolderSpec = 0;
94+
files = (
95+
);
96+
runOnlyForDeploymentPostprocessing = 1;
97+
};
7898
47370BD31EEE7C6800C6617B /* CopyFiles */ = {
7999
isa = PBXCopyFilesBuildPhase;
80100
buildActionMask = 2147483647;
@@ -399,6 +419,10 @@
399419
472594281EF8BEF60036F064 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
400420
472594311EF8C6CD0036F064 /* 内存相关 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "内存相关"; sourceTree = BUILT_PRODUCTS_DIR; };
401421
472594331EF8C6CD0036F064 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
422+
47359D441F15A60C0066598D /* Swift语法补充 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "Swift语法补充"; sourceTree = BUILT_PRODUCTS_DIR; };
423+
47359D461F15A60C0066598D /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
424+
47359D4F1F15A63A0066598D /* Swift4.0KVC和KVO */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "Swift4.0KVC和KVO"; sourceTree = BUILT_PRODUCTS_DIR; };
425+
47359D511F15A63A0066598D /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
402426
47370BD51EEE7C6800C6617B /* 属性 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "属性"; sourceTree = BUILT_PRODUCTS_DIR; };
403427
47370BD71EEE7C6800C6617B /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
404428
474236B01EE7950700D1C9F7 /* 运算符 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "运算符"; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -493,6 +517,20 @@
493517
);
494518
runOnlyForDeploymentPostprocessing = 0;
495519
};
520+
47359D411F15A60C0066598D /* Frameworks */ = {
521+
isa = PBXFrameworksBuildPhase;
522+
buildActionMask = 2147483647;
523+
files = (
524+
);
525+
runOnlyForDeploymentPostprocessing = 0;
526+
};
527+
47359D4C1F15A63A0066598D /* Frameworks */ = {
528+
isa = PBXFrameworksBuildPhase;
529+
buildActionMask = 2147483647;
530+
files = (
531+
);
532+
runOnlyForDeploymentPostprocessing = 0;
533+
};
496534
47370BD21EEE7C6800C6617B /* Frameworks */ = {
497535
isa = PBXFrameworksBuildPhase;
498536
buildActionMask = 2147483647;
@@ -765,6 +803,22 @@
765803
path = "内存相关";
766804
sourceTree = "<group>";
767805
};
806+
47359D451F15A60C0066598D /* Swift语法补充 */ = {
807+
isa = PBXGroup;
808+
children = (
809+
47359D461F15A60C0066598D /* main.swift */,
810+
);
811+
path = "Swift语法补充";
812+
sourceTree = "<group>";
813+
};
814+
47359D501F15A63A0066598D /* Swift4.0KVC和KVO */ = {
815+
isa = PBXGroup;
816+
children = (
817+
47359D511F15A63A0066598D /* main.swift */,
818+
);
819+
path = "Swift4.0KVC和KVO";
820+
sourceTree = "<group>";
821+
};
768822
47370BD61EEE7C6800C6617B /* 属性 */ = {
769823
isa = PBXGroup;
770824
children = (
@@ -1014,6 +1068,8 @@
10141068
470A72181EFA472C00858E5A /* 协议 */,
10151069
478CF0411F14579A006C0547 /* 扩展 */,
10161070
47F59D841F146116003BA564 /* 泛型 */,
1071+
47359D451F15A60C0066598D /* Swift语法补充 */,
1072+
47359D501F15A63A0066598D /* Swift4.0KVC和KVO */,
10171073
47FF0F001EDFF03A00C0ACC1 /* Products */,
10181074
);
10191075
sourceTree = "<group>";
@@ -1059,6 +1115,8 @@
10591115
470A72171EFA472C00858E5A /* 协议 */,
10601116
478CF0401F14579A006C0547 /* 扩展 */,
10611117
47F59D831F146116003BA564 /* 泛型 */,
1118+
47359D441F15A60C0066598D /* Swift语法补充 */,
1119+
47359D4F1F15A63A0066598D /* Swift4.0KVC和KVO */,
10621120
);
10631121
name = Products;
10641122
sourceTree = "<group>";
@@ -1189,6 +1247,40 @@
11891247
productReference = 472594311EF8C6CD0036F064 /* 内存相关 */;
11901248
productType = "com.apple.product-type.tool";
11911249
};
1250+
47359D431F15A60C0066598D /* Swift语法补充 */ = {
1251+
isa = PBXNativeTarget;
1252+
buildConfigurationList = 47359D4A1F15A60C0066598D /* Build configuration list for PBXNativeTarget "Swift语法补充" */;
1253+
buildPhases = (
1254+
47359D401F15A60C0066598D /* Sources */,
1255+
47359D411F15A60C0066598D /* Frameworks */,
1256+
47359D421F15A60C0066598D /* CopyFiles */,
1257+
);
1258+
buildRules = (
1259+
);
1260+
dependencies = (
1261+
);
1262+
name = "Swift语法补充";
1263+
productName = "Swift语法补充";
1264+
productReference = 47359D441F15A60C0066598D /* Swift语法补充 */;
1265+
productType = "com.apple.product-type.tool";
1266+
};
1267+
47359D4E1F15A63A0066598D /* Swift4.0KVC和KVO */ = {
1268+
isa = PBXNativeTarget;
1269+
buildConfigurationList = 47359D531F15A63A0066598D /* Build configuration list for PBXNativeTarget "Swift4.0KVC和KVO" */;
1270+
buildPhases = (
1271+
47359D4B1F15A63A0066598D /* Sources */,
1272+
47359D4C1F15A63A0066598D /* Frameworks */,
1273+
47359D4D1F15A63A0066598D /* CopyFiles */,
1274+
);
1275+
buildRules = (
1276+
);
1277+
dependencies = (
1278+
);
1279+
name = "Swift4.0KVC和KVO";
1280+
productName = "Swift4.0KVC和KVO";
1281+
productReference = 47359D4F1F15A63A0066598D /* Swift4.0KVC和KVO */;
1282+
productType = "com.apple.product-type.tool";
1283+
};
11921284
47370BD41EEE7C6800C6617B /* 属性 */ = {
11931285
isa = PBXNativeTarget;
11941286
buildConfigurationList = 47370BDB1EEE7C6800C6617B /* Build configuration list for PBXNativeTarget "属性" */;
@@ -1809,6 +1901,16 @@
18091901
DevelopmentTeam = WTB2BKP7UZ;
18101902
ProvisioningStyle = Automatic;
18111903
};
1904+
47359D431F15A60C0066598D = {
1905+
CreatedOnToolsVersion = 8.3.3;
1906+
DevelopmentTeam = WTB2BKP7UZ;
1907+
ProvisioningStyle = Automatic;
1908+
};
1909+
47359D4E1F15A63A0066598D = {
1910+
CreatedOnToolsVersion = 8.3.3;
1911+
DevelopmentTeam = WTB2BKP7UZ;
1912+
ProvisioningStyle = Automatic;
1913+
};
18121914
47370BD41EEE7C6800C6617B = {
18131915
CreatedOnToolsVersion = 8.3.3;
18141916
DevelopmentTeam = WTB2BKP7UZ;
@@ -2036,6 +2138,8 @@
20362138
470A72161EFA472C00858E5A /* 协议 */,
20372139
478CF03F1F14579A006C0547 /* 扩展 */,
20382140
47F59D821F146116003BA564 /* 泛型 */,
2141+
47359D431F15A60C0066598D /* Swift语法补充 */,
2142+
47359D4E1F15A63A0066598D /* Swift4.0KVC和KVO */,
20392143
);
20402144
};
20412145
/* End PBXProject section */
@@ -2065,6 +2169,22 @@
20652169
);
20662170
runOnlyForDeploymentPostprocessing = 0;
20672171
};
2172+
47359D401F15A60C0066598D /* Sources */ = {
2173+
isa = PBXSourcesBuildPhase;
2174+
buildActionMask = 2147483647;
2175+
files = (
2176+
47359D471F15A60C0066598D /* main.swift in Sources */,
2177+
);
2178+
runOnlyForDeploymentPostprocessing = 0;
2179+
};
2180+
47359D4B1F15A63A0066598D /* Sources */ = {
2181+
isa = PBXSourcesBuildPhase;
2182+
buildActionMask = 2147483647;
2183+
files = (
2184+
47359D521F15A63A0066598D /* main.swift in Sources */,
2185+
);
2186+
runOnlyForDeploymentPostprocessing = 0;
2187+
};
20682188
47370BD11EEE7C6800C6617B /* Sources */ = {
20692189
isa = PBXSourcesBuildPhase;
20702190
buildActionMask = 2147483647;
@@ -2402,6 +2522,42 @@
24022522
};
24032523
name = Release;
24042524
};
2525+
47359D481F15A60C0066598D /* Debug */ = {
2526+
isa = XCBuildConfiguration;
2527+
buildSettings = {
2528+
DEVELOPMENT_TEAM = WTB2BKP7UZ;
2529+
PRODUCT_NAME = "$(TARGET_NAME)";
2530+
SWIFT_VERSION = 3.0;
2531+
};
2532+
name = Debug;
2533+
};
2534+
47359D491F15A60C0066598D /* Release */ = {
2535+
isa = XCBuildConfiguration;
2536+
buildSettings = {
2537+
DEVELOPMENT_TEAM = WTB2BKP7UZ;
2538+
PRODUCT_NAME = "$(TARGET_NAME)";
2539+
SWIFT_VERSION = 3.0;
2540+
};
2541+
name = Release;
2542+
};
2543+
47359D541F15A63A0066598D /* Debug */ = {
2544+
isa = XCBuildConfiguration;
2545+
buildSettings = {
2546+
DEVELOPMENT_TEAM = WTB2BKP7UZ;
2547+
PRODUCT_NAME = "$(TARGET_NAME)";
2548+
SWIFT_VERSION = 3.0;
2549+
};
2550+
name = Debug;
2551+
};
2552+
47359D551F15A63A0066598D /* Release */ = {
2553+
isa = XCBuildConfiguration;
2554+
buildSettings = {
2555+
DEVELOPMENT_TEAM = WTB2BKP7UZ;
2556+
PRODUCT_NAME = "$(TARGET_NAME)";
2557+
SWIFT_VERSION = 3.0;
2558+
};
2559+
name = Release;
2560+
};
24052561
47370BD91EEE7C6800C6617B /* Debug */ = {
24062562
isa = XCBuildConfiguration;
24072563
buildSettings = {
@@ -3152,6 +3308,22 @@
31523308
defaultConfigurationIsVisible = 0;
31533309
defaultConfigurationName = Release;
31543310
};
3311+
47359D4A1F15A60C0066598D /* Build configuration list for PBXNativeTarget "Swift语法补充" */ = {
3312+
isa = XCConfigurationList;
3313+
buildConfigurations = (
3314+
47359D481F15A60C0066598D /* Debug */,
3315+
47359D491F15A60C0066598D /* Release */,
3316+
);
3317+
defaultConfigurationIsVisible = 0;
3318+
};
3319+
47359D531F15A63A0066598D /* Build configuration list for PBXNativeTarget "Swift4.0KVC和KVO" */ = {
3320+
isa = XCConfigurationList;
3321+
buildConfigurations = (
3322+
47359D541F15A63A0066598D /* Debug */,
3323+
47359D551F15A63A0066598D /* Release */,
3324+
);
3325+
defaultConfigurationIsVisible = 0;
3326+
};
31553327
47370BDB1EEE7C6800C6617B /* Build configuration list for PBXNativeTarget "属性" */ = {
31563328
isa = XCConfigurationList;
31573329
buildConfigurations = (
@@ -3384,6 +3556,7 @@
33843556
47F59D881F146116003BA564 /* Release */,
33853557
);
33863558
defaultConfigurationIsVisible = 0;
3559+
defaultConfigurationName = Release;
33873560
};
33883561
47FF0EFA1EDFF03A00C0ACC1 /* Build configuration list for PBXProject "Swift基础语法大全" */ = {
33893562
isa = XCConfigurationList;

Swift语法补充/main.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// main.swift
3+
// Swift语法补充
4+
//
5+
// Created by 韩俊强 on 2017/7/12.
6+
// Copyright © 2017年 HaRi. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
/*
12+
断言: 当程序发生异常时, 如果希望找到出错位置并打印一个消息, 就可以使用断言, 即通过一个全局的函数 assert
13+
assert 接受一个闭包作为其第一个参数, 第二个参数是一个字符串; 假如第一个闭包返回的是一个false, 那么这个字符串就会被打印到中控制台上, assert格式如下:
14+
*/
15+
//assert(()-> Bool, "Message")
16+
17+
// 如示例, 我们希望某个函数不为空, 如果为空则会使程序崩溃, 这时就可以使用assert, 当这个函数为空的时候, 会把后面的字符串打印到中控台, 这样就知道哪里出现问题了:
18+
//assert(someFunction() != nil, "someFunction 返回了空值! ")
19+
20+
21+
22+
/*
23+
precondition: 它和 assert 的格式类型, 也是动态的, 它会造成程序的提前终止并抛出错误信息; 前面讲过, Swift 数组的下标操作可能造成越界, 使用扩展的方式向其中怎敢爱一个方法来判断下标是否越界:
24+
*/
25+
extension Array {
26+
func ifOutBounds(index:Int){
27+
precondition((0..<endIndex).contains(index), "数组越界")
28+
print("继续执行")
29+
}
30+
}
31+
[1,2,3].ifOutBounds(index: 2) // index == 3 会被提前终止
32+
33+
34+
//最后要注意: precondition 在一般代码中并不多见, 因为它是动态的, 只会在程序运行时进行检查; 适用于那些无法在编译器确定的风险情况;
35+
36+

0 commit comments

Comments
 (0)