gpt4 book ai didi

iphone - 澄清 objective-c 中的属性

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:15:14 25 4
gpt4 key购买 nike

抱歉这个简单的问题。
当我在 h 文件 中看到属性的定义,但在类 @interface 范围的外部 时,这是什么意思?

@property (nonatomic, readonly) RMMapContents *mapContents;

代码如下:

@class RootViewController;
@class RMMapContents;

@interface MapTestbedAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;

//MAIN VIEW
//==============

RootViewController *rootViewController;


// NETWORK DATA
// =============

NSMutableArray *photoTitles; // Titles of images
NSMutableArray *photoSmallImageData; // Image data (thumbnail)
NSMutableArray *photoURLsLargeImage; // URL to larger image
NSMutableData *receivedData;
NSURLConnection *theConnection;
NSURLRequest *request;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
@property (nonatomic, readonly) RMMapContents *mapContents;


@end

在一个函数中我看到了这一行:

- (void)foo:(xyz *)abc{
..
RMMapContents *mapContents = [self mapContents];
..
}

所以,从 C++ 来看,mapContents 似乎不是一个全局范围的变量(毕竟,这就是他们称它们为属性的原因,对吧?),但没有定义相同的变量函数里面再命名是不是有点奇怪?

我希望有人能在这里澄清一点。
谢谢!

最佳答案

@interface block 的范围扩展到@end 关键字并且不限于大括号{}。

所以 @property 声明非常位于 @interface 的范围内,就像正确回答的 cli_hlt 一样,它充当 mapContents 属性的 setter 和 getter 方法的替代品。

因此名为 mapContents 的属性将具有如下所示的 setter 和 getter:

- (void)setMapContents; //setter

- (RMMapContents *)mapContents; //getter

并且可以使用这些方法从类中访问:

[self setMapContents:newContents];

RMMapContents *contents = [self mapContents];

关于iphone - 澄清 objective-c 中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10993091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com