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

时间:2022-09-07 20:13:14

I have a hard time getting myself introduced to Objective-C and iOS programming. I tried to search for this problem, but the results weren't explained simple enough.

我很难将自己介绍给Objective-C和iOS编程。我试图搜索这个问题,但结果不够简单。

So I have a viewController.h and a viewController.m. In the .h I add my Labels/Buttons etc. between @interface and @end

所以我有一个viewController.h和一个viewController.m。在.h中我在@interface和@end之间添加我的标签/按钮等

@property ( nonatomic, strong   ) UILabel             *facebookLoginTextLabel;
@property ( nonatomic, strong   ) UIButton            *facebookLoginButton;

And in the .m I'm simply synthesizing them between @implementation and the first method

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

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

The problem I'm facing now, is what to do with other values? As an example. I have a boolean value. Where do I declare it? How do I set it? How do I get the value? It's way too confusing for me. The same counts for an NSInteger?

我现在面临的问题是如何处理其他价值观?举个例子。我有一个布尔值。我在哪里申报?我该如何设置?我如何获得价值?这对我来说太混乱了。 NSInteger的计数相同吗?

I want, dependent on the ifcase, set the NSInteger to 0, 1 or 2. How do I achieve this? I tried getting and setting like this

我想,依赖于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;
}

This doesn't work. Why? How do I set, how do I get? How do I declare variables? Way too confusing from a PHP and an Android-Developer.

这不起作用。为什么?我如何设置,如何获得?我如何声明变量?来自PHP和Android-Developer的方式太混乱了。

2 个解决方案

#1


2  

First of all, it is CGFloat width not CGFloat *width as it is a C struct, not an Objective C class where you define the property in the header file:

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

@property (nonatomic) CGFloat width;

Instead of accessing the under-score instance variable, just use self.width whereby you access the @property using self.

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

#2


1  

Remember, all basic/primitive data types like (int, float, char, BOOL) need not be used as you are doing, always use assign.

请记住,所有基本/原始数据类型(如int,float,char,BOOL)都不需要像你一样使用,总是使用assign。

Yes BOOL is a primitive, as this is typedef to char.

是BOOL是一个原语,因为这是char的typedef。

It should be defined as :

它应该被定义为:

@property (assign) <primitivetype> propertyName;

Also to note down, with new compiler (I hope you are using Xcode4.2 onwards i.e LLVM),

另外要注意,使用新的编译器(我希望你使用的是Xcode4.2,即LLVM),

@synthesize propertyName is done automatically by the compiler as

@synthesize propertyName由编译器自动完成

@synthesize propertyName = _propertyName;

atomic and nonatomic depends on your requirement, so is the pointer to int or float.

原子和非原子取决于你的要求,指向int或float的指针也是如此。

#1


2  

First of all, it is CGFloat width not CGFloat *width as it is a C struct, not an Objective C class where you define the property in the header file:

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

@property (nonatomic) CGFloat width;

Instead of accessing the under-score instance variable, just use self.width whereby you access the @property using self.

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

#2


1  

Remember, all basic/primitive data types like (int, float, char, BOOL) need not be used as you are doing, always use assign.

请记住,所有基本/原始数据类型(如int,float,char,BOOL)都不需要像你一样使用,总是使用assign。

Yes BOOL is a primitive, as this is typedef to char.

是BOOL是一个原语,因为这是char的typedef。

It should be defined as :

它应该被定义为:

@property (assign) <primitivetype> propertyName;

Also to note down, with new compiler (I hope you are using Xcode4.2 onwards i.e LLVM),

另外要注意,使用新的编译器(我希望你使用的是Xcode4.2,即LLVM),

@synthesize propertyName is done automatically by the compiler as

@synthesize propertyName由编译器自动完成

@synthesize propertyName = _propertyName;

atomic and nonatomic depends on your requirement, so is the pointer to int or float.

原子和非原子取决于你的要求,指向int或float的指针也是如此。