美文网首页iOS Tips
swift之mutating关键字

swift之mutating关键字

作者: 李小争 | 来源:发表于2015-08-14 11:24 被阅读358次

    参考:[swift之mutating关键字]
    (http://blog.csdn.net/tounaobun/article/details/39694233) 来自CSDN
    将 PROTOCOL 的方法声明为 MUTATING 来自王巍

    在阅读iOS8 Swift Programming Cookbook 的时候有个说结构体和类的例子

    结构体的例子如下:

    import UIKit
    
    struct Person {
     var firstName,lastName:String
     // mutating 啥意思??
     mutating func setFirstNameTo(firstName:String){
      self.firstName = firstName
     }
    }
    
    @UIApplicationMain
    
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
     var window: UIWindow?
    
     
     func changeFirstNameOf(var person:Person,to:String){
      person.setFirstNameTo(to)
     }
     
     
    
     func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
      // Override point for customization after application launch.
      
      
      var vandad = Person(firstName: "Vandad", lastName: "Nahavandipoor")
      changeFirstNameOf(vandad, to: "VANDAD")
      
      
      
      return true
     }
    }
    

    我对mutating不懂,就简单搜了下可以参考下:
    http://swifter.tips/protocol-mutation/

    并找到了一篇博客,也有对mutating的解释

    在swift中,包含三种类型(type):structure,enumeration,class
    其中structure和enumeration是值类型(value type),class是引用类型(reference type)
      但是与objective-c不同的是,structure和enumeration也可以拥有方法(method),其中方法可以为实例方法(instance method),也可以为类方法(type method),实例方法是和类型的一个实例绑定的。
      在swift官方教程中有这样一句话:

    “Structures and enumerations are value types. By default, the properties of a value type
    cannot be modified from within its instance methods.”

    摘录来自: Apple Inc. “The Swift Programming Language”。 iBooks. https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewBook?id=881256329

    大致意思就是说,虽然结构体和枚举可以定义自己的方法,但是默认情况下,实例方法中是不可以修改值类型的属性。

    举个简单的例子,假如定义一个点结构体,该结构体有一个修改点位置的实例方法:

    struct Point {
        var x = 0, y = 0
        
        func moveXBy(x:Int,yBy y:Int) {
            self.x += x
            // Cannot invoke '+=' with an argument list of type '(Int, Int)'
            self.y += y
            // Cannot invoke '+=' with an argument list of type '(Int, Int)'
        }
    }
    

    编译器抛出错误,说明确实不能在实例方法中修改属性值。
    为了能够在实例方法中修改属性值,可以在方法定义前添加关键字mutating

    struct Point {
        var x = 0, y = 0
        
        mutating func moveXBy(x:Int,yBy y:Int) {
            self.x += x
            self.y += y
        }
    }
    
    var p = Point(x: 5, y: 5)
    
    p.moveXBy(3, yBy: 3)
    

    另外,在值类型的实例方法中,也可以直接修改self属性值。

    enum TriStateSwitch {
        case Off, Low, High
        mutating func next() {
            switch self {
            case Off:
                self = Low
            case Low:
                self = High
            case High:
                self = Off
            }
        }
    }
    var ovenLight = TriStateSwitch.Low
    ovenLight.next()
    // ovenLight is now equal to .High
    ovenLight.next()
    // ovenLight is now equal to .Off”
    

    TriStateSwitch枚举定义了一个三个状态的开关,在next实例方法中动态改变self属性的值。

    当然,在引用类型中(即class)中的方法默认情况下就可以修改属性值,不存在以上问题。

    [参考资料: The Swift Programming Language ] from iBook

    相关文章

      网友评论

        本文标题: swift之mutating关键字

        本文链接:https://www.haomeiwen.com/subject/kccwqttx.html