OC聲明變量的各種方式

一、 @interface中直接聲明,可見性protected(子類可繼承)

     這種直接在@interface中直接聲明變量的方法,需要手動寫get和set方法(也就是取值和設置值的方法)。專業一點,叫存取器。存取器(accessor):指用於獲取和設置實例變量的方法。用於獲取實例變量值的存取器是getter,用於設置實例變量值的存取器是setter.

// Car.h
//此部分只提供了聲明,具體的實現還是需要到@implementation裏面實現

#import <Foundation/Foundation.h>

@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 <Foundation/Foundation.h>

@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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章