Skip to content

Commit 559557b

Browse files
committed
print函数的细节
1 parent c9f3eec commit 559557b

4 files changed

Lines changed: 373 additions & 0 deletions

File tree

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
// !$*UTF8*$!
2+
{
3+
archiveVersion = 1;
4+
classes = {
5+
};
6+
objectVersion = 46;
7+
objects = {
8+
9+
/* Begin PBXBuildFile section */
10+
9B4614361C57472D00619C13 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B4614351C57472D00619C13 /* main.swift */; };
11+
9B46143D1C57836B00619C13 /* Print.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B46143C1C57836B00619C13 /* Print.swift */; };
12+
9B46143F1C57AFC300619C13 /* Debug.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B46143E1C57AFC300619C13 /* Debug.swift */; };
13+
/* End PBXBuildFile section */
14+
15+
/* Begin PBXCopyFilesBuildPhase section */
16+
9B4614301C57472D00619C13 /* CopyFiles */ = {
17+
isa = PBXCopyFilesBuildPhase;
18+
buildActionMask = 2147483647;
19+
dstPath = /usr/share/man/man1/;
20+
dstSubfolderSpec = 0;
21+
files = (
22+
);
23+
runOnlyForDeploymentPostprocessing = 1;
24+
};
25+
/* End PBXCopyFilesBuildPhase section */
26+
27+
/* Begin PBXFileReference section */
28+
9B4614321C57472D00619C13 /* Streamable */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Streamable; sourceTree = BUILT_PRODUCTS_DIR; };
29+
9B4614351C57472D00619C13 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
30+
9B46143C1C57836B00619C13 /* Print.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Print.swift; sourceTree = "<group>"; };
31+
9B46143E1C57AFC300619C13 /* Debug.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Debug.swift; sourceTree = "<group>"; };
32+
/* End PBXFileReference section */
33+
34+
/* Begin PBXFrameworksBuildPhase section */
35+
9B46142F1C57472D00619C13 /* Frameworks */ = {
36+
isa = PBXFrameworksBuildPhase;
37+
buildActionMask = 2147483647;
38+
files = (
39+
);
40+
runOnlyForDeploymentPostprocessing = 0;
41+
};
42+
/* End PBXFrameworksBuildPhase section */
43+
44+
/* Begin PBXGroup section */
45+
9B4614291C57472D00619C13 = {
46+
isa = PBXGroup;
47+
children = (
48+
9B4614341C57472D00619C13 /* Streamable */,
49+
9B4614331C57472D00619C13 /* Products */,
50+
);
51+
sourceTree = "<group>";
52+
};
53+
9B4614331C57472D00619C13 /* Products */ = {
54+
isa = PBXGroup;
55+
children = (
56+
9B4614321C57472D00619C13 /* Streamable */,
57+
);
58+
name = Products;
59+
sourceTree = "<group>";
60+
};
61+
9B4614341C57472D00619C13 /* Streamable */ = {
62+
isa = PBXGroup;
63+
children = (
64+
9B4614351C57472D00619C13 /* main.swift */,
65+
9B46143C1C57836B00619C13 /* Print.swift */,
66+
9B46143E1C57AFC300619C13 /* Debug.swift */,
67+
);
68+
path = Streamable;
69+
sourceTree = "<group>";
70+
};
71+
/* End PBXGroup section */
72+
73+
/* Begin PBXNativeTarget section */
74+
9B4614311C57472D00619C13 /* Streamable */ = {
75+
isa = PBXNativeTarget;
76+
buildConfigurationList = 9B4614391C57472D00619C13 /* Build configuration list for PBXNativeTarget "Streamable" */;
77+
buildPhases = (
78+
9B46142E1C57472D00619C13 /* Sources */,
79+
9B46142F1C57472D00619C13 /* Frameworks */,
80+
9B4614301C57472D00619C13 /* CopyFiles */,
81+
);
82+
buildRules = (
83+
);
84+
dependencies = (
85+
);
86+
name = Streamable;
87+
productName = Streamable;
88+
productReference = 9B4614321C57472D00619C13 /* Streamable */;
89+
productType = "com.apple.product-type.tool";
90+
};
91+
/* End PBXNativeTarget section */
92+
93+
/* Begin PBXProject section */
94+
9B46142A1C57472D00619C13 /* Project object */ = {
95+
isa = PBXProject;
96+
attributes = {
97+
LastSwiftUpdateCheck = 0720;
98+
LastUpgradeCheck = 0720;
99+
ORGANIZATIONNAME = zxy;
100+
TargetAttributes = {
101+
9B4614311C57472D00619C13 = {
102+
CreatedOnToolsVersion = 7.2;
103+
};
104+
};
105+
};
106+
buildConfigurationList = 9B46142D1C57472D00619C13 /* Build configuration list for PBXProject "Streamable" */;
107+
compatibilityVersion = "Xcode 3.2";
108+
developmentRegion = English;
109+
hasScannedForEncodings = 0;
110+
knownRegions = (
111+
en,
112+
);
113+
mainGroup = 9B4614291C57472D00619C13;
114+
productRefGroup = 9B4614331C57472D00619C13 /* Products */;
115+
projectDirPath = "";
116+
projectRoot = "";
117+
targets = (
118+
9B4614311C57472D00619C13 /* Streamable */,
119+
);
120+
};
121+
/* End PBXProject section */
122+
123+
/* Begin PBXSourcesBuildPhase section */
124+
9B46142E1C57472D00619C13 /* Sources */ = {
125+
isa = PBXSourcesBuildPhase;
126+
buildActionMask = 2147483647;
127+
files = (
128+
9B4614361C57472D00619C13 /* main.swift in Sources */,
129+
9B46143F1C57AFC300619C13 /* Debug.swift in Sources */,
130+
9B46143D1C57836B00619C13 /* Print.swift in Sources */,
131+
);
132+
runOnlyForDeploymentPostprocessing = 0;
133+
};
134+
/* End PBXSourcesBuildPhase section */
135+
136+
/* Begin XCBuildConfiguration section */
137+
9B4614371C57472D00619C13 /* Debug */ = {
138+
isa = XCBuildConfiguration;
139+
buildSettings = {
140+
ALWAYS_SEARCH_USER_PATHS = NO;
141+
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
142+
CLANG_CXX_LIBRARY = "libc++";
143+
CLANG_ENABLE_MODULES = YES;
144+
CLANG_ENABLE_OBJC_ARC = YES;
145+
CLANG_WARN_BOOL_CONVERSION = YES;
146+
CLANG_WARN_CONSTANT_CONVERSION = YES;
147+
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
148+
CLANG_WARN_EMPTY_BODY = YES;
149+
CLANG_WARN_ENUM_CONVERSION = YES;
150+
CLANG_WARN_INT_CONVERSION = YES;
151+
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
152+
CLANG_WARN_UNREACHABLE_CODE = YES;
153+
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
154+
CODE_SIGN_IDENTITY = "-";
155+
COPY_PHASE_STRIP = NO;
156+
DEBUG_INFORMATION_FORMAT = dwarf;
157+
ENABLE_STRICT_OBJC_MSGSEND = YES;
158+
ENABLE_TESTABILITY = YES;
159+
GCC_C_LANGUAGE_STANDARD = gnu99;
160+
GCC_DYNAMIC_NO_PIC = NO;
161+
GCC_NO_COMMON_BLOCKS = YES;
162+
GCC_OPTIMIZATION_LEVEL = 0;
163+
GCC_PREPROCESSOR_DEFINITIONS = (
164+
"DEBUG=1",
165+
"$(inherited)",
166+
);
167+
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
168+
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
169+
GCC_WARN_UNDECLARED_SELECTOR = YES;
170+
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
171+
GCC_WARN_UNUSED_FUNCTION = YES;
172+
GCC_WARN_UNUSED_VARIABLE = YES;
173+
MACOSX_DEPLOYMENT_TARGET = 10.11;
174+
MTL_ENABLE_DEBUG_INFO = YES;
175+
ONLY_ACTIVE_ARCH = YES;
176+
SDKROOT = macosx;
177+
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
178+
};
179+
name = Debug;
180+
};
181+
9B4614381C57472D00619C13 /* Release */ = {
182+
isa = XCBuildConfiguration;
183+
buildSettings = {
184+
ALWAYS_SEARCH_USER_PATHS = NO;
185+
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
186+
CLANG_CXX_LIBRARY = "libc++";
187+
CLANG_ENABLE_MODULES = YES;
188+
CLANG_ENABLE_OBJC_ARC = YES;
189+
CLANG_WARN_BOOL_CONVERSION = YES;
190+
CLANG_WARN_CONSTANT_CONVERSION = YES;
191+
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
192+
CLANG_WARN_EMPTY_BODY = YES;
193+
CLANG_WARN_ENUM_CONVERSION = YES;
194+
CLANG_WARN_INT_CONVERSION = YES;
195+
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
196+
CLANG_WARN_UNREACHABLE_CODE = YES;
197+
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
198+
CODE_SIGN_IDENTITY = "-";
199+
COPY_PHASE_STRIP = NO;
200+
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
201+
ENABLE_NS_ASSERTIONS = NO;
202+
ENABLE_STRICT_OBJC_MSGSEND = YES;
203+
GCC_C_LANGUAGE_STANDARD = gnu99;
204+
GCC_NO_COMMON_BLOCKS = YES;
205+
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
206+
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
207+
GCC_WARN_UNDECLARED_SELECTOR = YES;
208+
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
209+
GCC_WARN_UNUSED_FUNCTION = YES;
210+
GCC_WARN_UNUSED_VARIABLE = YES;
211+
MACOSX_DEPLOYMENT_TARGET = 10.11;
212+
MTL_ENABLE_DEBUG_INFO = NO;
213+
SDKROOT = macosx;
214+
};
215+
name = Release;
216+
};
217+
9B46143A1C57472D00619C13 /* Debug */ = {
218+
isa = XCBuildConfiguration;
219+
buildSettings = {
220+
PRODUCT_NAME = "$(TARGET_NAME)";
221+
};
222+
name = Debug;
223+
};
224+
9B46143B1C57472D00619C13 /* Release */ = {
225+
isa = XCBuildConfiguration;
226+
buildSettings = {
227+
PRODUCT_NAME = "$(TARGET_NAME)";
228+
};
229+
name = Release;
230+
};
231+
/* End XCBuildConfiguration section */
232+
233+
/* Begin XCConfigurationList section */
234+
9B46142D1C57472D00619C13 /* Build configuration list for PBXProject "Streamable" */ = {
235+
isa = XCConfigurationList;
236+
buildConfigurations = (
237+
9B4614371C57472D00619C13 /* Debug */,
238+
9B4614381C57472D00619C13 /* Release */,
239+
);
240+
defaultConfigurationIsVisible = 0;
241+
defaultConfigurationName = Release;
242+
};
243+
9B4614391C57472D00619C13 /* Build configuration list for PBXNativeTarget "Streamable" */ = {
244+
isa = XCConfigurationList;
245+
buildConfigurations = (
246+
9B46143A1C57472D00619C13 /* Debug */,
247+
9B46143B1C57472D00619C13 /* Release */,
248+
);
249+
defaultConfigurationIsVisible = 0;
250+
};
251+
/* End XCConfigurationList section */
252+
};
253+
rootObject = 9B46142A1C57472D00619C13 /* Project object */;
254+
}

