Skip to content

Commit b1f6168

Browse files
committed
new post
1 parent 2246a77 commit b1f6168

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

‎_posts/2017-02-22-使用-.gitignore-忽略-git-仓库中的文件.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ tags:
2121
在Git中,很多时候你只想将代码提交到仓库,而不是将当前文件目录下的文件全部提交到Git仓库中,例如在MacOS系统下面的`.DS_Store`文件,或者是Xocde的操作记录,又或者是pod库的中一大串的源代码。这种情况下使用`.gitignore`就能够在Git提交时自动忽略掉这些文件。
2222

2323

24+
2425
## 忽略的格式
2526

2627
- `#` :此为注释 – 将被 Git 忽略
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
layout: post
3+
title: 强化 swift 中的 print
4+
subtitle: 强化 swift 中的 print 输出函数
5+
date: 2017-04-07
6+
author: BY
7+
header-img: img/post-bg-universe.jpg
8+
catalog: true
9+
tags:
10+
- iOS
11+
- Swift
12+
- Xcode
13+
- Debug
14+
---
15+
16+
在 Swift 中,最简单的输出方法就是使用 `print()`,在我们关心的地方输出字符串和值。
17+
18+
当程序变得非常复杂的时候,我们可能会输出很多内容,而想在其中寻找到我们希望的输出其实并不容易。我们往往需要更好更精确的输出,这包括输出这个 log 的文件,调用的行号以及所处的方法名字等等。
19+
20+
在 Swift 中,编译器为我们准备了几个很有用的编译符号,它们分别是:
21+
22+
<table><thead>
23+
<tr>
24+
<th>符号</th>
25+
<th>类型</th>
26+
<th>描述</th>
27+
</tr>
28+
</thead><tbody>
29+
<tr>
30+
<td>#file</td>
31+
<td>String</td>
32+
<td>包含这个符号的文件的路径</td>
33+
</tr>
34+
<tr>
35+
<td>#line</td>
36+
<td>Int</td>
37+
<td>符号出现处的行号</td>
38+
</tr>
39+
<tr>
40+
<td>#column</td>
41+
<td>Int</td>
42+
<td>符号出现处的列</td>
43+
</tr>
44+
<tr>
45+
<td>#function</td>
46+
<td>String</td>
47+
<td>包含这个符号的方法名字</td>
48+
</tr>
49+
</tbody></table>
50+
51+
52+
有了上面的这些编译符号,我们就可以自定义一个输出函数:`printm`
53+
54+
```swift
55+
public func printm(items: Any..., filename: String = #file, function: String = #function, line: Int = #line) {
56+
print("[\((filename as NSString).lastPathComponent) \(line) \(function)]\n",items)
57+
}
58+
```
59+
60+
因为输出是一个很消耗性能的操作,所以在releass环境下需要将输出函数去掉,将上面的函数换成:
61+
62+
```swift
63+
#if DEBUG
64+
65+
public func printm(items: Any..., filename: String = #file, function: String = #function, line: Int = #line) {
66+
print("[\((filename as NSString).lastPathComponent) \(line) \(function)]\n",items)
67+
}
68+
69+
#else
70+
71+
public func printm(items: Any..., filename: String = #file, function: String = #function, line: Int = #line) { }
72+
73+
#endif
74+
```
75+
76+
#### 参考:
77+
78+
- [《LOG 输出》](http://swifter.tips/log/) - 王巍 (@ONEVCAT)

0 commit comments

Comments
 (0)