iOS Developer

Swift — 基础学习(二) 【2016-11-18】

2016-11-19  本文已影响0人  GlenRiver

随机数的生成


生成1~100(含1和100)的随机数
var rangeValue:Int = Int(arc4random()%100)+1
var rangeValue:Int = Int(arc4random_uniform(100))+1

区间运算符的使用


//遍历0到255的数值(含0和255)
for number in 0...255 {
    print(number)
}
//遍历数组元素
var array = ["red", "green", "blue"]
var array = ["red", "green", "blue"]
for i in 0..<array.count {
    print("数组第\(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)不是小写字母")
    }
}

运算符重载


运算符的重载:已有运算符对自定义的类和结构进行运算或重新定义已有运算符的运算规则的机制
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:一般函数内不能改变函数外的值,inout关键值作用是使得函数内可以改变函数外的值
// 特别注意: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

上一篇下一篇

猜你喜欢

热点阅读