Streamable/Streamable/Debug.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// Debug.swift
3+
// Streamable
4+
//
5+
// Created by 张星宇 on 16/1/26.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
struct PersonDebug {
12+
var name: String
13+
private var age: Int
14+
15+
init(name: String, age: Int) {
16+
self.name = name
17+
self.age = age
18+
}
19+
}
20+
21+
extension PersonDebug: CustomStringConvertible, CustomDebugStringConvertible {
22+
var description: String {
23+
return "In CustomStringConvertible Protocol"
24+
}
25+
26+
var debugDescription: String {
27+
return "In CustomDebugStringConvertible Protocol"
28+
}
29+
}
30+
31+
// print优先调用CustomStringConvertible
32+
func testCustomStringConvertible() {
33+
print("print结构体")
34+
let kt = PersonDebug(name: "kt", age: 21)
35+
print(kt)
36+
print(String(kt))
37+
print("")
38+
}
39+
40+
// debugPrint优先调用CustomDebugStringConvertible
41+
func testCustomDebugStringConvertible() {
42+
print("debugPrint结构体")
43+
let kt = PersonDebug(name: "kt", age: 21)
44+
debugPrint(kt)
45+
print(String(reflecting: kt))
46+
print("")
47+
}

Streamable/Streamable/Print.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// Print.swift
3+
// Streamable
4+
//
5+
// Created by 张星宇 on 16/1/26.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
struct Person {
12+
var name: String
13+
private var age: Int
14+
15+
init(name: String, age: Int) {
16+
self.name = name
17+
self.age = age
18+
}
19+
}
20+
21+
// 1. 调用不带`output`参数的`print`函数,函数内部生成`_Stdout `类型的输出流,调用`_print`函数
22+
// 2. 在`_print`函数中国处理完`separator`和`terminator `等格式参数后,调用`_print_unlocked `函数处理字符串输出。
23+
// 3. 在`_print_unlocked `函数的第一个if判断中,因为字符串类型实现了`Streamable `协议,所以调用字符串的`writeTo`函数,写入到输出流中。
24+
// 4. 根据字符串的`writeTo`函数的定义,它在内部调用了输出流的`write`方法
25+
// 5. `_Stdout`在其`write`方法中,调用C语言的`putchar`函数输出字符串的每个字符
26+
func testPrintString() {
27+
print("Hello, world!")
28+
}
29+
30+
// 1. 调用不带`output`参数的`print`函数,函数内部生成`_Stdout `类型的输出流,调用`_print`函数
31+
// 2. 在`_print`函数中国处理完`separator`和`terminator `等格式参数后,调用`_print_unlocked `函数处理字符串输出。
32+
// 3. 截止目前和输出字符串一致,不过Int类型(以及其他除了和字符有关的几乎所有类型)没有实现`Streamable `协议,它实现的是`CustomStringConvertible `协议,定义了自己的计算属性`description`
33+
// 4. `description`是一个字符串类型,调用字符串的`writeTo`方法此前已经讲过,就不再赘述了。
34+
func testPrintInteger() {
35+
print(123)
36+
}
37+
38+
39+
//1. 调用不带`output`参数的`print`函数,函数内部生成`_Stdout `类型的输出流,调用`_print`函数
40+
//2. 在`_print`函数中国处理完`separator`和`terminator `等格式参数后,调用`_print_unlocked `函数处理字符串输出。
41+
//3. 在`_print_unlocked `中调用`_adHocPrint `函数
42+
//4. switch语句匹配,参数类型是结构体,执行对应case语句中的代码
43+
func testPrintStruct() {
44+
print("测试直接打印结构体")
45+
let kt = Person(name: "kt", age: 21)
46+
print(kt)
47+
print("")
48+
}
49+
50+
// 字符串的初始化方法中调用`_print_unlocked `函数
51+
func testCreateString() {
52+
print("测试通过结构体创建字符串并输出到屏幕")
53+
let kt = Person(name: "kt", age: 21)
54+
let string = String(kt)
55+
print(string)
56+
print("")
57+
}

Streamable/Streamable/main.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// main.swift
3+
// Streamable
4+
//
5+
// Created by 张星宇 on 16/1/26.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
testPrintStruct()
12+
testCreateString()
13+
14+
testCustomStringConvertible()
15+
testCustomDebugStringConvertible()

0 commit comments

Comments
 (0)