Swift — 基礎學習(二) 【2016-11-18】

隨機數(shù)的生成


生成1~100(含1和100)的隨機數(shù)
  • arc4random()
var rangeValue:Int = Int(arc4random()%100)+1
  • arc4random_uniform()
var rangeValue:Int = Int(arc4random_uniform(100))+1

區(qū)間運算符的使用


  • 閉區(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)個元素是:\(array[i])")
}
  • 應用一:字符串截取
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)")
  • 應用二:檢查字符串
//檢測字符串中的大寫字母
let str2 = "GlenRiver"
let interval = "a"..."z"
for char in str2.characters {
    if !interval.contains(String(char)){
        print("\(char)不是小寫字母")
    }
}

運算符重載


運算符的重載:已有運算符對自定義的類和結構進行運算或重新定義已有運算符的運算規(guī)則的機制
  • 重載加號運算符 -- 計算中點經緯度坐標
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)]")
  • 重載判斷運算符 -- 判斷坐標點是不是同一個點
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.")
}
  • 組合運算符 -- 將左邊坐標點移動到兩點中點位置
struct CenterLatLngPoint{
    var lng = 0.0;
    var lat = 0.0;
}

// inout:一般函數(shù)內不能改變函數(shù)外的值,inout關鍵值作用是使得函數(shù)內可以改變函數(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")

轉載,請表明出處! GlenRiver


代碼下載:GR-Swift.rar


2016-2017 GlenRiver

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容