隨機(jī)數(shù)的生成
生成1~100(含1和100)的隨機(jī)數(shù)
arc4random()
var rangeValue:Int = Int(arc4random()%100)+1
arc4random_uniform()
var rangeValue:Int = Int(arc4random_uniform(100))+1
區(qū)間運(yùn)算符的使用
- 閉區(qū)間:
...
//遍歷0到255的數(shù)值(含0和255)
for number in 0...255 {
print(number)
}
- 半閉區(qū)間:
..<
//遍歷數(shù)組元素
var array = ["red", "green", "blue"]
var array = ["red", "green", "blue"]
for i in 0..<array.count {
print("數(shù)組第\(i+1)個(gè)元素是:\(array[i])")
}
- 應(yīng)用一:
字符串截取
let str = "GlenRiver.Study.Swift"
//swift2.2的寫法
//let str1 = str.startIndex.advancedBy(4)..<str.startIndex.advancedBy(9)
//let result = str.substringWithRange(str1)
//swift3.0的寫法
//let str1 = str.index(str.startIndex , offsetBy: 4)..<str.index(str.startIndex , offsetBy: 9)
//let result = str.substring(with: str1)
let str1 = str.index(str.startIndex , offsetBy: 4)...str.index(str.startIndex , offsetBy: 8)
let result = str[str1]
print("I'm \(result)")
- 應(yīng)用二:
檢查字符串
//檢測(cè)字符串中的大寫字母
let str2 = "GlenRiver"
let interval = "a"..."z"
for char in str2.characters {
if !interval.contains(String(char)){
print("\(char)不是小寫字母")
}
}
運(yùn)算符重載
運(yùn)算符的重載:已有運(yùn)算符對(duì)自定義的類和結(jié)構(gòu)進(jìn)行運(yùn)算或重新定義已有運(yùn)算符的運(yùn)算規(guī)則的機(jī)制
-
重載加號(hào)運(yùn)算符
-- 計(jì)算中點(diǎn)經(jīng)緯度坐標(biāo)
struct CenterLatLngPoint{
var lng = 0.0;
var lat = 0.0;
}
func + (leftpoint:CenterLatLngPoint, rightpoint:CenterLatLngPoint) -> CenterLatLngPoint{
return CenterLatLngPoint(lng:(rightpoint.lng+leftpoint.lng)/2.0, lat:(rightpoint.lat+leftpoint.lat)/2.0)
}
let firstPoint = CenterLatLngPoint(lng:114.00, lat:23.45);
let secondPoint = CenterLatLngPoint(lng:115.35, lat:21.43);
let centerPoint = firstPoint + secondPoint;
print("The first point is [\(firstPoint.lng), \(firstPoint.lat)]")
print("The second point is [\(secondPoint.lng), \(secondPoint.lat)]")
print("The center point is [\(centerPoint.lng), \(centerPoint.lat)]")
-
重載判斷運(yùn)算符
-- 判斷坐標(biāo)點(diǎn)是不是同一個(gè)點(diǎn)
struct CenterLatLngPoint{
var lng = 0.0;
var lat = 0.0;
}
func == (leftpoint:CenterLatLngPoint, rightpoint:CenterLatLngPoint) -> Bool {
return (leftpoint.lng == rightpoint.lng) && (leftpoint.lat == rightpoint.lat)
}
func != (leftpoint:CenterLatLngPoint, rightpoint:CenterLatLngPoint) -> Bool {
return !(leftpoint == rightpoint)
}
let thirdPoint = CenterLatLngPoint(lng:115.35, lat:21.43)
let judgeResult1 = firstPoint == secondPoint
let judgeResult2 = secondPoint == thirdPoint
//let judgeResult1 = firstPoint != secondPoint
//let judgeResult1 = secondPoint != thirdPoint
if(judgeResult1){
print("The first point and the second point are the same point.")
}
else{
print("The first point and the second point are different points.")
}
if(judgeResult2){
print("The second point and the third point are the same point.")
}
else{
print("The second point and the third point are different points.")
}
-
組合運(yùn)算符
-- 將左邊坐標(biāo)點(diǎn)移動(dòng)到兩點(diǎn)中點(diǎn)位置
struct CenterLatLngPoint{
var lng = 0.0;
var lat = 0.0;
}
// inout:一般函數(shù)內(nèi)不能改變函數(shù)外的值,inout關(guān)鍵值作用是使得函數(shù)內(nèi)可以改變函數(shù)外的值
// 特別注意:leftpoint的定義不能用let(常量)定義
func += ( leftpoint:inout CenterLatLngPoint, rightpoint:CenterLatLngPoint){
leftpoint = leftpoint + rightpoint
}
var leftPoint = CenterLatLngPoint(lng:115.35, lat:21.43)
var rightPoint = CenterLatLngPoint(lng:114.00, lat:23.45)
print("The left point is [\(leftPoint.lng), \(leftPoint.lat)] before")
leftPoint += rightPoint
print("The left point is [\(leftPoint.lng), \(leftPoint.lat)] now")
轉(zhuǎn)載,請(qǐng)表明出處洽腺! GlenRiver
代碼下載:GR-Swift.rar
2016-2017 GlenRiver