swift 笔记
2017-09-05 本文已影响3人
醉影
func Qprint<T>(_ message: T,
file: String = #file,
method: String = #function,
line: Int = #line)
{
print("[\(line)]-[\((file as NSString).lastPathComponent)]-\(method):\n\(message)\n")
}
字符串操作
///去掉字符串里面所有的某个字符
///- Parameter str: 字符串
///- Parameter c: 要去掉的字符
func deleteCharacterAtString(_ str:String, deleteCharacter c: Character) -> String {
var newStr = ""
_ = str.characters.prefix { (i: Character) -> Bool in
guard i == c else {
newStr += String(i)
return true
}
return true
}
return newStr
}
let name = "Hello World good"
Qprint(deleteCharacterAtString(name, deleteCharacter: " "))
//打印:
[57]-[ViewController.swift]-viewDidLoad():
HelloWorldgood
let firstSpace = name.characters.index(of: " ")!
let firstName = String(name.characters.prefix(upTo: firstSpace)) //在...之前的字符串
Qprint(firstName)
//打印:
[55]-[ViewController.swift]-viewDidLoad():
Hello World
//从 xxx 开始到字符串结束
let last = String(name.characters.suffix(from: firstSpace))
符号
let w = "A"..."Z" //ClosedRange 不可数的闭区间
Qprint(w)
if w ~= "C" {
print("C在A到Z的范围内")
}
for in
var array = Array<Any>()
for i in 0...100 where i%2 == 0 {
array.append(i)
}
Qprint(array)
//打印
[68]-[ViewController.swift]-viewDidLoad():
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]