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

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

概述我想实现我的自定义MKAnnotation.我看了一下MKAnnotation协议(MKAnnotation.h). 它如下: //// MKAnnotation.h// MapKit//// Copyright (c) 2009-2014, Apple Inc. All rights reserved.//protocol MKAnnotation : NSObjectPro 我想实现我的自定义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: CLLocationCoordinate2Dvar 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 exampleprotocol 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 protocolfunc 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 classvar 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()
总结

以上是内存溢出为你收集整理的在Swift中初始化期间设置只读存储属性的值全部内容,希望文章能够帮你解决在Swift中初始化期间设置只读存储属性的值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1089810.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存