//swift恒等运算符
//===恒等,不等!==
//运用这两个运算符检测两个常量或者变量是否引用同一个实例
//“等价于”表示两个类类型(class type)的常量或者变量引用同一个类实例。
//等于”表示两个实例的值“相等”或“相同”,判定时要遵照设计者定义的评判标准,因此相对于“相 等”来说,这是一种更加合适的叫法。
//类和结构体的选择
//属性的存储只能是类或者是结构体
//常量属性
struct FixedLengthRange {
var first: Int
let length: Int
}
var range = @H_301_145@FixedLengthRange(first: 5,length: 69)
range.first = 7
@H_403_170@//对于常量赋值则不允许
//常量结构体的存储属性
//延迟存储属性:延迟存储属性是指当第一次被调用的时候才会计算其初始值的属性。
@H_403_170@class DataImporter {
var fileName = "lihuan.txt"
}
@H_403_170@class DataManager {
lazy var importer = @H_301_145@DataImporter()
var data = [String]()//提供数据管理功能
}
@H_403_170@let manager = @H_301_145@DataManager()
manager.data.append("some data")
@H_301_145@manager.@H_301_145@data.append("some more data")
@H_403_170@print(manager.importer.fileName)
//存储属性和实例变量
//计算属性
struct Point {
var x = 0.0,y = 0.0
}
struct Size {
var width = 0.0,height = 0.0
}
struct Rect {
var origin = @H_301_145@Point()
var size = @H_301_145@Size ()
var center:@H_301_145@Point {
get {
@H_403_170@let centerX = @H_301_145@origin.@H_301_145@x + (@H_301_145@size.@H_301_145@width / 2)
let centerY = @H_301_145@origin.@H_301_145@y + (@H_301_145@size.@H_301_145@height / 2)
return @H_301_145@Point(x:centerX,y: centerY)
@H_403_170@}
@H_403_170@set(newCenter) {
@H_403_170@@H_301_145@origin.@H_301_145@x = newCenter.@H_301_145@x - (@H_301_145@size.@H_301_145@width / 2)
@H_301_145@origin.@H_301_145@y = newCenter.@H_301_145@y - (@H_301_145@size.@H_301_145@height / 2)
@H_403_170@}
}
}
var square = @H_301_145@Rect(origin: @H_301_145@Point(x: 0.0,y: 0.0),size:@H_301_145@Size(width: 10.0,height: 10.0))
let initialSquareCenter = @H_301_145@square.@H_301_145@center
square.center = Point(x: 15.0,y: 15.0)
print("square.origin is now at (\(@H_301_145@square.@H_301_145@origin.@H_301_145@x),\(@H_301_145@square.@H_301_145@origin.@H_301_145@y))")
//只读计算属性,只读计算属性的声明可以去掉get关键字和花括号
struct Cuboid {
var width = 0.0,height = 0.0,depth = 0.0
var volume:Double {
return width*height*depth
}
}
let fourByFiveByTwo = @H_301_145@Cuboid(width: 4.0,height: 5.0,depth: 2.0)
print("\(fourByFiveByTwo.volume)")
//属性观察器:属性观察器监控和响应属性值的变化,每次属性被设置值的时候都会调用属性观察器,甚至新的值和现在的值相同的时候也不例外
//可以为除了延迟存储属性之外的其他存储属性添加属性观察器,也可以通过重写属性的方式为继承的属性(包括 存储属性和计算属性)添加属性观察器
// willSet在新的值被设置之前调用,didSet在新的值被设置之后立即调用
class StepCounter {
var totalSteps:Int = 0 {
willSet(newTotalSteps){
print("will set \(newTotalSteps)")
}
didSet{
@H_403_170@if @H_301_145@totalSteps > oldValue {
print("did set \(@H_301_145@totalSteps - oldValue)")
}
}
}
}
let setpCounter = @H_301_145@StepCounter()
setpCounter.totalSteps = 200
setpCounter.totalSteps = 360
//全局变量和局部变量
struct SomeStructure {
@H_403_170@static var storedTypeProperty = "some value"
static var computedTypeProperty: Int {
return 6
}
@H_403_170@}
class SomeClass {
static var storedTypeProperty = "some value"
static var computedTypeProperty: Int {
return 104
}
}
print(@H_301_145@SomeStructure.storedTypeProperty)
@H_301_145@SomeStructure.storedTypeProperty = "another value"
print(@H_301_145@SomeStructure.storedTypeProperty)
print(@H_301_145@SomeClass.computedTypeProperty)
//eg
struct AudioChannel {
static let thresholdLevel = 10
static var maxInputLevelForAllChannels = 0
var currentLevel: Int = 0 {
didSet {
if @H_301_145@currentLevel > @H_301_145@AudioChannel.thresholdLevel {
@H_301_145@currentLevel = @H_301_145@AudioChannel.thresholdLevel
}
if @H_301_145@currentLevel > @H_301_145@AudioChannel.maxInputLevelForAllChannels {
@H_301_145@AudioChannel.maxInputLevelForAllChannels = @H_301_145@currentLevel
}
}
@H_403_170@}
}
var leftChannel = @H_301_145@AudioChannel()
var rightChannel = @H_301_145@AudioChannel()
@H_403_170@leftChannel.currentLevel = 7
print(leftChannel.currentLevel)
//方法
class Counter {
//类中定义了三个实例话方法
var count = 0
func increment() {
@H_301_145@count += 1
}
func incrementBy(amount:Int) {
self.@H_301_145@count += amount
}
func reset() {
@H_301_145@count = 0
}
@H_403_170@}
let counter = @H_301_145@Counter()
@H_301_145@counter.increment()
@H_301_145@counter.incrementBy(5)
counter.reset()
print(counter.count)
@H_403_170@class CounterTwo {
var count: Int = 0
func incrementBy(amount:Int,numberOfTimes:Int) {
@H_301_145@count += amount*numberOfTimes
}
}
//默认的情况下第一个参数被认为是局部名称,第二个参数被认为既可以看作内部也可以看做外部
let counterTwo = @H_301_145@CounterTwo()
@H_301_145@counterTwo.incrementBy(5,numberOfTimes:9)
print(counterTwo.count)
@H_403_170@//self属性
//类型方法调用也是.语法 实例方法是被类型的某个实例调用的方法。你也可以定义类型本身调用的方法,这种方法就叫做类型方法
class SomeOtherClass {
@H_403_170@static func someMethod() {
@H_403_170@print("类型方法")
@H_403_170@}
@H_403_170@}
SomeOtherClass.someMethod()
//水平
struct LevelTracker {
@H_403_170@static var highestUnlockedLevel = 1
@H_403_170@static func unlockLevel(level:Int){
highestUnlockedLevel = level
}
@H_403_170@static func levelIsUnlocked(level:Int) -> Bool {
return level <= highestUnlockedLevel
}
var currentLevel = 1
@H_403_170@mutating func advanceToLevel(level:Int) -> Bool {
if @H_301_145@LevelTracker.levelIsUnlocked(level) {
@H_301_145@currentLevel = level
return true
}else {
return false
}
}
@H_403_170@}
//监测进度
class Player {
@H_403_170@var tracker = @H_301_145@LevelTracker()
let playName:String
func completedLevel(level:Int){
@H_403_170@@H_301_145@LevelTracker.unlockLevel(level + 1)
@H_301_145@tracker.advanceToLevel(level + 1)
@H_403_170@}
init(name:String) {
@H_403_170@@H_301_145@playName = name
@H_403_170@}
@H_403_170@ @H_403_170@}
@H_403_170@@H_403_170@
var player = @H_301_145@Player(name:"lihuan")
if @H_301_145@player.@H_301_145@tracker.advanceToLevel(6) {
print("player is now on level 6")
@H_403_170@}
else{
@H_403_170@print("level 6 has not yet been unlocked")
@H_403_170@}
//下标脚本下标脚本 可以定义在类(Class)、结构体(structure)和枚举(enumeration)这些目标中,可以认为是访问 集合(collection),列表(list)或序列(sequence的快捷方式,使用下标脚本的索引设置和获取值,不需要 再调用实例的特定的赋值和访问方法。举例来说,用下标脚本访问一个数组(Array)实例中的元素可以这样写 meArray[index],访问字典(Dictionary)实例中的元素可以这样写 someDictionary[key]
//下标脚本语法
//subscript(index:Int) -> Int {
// get{
// //返回与入参匹配的Int类型的值
// }
// set(newValue){
//
// //执行赋值操作
//
//}
//eg
struct TimesTable {
@H_403_170@let multiplier:Int
subscript(index:Int) -> Int {
@H_403_170@return @H_301_145@multiplier * index
@H_403_170@}
@H_403_170@}
let threeTimesTable = @H_301_145@TimesTable(multiplier: 3)
//根据角标找到然后返回
print("3的6倍是\(threeTimesTable[6])")
//下标脚本用法
var numberOfLegs = ["splider":8,"ant":6,"cat":4]
numberOfLegs["bird"] = 2
print(numberOfLegs)
//继承
//基类:不继承其它类的类
class Vehicle {
var currentSpeed = 0.0
var description:String {
return "traveling at \(@H_301_145@currentSpeed) miles per hour"
}
func makeNoise() {
//
print("我有噪音");
}
@H_403_170@}
@H_403_170@let someVehicle = @H_301_145@Vehicle()
print("vehicle :\(someVehicle.description)")
@H_403_170@//子类生成 子类生成(Subclassing)指的是在一个已有类的基础上创建一个新的类
class Bicycle:@H_301_145@Vehicle {
//继承父类的子类
var hasBasket = false
}
let bicycle = @H_301_145@Bicycle()
bicycle.hasBasket = true;
bicycle.currentSpeed = 15.0
print("vehicle :\(bicycle.description)")
@H_403_170@@H_403_170@
class Tandem: @H_301_145@Bicycle {
@H_403_170@var currentNumberOfPages = 0
override func makeNoise() {
print("hello")
}
}
let tandem = @H_301_145@Tandem()
tandem.hasBasket = true
tandem.currentSpeed = 22.0
@H_403_170@print("\(tandem.hasBasket)")
@H_301_145@tandem.makeNoise()
@H_403_170@//重写属性,防止重写的话天假final
//构造过程
//存储属性的初赋值
//构造器构造器在创建某特定类型的新实例时调用。它的最简形式类似于一个不带任何参数的实例方法,以关键字 init 命 名。
struct Fathreheit {
var temperature :Double
init() {
@H_403_170@temperature = 32.0
@H_403_170@}
@H_403_170@ @H_403_170@}
var f = Fathreheit()
print("\(f.temperature)")
//默认属性值
//自定义构造过程
原文链接:https://www.f2er.com/swift/323866.html