Skip to content

Commit 6e08493

Browse files
author
HaRi
committed
fix: 1.方法; 2.下标subscripts;
1 parent e09b4a7 commit 6e08493

File tree

6 files changed

+410
-2
lines changed

6 files changed

+410
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Swift基础知识大全; Swift学习从简单到复杂; 不断地完善与更新
3131
- 结构体
3232
-
3333
- 属性
34+
- 方法
35+
- 下标subscripts
3436

3537
## 更新中...
3638

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

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
47370BD81EEE7C6800C6617B /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47370BD71EEE7C6800C6617B /* main.swift */; };
1111
474236B31EE7950700D1C9F7 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 474236B21EE7950700D1C9F7 /* main.swift */; };
1212
474236BE1EE79D9100D1C9F7 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 474236BD1EE79D9100D1C9F7 /* main.swift */; };
13+
47537DA51EEF7042005F4316 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47537DA41EEF7042005F4316 /* main.swift */; };
14+
47537DB01EEF7C48005F4316 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47537DAF1EEF7C48005F4316 /* main.swift */; };
1315
47664D7A1EEE6237009ECB34 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47664D791EEE6237009ECB34 /* main.swift */; };
1416
47664D851EEE64C3009ECB34 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47664D841EEE64C3009ECB34 /* main.swift */; };
1517
47664D901EEE6ED0009ECB34 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47664D8F1EEE6ED0009ECB34 /* main.swift */; };
@@ -64,6 +66,24 @@
6466
);
6567
runOnlyForDeploymentPostprocessing = 1;
6668
};
69+
47537DA01EEF7042005F4316 /* CopyFiles */ = {
70+
isa = PBXCopyFilesBuildPhase;
71+
buildActionMask = 2147483647;
72+
dstPath = /usr/share/man/man1/;
73+
dstSubfolderSpec = 0;
74+
files = (
75+
);
76+
runOnlyForDeploymentPostprocessing = 1;
77+
};
78+
47537DAB1EEF7C48005F4316 /* CopyFiles */ = {
79+
isa = PBXCopyFilesBuildPhase;
80+
buildActionMask = 2147483647;
81+
dstPath = /usr/share/man/man1/;
82+
dstSubfolderSpec = 0;
83+
files = (
84+
);
85+
runOnlyForDeploymentPostprocessing = 1;
86+
};
6787
47664D751EEE6237009ECB34 /* CopyFiles */ = {
6888
isa = PBXCopyFilesBuildPhase;
6989
buildActionMask = 2147483647;
@@ -289,6 +309,10 @@
289309
474236B21EE7950700D1C9F7 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
290310
474236BB1EE79D9100D1C9F7 /* 数组的基本使用 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "数组的基本使用"; sourceTree = BUILT_PRODUCTS_DIR; };
291311
474236BD1EE79D9100D1C9F7 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
312+
47537DA21EEF7042005F4316 /* 方法 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "方法"; sourceTree = BUILT_PRODUCTS_DIR; };
313+
47537DA41EEF7042005F4316 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
314+
47537DAD1EEF7C48005F4316 /* 下标subscripts */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "下标subscripts"; sourceTree = BUILT_PRODUCTS_DIR; };
315+
47537DAF1EEF7C48005F4316 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
292316
47664D771EEE6237009ECB34 /* 闭包捕获值 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "闭包捕获值"; sourceTree = BUILT_PRODUCTS_DIR; };
293317
47664D791EEE6237009ECB34 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
294318
47664D821EEE64C3009ECB34 /* 枚举 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "枚举"; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -361,6 +385,20 @@
361385
);
362386
runOnlyForDeploymentPostprocessing = 0;
363387
};
388+
47537D9F1EEF7042005F4316 /* Frameworks */ = {
389+
isa = PBXFrameworksBuildPhase;
390+
buildActionMask = 2147483647;
391+
files = (
392+
);
393+
runOnlyForDeploymentPostprocessing = 0;
394+
};
395+
47537DAA1EEF7C48005F4316 /* Frameworks */ = {
396+
isa = PBXFrameworksBuildPhase;
397+
buildActionMask = 2147483647;
398+
files = (
399+
);
400+
runOnlyForDeploymentPostprocessing = 0;
401+
};
364402
47664D741EEE6237009ECB34 /* Frameworks */ = {
365403
isa = PBXFrameworksBuildPhase;
366404
buildActionMask = 2147483647;
@@ -556,6 +594,22 @@
556594
path = "数组的基本使用";
557595
sourceTree = "<group>";
558596
};
597+
47537DA31EEF7042005F4316 /* 方法 */ = {
598+
isa = PBXGroup;
599+
children = (
600+
47537DA41EEF7042005F4316 /* main.swift */,
601+
);
602+
path = "方法";
603+
sourceTree = "<group>";
604+
};
605+
47537DAE1EEF7C48005F4316 /* 下标subscripts */ = {
606+
isa = PBXGroup;
607+
children = (
608+
47537DAF1EEF7C48005F4316 /* main.swift */,
609+
);
610+
path = "下标subscripts";
611+
sourceTree = "<group>";
612+
};
559613
47664D781EEE6237009ECB34 /* 闭包捕获值 */ = {
560614
isa = PBXGroup;
561615
children = (
@@ -706,6 +760,8 @@
706760
47664D8E1EEE6ED0009ECB34 /* 结构体 */,
707761
47664D991EEE7416009ECB34 /* 类 */,
708762
47370BD61EEE7C6800C6617B /* 属性 */,
763+
47537DA31EEF7042005F4316 /* 方法 */,
764+
47537DAE1EEF7C48005F4316 /* 下标subscripts */,
709765
47FF0F001EDFF03A00C0ACC1 /* Products */,
710766
);
711767
sourceTree = "<group>";
@@ -740,6 +796,8 @@
740796
47664D8D1EEE6ED0009ECB34 /* 结构体 */,
741797
47664D981EEE7416009ECB34 /* 类 */,
742798
47370BD51EEE7C6800C6617B /* 属性 */,
799+
47537DA21EEF7042005F4316 /* 方法 */,
800+
47537DAD1EEF7C48005F4316 /* 下标subscripts */,
743801
);
744802
name = Products;
745803
sourceTree = "<group>";
@@ -870,6 +928,40 @@
870928
productReference = 474236BB1EE79D9100D1C9F7 /* 数组的基本使用 */;
871929
productType = "com.apple.product-type.tool";
872930
};
931+
47537DA11EEF7042005F4316 /* 方法 */ = {
932+
isa = PBXNativeTarget;
933+
buildConfigurationList = 47537DA81EEF7042005F4316 /* Build configuration list for PBXNativeTarget "方法" */;
934+
buildPhases = (
935+
47537D9E1EEF7042005F4316 /* Sources */,
936+
47537D9F1EEF7042005F4316 /* Frameworks */,
937+
47537DA01EEF7042005F4316 /* CopyFiles */,
938+
);
939+
buildRules = (
940+
);
941+
dependencies = (
942+
);
943+
name = "方法";
944+
productName = "方法";
945+
productReference = 47537DA21EEF7042005F4316 /* 方法 */;
946+
productType = "com.apple.product-type.tool";
947+
};
948+
47537DAC1EEF7C48005F4316 /* 下标subscripts */ = {
949+
isa = PBXNativeTarget;
950+
buildConfigurationList = 47537DB11EEF7C48005F4316 /* Build configuration list for PBXNativeTarget "下标subscripts" */;
951+
buildPhases = (
952+
47537DA91EEF7C48005F4316 /* Sources */,
953+
47537DAA1EEF7C48005F4316 /* Frameworks */,
954+
47537DAB1EEF7C48005F4316 /* CopyFiles */,
955+
);
956+
buildRules = (
957+
);
958+
dependencies = (
959+
);
960+
name = "下标subscripts";
961+
productName = "下标subscripts";
962+
productReference = 47537DAD1EEF7C48005F4316 /* 下标subscripts */;
963+
productType = "com.apple.product-type.tool";
964+
};
873965
47664D761EEE6237009ECB34 /* 闭包捕获值 */ = {
874966
isa = PBXNativeTarget;
875967
buildConfigurationList = 47664D7D1EEE6237009ECB34 /* Build configuration list for PBXNativeTarget "闭包捕获值" */;
@@ -1303,6 +1395,16 @@
13031395
DevelopmentTeam = WTB2BKP7UZ;
13041396
ProvisioningStyle = Automatic;
13051397
};
1398+
47537DA11EEF7042005F4316 = {
1399+
CreatedOnToolsVersion = 8.3.3;
1400+
DevelopmentTeam = WTB2BKP7UZ;
1401+
ProvisioningStyle = Automatic;
1402+
};
1403+
47537DAC1EEF7C48005F4316 = {
1404+
CreatedOnToolsVersion = 8.3.3;
1405+
DevelopmentTeam = WTB2BKP7UZ;
1406+
ProvisioningStyle = Automatic;
1407+
};
13061408
47664D761EEE6237009ECB34 = {
13071409
CreatedOnToolsVersion = 8.3.3;
13081410
DevelopmentTeam = WTB2BKP7UZ;
@@ -1464,6 +1566,8 @@
14641566
47664D8C1EEE6ED0009ECB34 /* 结构体 */,
14651567
47664D971EEE7416009ECB34 /* 类 */,
14661568
47370BD41EEE7C6800C6617B /* 属性 */,
1569+
47537DA11EEF7042005F4316 /* 方法 */,
1570+
47537DAC1EEF7C48005F4316 /* 下标subscripts */,
14671571
);
14681572
};
14691573
/* End PBXProject section */
@@ -1493,6 +1597,22 @@
14931597
);
14941598
runOnlyForDeploymentPostprocessing = 0;
14951599
};
1600+
47537D9E1EEF7042005F4316 /* Sources */ = {
1601+
isa = PBXSourcesBuildPhase;
1602+
buildActionMask = 2147483647;
1603+
files = (
1604+
47537DA51EEF7042005F4316 /* main.swift in Sources */,
1605+
);
1606+
runOnlyForDeploymentPostprocessing = 0;
1607+
};
1608+
47537DA91EEF7C48005F4316 /* Sources */ = {
1609+
isa = PBXSourcesBuildPhase;
1610+
buildActionMask = 2147483647;
1611+
files = (
1612+
47537DB01EEF7C48005F4316 /* main.swift in Sources */,
1613+
);
1614+
runOnlyForDeploymentPostprocessing = 0;
1615+
};
14961616
47664D731EEE6237009ECB34 /* Sources */ = {
14971617
isa = PBXSourcesBuildPhase;
14981618
buildActionMask = 2147483647;
@@ -1742,6 +1862,42 @@
17421862
};
17431863
name = Release;
17441864
};
1865+
47537DA61EEF7042005F4316 /* Debug */ = {
1866+
isa = XCBuildConfiguration;
1867+
buildSettings = {
1868+
DEVELOPMENT_TEAM = WTB2BKP7UZ;
1869+
PRODUCT_NAME = "$(TARGET_NAME)";
1870+
SWIFT_VERSION = 3.0;
1871+
};
1872+
name = Debug;
1873+
};
1874+
47537DA71EEF7042005F4316 /* Release */ = {
1875+
isa = XCBuildConfiguration;
1876+
buildSettings = {
1877+
DEVELOPMENT_TEAM = WTB2BKP7UZ;
1878+
PRODUCT_NAME = "$(TARGET_NAME)";
1879+
SWIFT_VERSION = 3.0;
1880+
};
1881+
name = Release;
1882+
};
1883+
47537DB21EEF7C48005F4316 /* Debug */ = {
1884+
isa = XCBuildConfiguration;
1885+
buildSettings = {
1886+
DEVELOPMENT_TEAM = WTB2BKP7UZ;
1887+
PRODUCT_NAME = "$(TARGET_NAME)";
1888+
SWIFT_VERSION = 3.0;
1889+
};
1890+
name = Debug;
1891+
};
1892+
47537DB31EEF7C48005F4316 /* Release */ = {
1893+
isa = XCBuildConfiguration;
1894+
buildSettings = {
1895+
DEVELOPMENT_TEAM = WTB2BKP7UZ;
1896+
PRODUCT_NAME = "$(TARGET_NAME)";
1897+
SWIFT_VERSION = 3.0;
1898+
};
1899+
name = Release;
1900+
};
17451901
47664D7B1EEE6237009ECB34 /* Debug */ = {
17461902
isa = XCBuildConfiguration;
17471903
buildSettings = {
@@ -2274,6 +2430,7 @@
22742430
47370BDA1EEE7C6800C6617B /* Release */,
22752431
);
22762432
defaultConfigurationIsVisible = 0;
2433+
defaultConfigurationName = Release;
22772434
};
22782435
474236B61EE7950700D1C9F7 /* Build configuration list for PBXNativeTarget "运算符" */ = {
22792436
isa = XCConfigurationList;
@@ -2293,6 +2450,22 @@
22932450
defaultConfigurationIsVisible = 0;
22942451
defaultConfigurationName = Release;
22952452
};
2453+
47537DA81EEF7042005F4316 /* Build configuration list for PBXNativeTarget "方法" */ = {
2454+
isa = XCConfigurationList;
2455+
buildConfigurations = (
2456+
47537DA61EEF7042005F4316 /* Debug */,
2457+
47537DA71EEF7042005F4316 /* Release */,
2458+
);
2459+
defaultConfigurationIsVisible = 0;
2460+
};
2461+
47537DB11EEF7C48005F4316 /* Build configuration list for PBXNativeTarget "下标subscripts" */ = {
2462+
isa = XCConfigurationList;
2463+
buildConfigurations = (
2464+
47537DB21EEF7C48005F4316 /* Debug */,
2465+
47537DB31EEF7C48005F4316 /* Release */,
2466+
);
2467+
defaultConfigurationIsVisible = 0;
2468+
};
22962469
47664D7D1EEE6237009ECB34 /* Build configuration list for PBXNativeTarget "闭包捕获值" */ = {
22972470
isa = XCConfigurationList;
22982471
buildConfigurations = (
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//
2+
// main.swift
3+
// 下标subscripts
4+
//
5+
// Created by 韩俊强 on 2017/6/13.
6+
// Copyright © 2017年 HaRi. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
/*
12+
subscripts(下标): 访问对象中数据的快捷方式
13+
所谓下标脚本语法就是能够通过, 实例[索引值]来访问实例中的数据
14+
类似于以前我们访问数字和字典, 其实Swift中的数组和字典就是一个结构体
15+
16+
Array: subscript (index: Int) -> T
17+
Dictionary: subscript (key: Key) -> Value?
18+
arr[0] == arr.subscript(0)
19+
dict["key"] == dict.subscript("key")
20+
*/
21+
22+
struct Student {
23+
var name: String = "HaRi"
24+
var math: Double = 98.0
25+
var chinese: Double = 99.0
26+
var english: Double = 100.0
27+
28+
func score(course: String) -> Double
29+
{
30+
switch course {
31+
case "math":
32+
return math
33+
case "chinese":
34+
return chinese
35+
case "english":
36+
return english
37+
default:
38+
return math + chinese + english // 此处待探究为何返回nil就会爆红
39+
}
40+
}
41+
42+
//1.要想实现下标访问, 必须实现subscript方法;
43+
//2.要想通过下标访问, 必须实现get方法;
44+
//3.要想通过下标赋值, 必须实现set方法.
45+
subscript(course: String) -> Double?{
46+
get{
47+
switch course {
48+
case "math":
49+
return math
50+
case "chinese":
51+
return chinese
52+
case "english":
53+
return english
54+
default:
55+
return math + chinese + english
56+
}
57+
}
58+
set{
59+
switch course{
60+
case "math":
61+
//以为返回的是可选类型
62+
math = newValue!
63+
case "chinese":
64+
chinese = newValue!
65+
case "english":
66+
english = newValue!
67+
default:
68+
print("not found")
69+
}
70+
}
71+
}
72+
}
73+
var stu = Student(name: "han", math: 99.0, chinese: 98.0, english: 57.0)
74+
print(stu.score(course: "english"))
75+
stu["math"] = 100.0
76+
print(stu["math"]!)
77+
78+
79+
/** Swift中是允许多索引的下标的 **/
80+
struct Mul {
81+
subscript(a: Int, b: Int) -> Int
82+
{
83+
return a + b
84+
}
85+
}
86+
var m = Mul()
87+
print(m[10,20])
88+
89+

Swift基础语法大全/属性/main.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ print("name = \(p.name) age = \(p.age)")
3232
struct Person2 {
3333
var name: String
3434
var age : Int
35-
let card: String // ID Card
35+
let card: String // 常量 ID Card
3636
}
3737
var p2 = Person2(name: "HaRi", age: 24, card: "1234")
3838
p2.name = "helloName"
@@ -89,7 +89,7 @@ class Line {
8989
lazy var container: Array<AnyObject> = {
9090
print("懒加载")
9191

92-
var arrrM:Array = [1,2,3]
92+
var arrrM:Array<Int> = []
9393
// return self.end - self.start Error!
9494
return arrrM as [AnyObject]
9595
}()

0 commit comments

Comments
 (0)