加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 02:27:21 所属栏目:百科 来源:网络整理
导读:我想实现我的自定义MKAnnotation.我看了一下MKAnnotation协议(MKAnnotation.h). 它如下: //// MKAnnotation.h// MapKit//// Copyright (c) 2009-2014,Apple Inc. All rights reserved.//protocol MKAnnotation : NSObjectProtocol { // Center latitude and
我想实现我的自定义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方法,我试图分配给我的坐标属性不能分配给’self’中的’coordinate’显然是因为它是一个只读属性.

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

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

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

您应该只需添加一个setter并将信息存储在内部坐标值中.由于你有一个getter,它仍然符合协议:
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()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读