随机数的生成
生成1~100(含1和100)的随机数
arc4random()
var rangeValue:Int = Int(arc4random()%100)+1
arc4random_uniform()
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
网友评论