在Swift中初始化期间设置只读存储属性的值

时间:2014-06-09 23:27:29

标签: swift

我想实现我的自定义MKAnnotation。我看了一下MKAnnotation协议(MKAnnotation.h)。 它如下:

//
//  MKAnnotation.h
//  MapKit
//
//  Copyright (c) 2009-2014, Apple Inc. All rights reserved.
//

protocol MKAnnotation : NSObjectProtocol {

    // Center latitude and longitude of the annotation view.
    // The implementation of this property must be KVO compliant.
    var coordinate: CLLocationCoordinate2D { get }

    // Title and subtitle for use by selection UI.
    @optional var title: String! { get }
    @optional var subtitle: String! { get }

    // Called as a result of dragging an annotation view.
    @optional func setCoordinate(newCoordinate: CLLocationCoordinate2D)
}

请注意坐标属性(这是一个只读存储属性)。 以下是我实施此协议的方式:

class RWDefaultPin: NSObject, MKAnnotation {
    var title:String = ""
    var subtitle:String = ""
    var groupTag:String = ""
    var coordinate: CLLocationCoordinate2D { get {
        return self.coordinate // this is obviously wrong because property's trying to return itself
    } };


    init(coordinate:CLLocationCoordinate2D) {
        super.init()
        self.coordinate = coordinate
    }
}

但显然编译器投诉我的init方法我试图分配给我的坐标属性Cannot assign to 'coordinate' in 'self'显然是因为它是一个只读属性。

以前在Objective-C中我们可以克服这个问题,因为属性由ivars支持。

我希望Swift中有访问修饰符,所以我可以在我的类中定义一个私有属性,并在init上设置它的值,并在get动作中返回它的值,但是没有这样的东西!

我不知道如何在Swift中解决这个问题,或者我可能需要将其打开并将我的坐标更改为可读/可写?

2 个答案:

答案 0 :(得分:8)

您应该只需向其添加setter并将信息存储在内部坐标值中。由于你有一个吸气剂,它仍然符合协议:

var innerCoordinate: CLLocationCoordinate2D

var coordinate: CLLocationCoordinate2D { 
    get {
        return self.innerCoordinate
    } 
    set {
        self.innerCoordinate = newValue
    }
};

init(coordinate:CLLocationCoordinate2D) {
    super.init()
    self.innerCoordinate = coordinate
}

这实际上是我如何实现只读和私有属性(使用协议和工厂模式)。我使用公共接口和具有私有变量和setter的类设置协议。它实际上是设置代码的超级干净方式(并且避免了Swift中缺少受保护/私有属性)。


以下是我所谈论的一个抽象的例子(如果你关心的话):

// this is your MKAnnotation in this example
protocol SomeProtocol {
    var getterProperty: String { get }
    var setterProperty: String { set get }

    func publicFunction(someStirng: String) -> ();

}

// setup a function that returns a class conforming to your needed protocol
func SomeClassMaker() -> SomeProtocol {
    // your internal class that no one can access unless by calling the maker function
    class SomeClassInternal: NSObject, SomeProtocol {

        // private and no one can get to me!
        var innerSetterProperty = "default setter";

        var getterProperty = "default getter"

        var setterProperty: String {
            get {
                return self.innerSetterProperty;
            }
            set {
                "hit"
                self.innerSetterProperty = newValue
            }
        }

        func publicFunction(someString: String) -> ()  {
            // anyone get me
            self.getterProperty = someString;
        }

        func privateFunction() -> () {
            // no one can get me except internal functions
        }

    }

    return SomeClassInternal();
}


// create the class
var classInstance = SomeClassMaker();

// totally fine!
classInstance.setterProperty = "secret string"
// prints "secret string"
classInstance.setterProperty;

// error! no public setter for "getter"
classInstance.getterProperty = "another secret"

classInstance.publicFunction("try secret again")
// prints "try secret again"
let blahed = classInstance.getterProperty

// error!
classInstance.privateFunction()

答案 1 :(得分:5)

即使协议中的属性为{ get },也就是建立最低标准。将其定义为读写是完全可以接受的:

class MyAnnotation:NSObject, MKAnnotation
{
    var coordinate:CLLocationCoordinate2D

    init(coordinate:CLLocationCoordinate2D) {
        self.coordinate = coordinate
    }
}

或者,如果你真的想把它保持为只读,你可以使用let:

class MyAnnotation:NSObject, MKAnnotation
{
    let coordinate:CLLocationCoordinate2D

    init(coordinate:CLLocationCoordinate2D) {
        self.coordinate = coordinate
    }
}