OC声明变量的各种方式

一、 @interface中直接声明,可见性protected(子类可继承)

     这种直接在@interface中直接声明变量的方法,需要手动写get和set方法(也就是取值和设置值的方法)。专业一点,叫存取器。存取器(accessor):指用于获取和设置实例变量的方法。用于获取实例变量值的存取器是getter,用于设置实例变量值的存取器是setter.

// Car.h
//此部分只提供了声明,具体的实现还是需要到@implementation里面实现

#import 

@interface Car : NSObject
{
    // 实例变量
    NSString *carName;
    NSString *carType;
}

// setter                                   
- (void)setCarName:(NSString *)newCarName; 

// getter
- (NSString *)carName;

// setter
- (void)setCarType:(NSString *)newCarType;

// getter
- (NSString *)carType;

@end  

实现举例:

#import "Car.h"

@implementation Car
// setter
- (void)setCarType:(NSString *)newCarType
{
    carType = newCarType;
}
@end

// getter
- (NSString *)carType
{
    return carType;
}

调用举例

Car *car = [[Car alloc] init];
[car setCarName:@"Jeep Cherokee"];
[car setCarType:@"SUV"];
NSLog(@"The car name is %@ and the type is %@",[car carName],[car carType]);  

二、@property声明,可见性public

// Car.h

#import 

@interface Car : NSObject
{
    // 实例变量
    NSString *carName;
    NSString *carType;
}

@property(nonatomic,strong) NSString *carName;
@property(nonatomic,strong) NSString *carType;

@end

使用举例:

 // 点语法
//car.carName = @"Jeep Compass";
//car.carType = @"SUV";
//NSLog(@"The car name is %@ and the type is %@",car.carName,car.carType);

// 消息语法
[car setCarName:@"Jeep Compass"];
[car setCarType:@"SUV"];
NSLog(@"The car name is %@ and the type is %@",[car carName],[car carType]);

说明:

  1. 在老式的代码中,@property只能写在@interface @end中(只提供存取器的声明方法),@synthesize只能写在@implementation @end中(@synthesize提供存取器的实现),自从xcode 4.4后,@property就独揽了@property和@synthesize的功能(提供声明和实现)。

  2. @property NSString *carName;这句话完成了3个功能:1)生成一个_carName的成员变量;2)生成_carName成员变量的get和set方法的声明;3)生成_carName成员变量set和get方法的实现;。

  3. 如果自定了,则编译器不自动生成。

三、私有属性声明

三种方式:

1) @implementantion并带花括号(DO IT)

说明:实现过程中需要用到的变量,放到这个里面啦

#import "Person.h"

@implementation Person
{
    int age;
    NSString *name;
}

- (id)init
{
    self = [super init];
    if (self)
    {
        age = 40;
        name = @"Holli";
    }
    return self;
}
@end

2)@implementation中不带花括号(DONT’T DO IT)

说明:最好不用,这类变量属于全局变量,不适于任何一个实例,而是属于类,类似于C++里面的静态变量。

#import "Person.h"

@implementation Person

int age;
NSString *name;


- (id)init
{
    self = [super init];
    if (self)
    {
        age = 40;
        name = @"Holli";
    }
    return self;
}
@end

3)拓展(extention)(DO IT WHEN NECESSARY)

注意:@interce的类名后面加了圆括号,也就是拓展啦。

#import "Person.h"

@interface Person()
{
    int age;
    NSString *name;
}
@end

@implementation Person

- (id)init
{
    self = [super init];
    if (self)
    {
        age = 40;
        name = @"Holli";
    }
    return self;
}
@end

参考文章:

  1. 关于@property和@synthesizehttps://www.cnblogs.com/wendingding/p/3706430.html
  2. @property https://www.devtalking.com/articles/you-should-to-know-property/
  3. 成员变量的可见性:https://blog.csdn.net/ZZB_Bin/article/details/77718992
  4. 私有变量:https://stackoverflow.com/questions/13566862/where-to-put-ivars-in-modern-objective-c

你可能感兴趣的:(Object-C,objective-c)