《Effective Objective-C 2.0 》 閱讀筆記 item18

第18條:盡量使用不可變對(duì)象

  • 盡量把對(duì)外公布出來的屬性設(shè)為只讀,而且只在確有必要時(shí)才將屬性對(duì)外公布。
  • 對(duì)象中表示各種collection的那些屬性,通常情況下,提供一個(gè)readonly屬性供外界使用,該屬性將返回一個(gè)不可變的collection,而此collection則是內(nèi)部那個(gè)可變collection的一份拷貝。
  • 強(qiáng)調(diào):不要在返回的對(duì)象上查詢類型以確定是否可變。

例子

EOCPerson類

/* EOCPerson頭文件 */
#import <Foundation/Foundation.h>

@interface EOCPerson : NSObject

@property(nonatomic ,copy, readonly) NSString *firstName;
@property(nonatomic ,copy, readonly) NSString *lastName;
@property(nonatomic ,strong, readonly) NSSet *friends; // 不可變set
- (id)initWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName;
- (void)addFriend:(EOCPerson *)person;
- (void)removeFriend:(EOCPerson *)person;
@end

#import "EOCPerson.h"

@interface EOCPerson ()
// 改為readwrite屬性
@property(nonatomic ,copy, readwrite) NSString *firstName;
@property(nonatomic ,copy, readwrite) NSString *lastName;

@end

/* EOCPerson實(shí)現(xiàn)文件 */
@implementation EOCPerson
{// 實(shí)例變量
    NSMutableSet *_internalFriends; // 可變set
}

- (NSSet *)friends{
    return [_internalFriends copy]; // 拷貝一份不可變的set
}

- (void)addFriend:(EOCPerson *)person{
    [_internalFriends addObject:person];
}

- (void)removeFriend:(EOCPerson *)person{
    [_internalFriends removeObject:person];
}

- (id)initWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName{
    if (self = [super init]) {
#warning 不需要再copy一份
        _firstName = firstName;
        _lastName = lastName;
        _internalFriends = [NSMutableSet new]; // 延遲初始化
    }
    return self;
}

// 覆寫description方法
- (NSString *)description{
    return [NSString stringWithFormat:@"%@ %@", self.firstName, self.lastName];
}
@end

main函數(shù)

#import <Foundation/Foundation.h>
#import "EOCPerson.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // 1.創(chuàng)建3個(gè)EOCPerson對(duì)象
        EOCPerson *person = [[EOCPerson alloc] initWithFirstName:@"Bob" andLastName:@"Simth"];
        NSLog(@"person = %@ %@", person.firstName, person.lastName);
        
        EOCPerson *person2 = [[EOCPerson alloc] initWithFirstName:@"Bill" andLastName:@"Jobs"];
        NSLog(@"person2 = %@ %@", person2.firstName, person2.lastName);
        
        EOCPerson *person3 = [[EOCPerson alloc] initWithFirstName:@"Jane" andLastName:@"Galloway"];
        NSLog(@"person3 = %@ %@", person3.firstName, person3.lastName);
        
        // 2.通過addFriend:方法給person添加2個(gè)朋友(person2&person3)
        [person addFriend:person2];
        [person addFriend:person3];
        NSLog(@"the friends of person = %@", person.friends);
        
    }
    return 0;
}

輸出結(jié)果:

Snip20160318_30.png

要點(diǎn)

  • 盡量創(chuàng)建不可變的對(duì)象
  • 若某屬性僅可于對(duì)象內(nèi)部修改,則在“class-continue分類”中將其由readonly屬性擴(kuò)展為readwrite屬性
  • 不要可變的collection作為屬性公開,而應(yīng)提供相關(guān)方法,以此修改對(duì)象中的可變collection。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容