1

在 MapBox 的 Route-Me 中,我看到 iVars 是这样实现的:

@implementation RMMapView
{
    BOOL _delegateHasBeforeMapMove;
    BOOL _delegateHasAfterMapMove;
    BOOL _delegateHasBeforeMapZoom;
    BOOL _delegateHasAfterMapZoom;
    BOOL _delegateHasMapViewRegionDidChange;
    BOOL _delegateHasDoubleTapOnMap;
    BOOL _delegateHasSingleTapOnMap;
    BOOL _delegateHasSingleTapTwoFingersOnMap;
    BOOL _delegateHasLongSingleTapOnMap;
    BOOL _delegateHasTapOnAnnotation;
    BOOL _delegateHasDoubleTapOnAnnotation;
    BOOL _delegateHasTapOnLabelForAnnotation;
    BOOL _delegateHasDoubleTapOnLabelForAnnotation;
    BOOL _delegateHasShouldDragMarker;
    BOOL _delegateHasDidDragMarker;
    BOOL _delegateHasDidEndDragMarker;
    BOOL _delegateHasLayerForAnnotation;
    BOOL _delegateHasWillHideLayerForAnnotation;
    BOOL _delegateHasDidHideLayerForAnnotation;

    BOOL _constrainMovement;
    RMProjectedRect _constrainingProjectedBounds;

    float _lastZoom;
    CGPoint _lastContentOffset, _accumulatedDelta;
    BOOL _mapScrollViewIsZooming;
}

也就是说,它是在 .m 文件而不是 .h 文件中声明的。如果我对这个 RMMapView 进行子类化,我看不到这些 iVar。有没有办法让它在子类中可见而不对世界可见?

我打算保留原始代码,因此选择子类化并从那里修改行为。但也许只是直接更改文件并通过 git 从主存储库更新代码是更好的方法吗?

4

2 回答 2

1

在 Objective-C(与其他 OO 语言一样)中,大多数开发人员将实例变量视为定义类的实现细节。它们不应该被其他类直接访问。

在这种情况下,实例变量是在实现中声明的,这强烈暗示作者RMMapView不想将变量暴露给外部代码。

于 2012-05-23T09:55:50.340 回答
0

如果你真的希望实例变量对子类可见,你需要在.@interface而不是@implementation.

@interface RMMapView : NSObject
{
@protected
    BOOL _delegateHasBeforeMapMove;
    BOOL _delegateHasAfterMapMove;
    BOOL _delegateHasBeforeMapZoom;
    BOOL _delegateHasAfterMapZoom;
    BOOL _delegateHasMapViewRegionDidChange;
    BOOL _delegateHasDoubleTapOnMap;
    BOOL _delegateHasSingleTapOnMap;
    BOOL _delegateHasSingleTapTwoFingersOnMap;
    BOOL _delegateHasLongSingleTapOnMap;
    BOOL _delegateHasTapOnAnnotation;
    BOOL _delegateHasDoubleTapOnAnnotation;
    BOOL _delegateHasTapOnLabelForAnnotation;
    BOOL _delegateHasDoubleTapOnLabelForAnnotation;
    BOOL _delegateHasShouldDragMarker;
    BOOL _delegateHasDidDragMarker;
    BOOL _delegateHasDidEndDragMarker;
    BOOL _delegateHasLayerForAnnotation;
    BOOL _delegateHasWillHideLayerForAnnotation;
    BOOL _delegateHasDidHideLayerForAnnotation;

    BOOL _constrainMovement;
    RMProjectedRect _constrainingProjectedBounds;

    float _lastZoom;
    CGPoint _lastContentOffset, _accumulatedDelta;
    BOOL _mapScrollViewIsZooming;
}

@protected顶部的 限定可见性,它也可以是@public@private@protected是默认值,但最好明确地将其放入以显示您故意希望这些 ivars 对子类可见。

于 2012-05-23T10:34:23.573 回答