gpt4 book ai didi

ios - 在 Objective-C 中定义变量、setter 和 getter

转载 作者:行者123 更新时间:2023-11-28 20:11:00 25 4
gpt4 key购买 nike

我很难让自己了解 Objective-C 和 iOS 编程。我试图搜索这个问题,但结果解释得不够简单。

所以我有一个 viewController.h 和一个 viewController.m。在 .h 中,我在 @interface@end

之间添加了我的标签/按钮等
@property ( nonatomic, strong   ) UILabel             *facebookLoginTextLabel;
@property ( nonatomic, strong ) UIButton *facebookLoginButton;

.m 中,我只是在 @implementation 和第一个方法之间综合了它们

@synthesize facebookLoginTextLabel  = _facebookLoginTextLabel;
@synthesize facebookLoginButton = _facebookLoginButton;

我现在面临的问题是,其他值怎么办?举个例子。我有一个 bool 值。我在哪里声明它?我该如何设置?我如何获得值(value)?这对我来说太困惑了。 NSInteger 也一样吗?

我想根据 ifcase 将 NSInteger 设置为 0、1 或 2。我该如何实现?我试过这样获取和设置

.h
@property ( nonatomic ) CGFloat *width;
.m
@synthesize width = _width;

viewDidLoadMethod
_width = [self returnScreenWidth];

returnScreenWith method
- ( CGFloat ) returnScreenWidth {
return [UIScreen mainScreen].bounds.size.width;
}

这行不通。为什么?我如何设置,如何获取?如何声明变量? PHP 和 Android 开发人员的方式太困惑了。

最佳答案

首先,它是 CGFloat width 而不是 CGFloat *width 因为它是一个 C 结构,而不是您在头文件中定义属性的 Objective C 类:

@property (nonatomic) CGFloat width;

无需访问 under-score 实例变量,只需使用 self.width 即可使用 self 访问 @property

关于ios - 在 Objective-C 中定义变量、setter 和 getter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20264852/

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