티스토리 뷰

Item 6: Understand Properties

 

프로퍼티 속성 중 Memory-Management 관련된 속성인데

- assign : The setter is a simple assign operation used for scalar types, such as CGFloat or NSInteger.

- strong : This designates that the property defines an owning relationship. When a new value is set, it is first retained, the old value is released, and then the value is set.

- weak : This designates that the property defines a nonowning relationship. When a new value is set, it is not retained; nor is the old value released. This is similar to what assign does, but the value is also nilled out when the object pointed to by the property at any time is destroyed.

- copy : This designates an owning relationship similar to strong; however, instead of retaining the value, it is copied. This is often used when the type is NSString* to preserve encapsulation, since the value passed into the setter might be an instance of the subclass NSMutableString. If it’s this mutable variant, the value could be mutated after the property is set, without the object’s knowing. So an immutable copy is taken to ensure that the string cannot change from underneath the object. Any object that may be mutable should take a copy.

 

얘네는 setter와 관계있고, getter와는 관련이 없다.

 

strong/weak은 swift와 동일하다고 생각하면 되고,

assign은 setter에서 객체를 지정받을 때, 그냥 값을 대입하는 방식이라고 생각하면 된다. "number = n" 이런 식으로.

assign은 객체의 retain count를 증가시키지 않는데, 외부에서 retain 외부에서 참조 카운트를 감소시켜 객체가 해제될 위험이 있으므로,

객체가 아닌 int와 같은 primitive type에 적합하다.

 

 Use nonatomic on iOS, since performance is severely impacted if atomic is used.

 

 

Item 7: Access Instance Variables Primarily Directly When Accessing Them Internally

 

Item 8: Understand Object Equality

 

'==' operator compares the pointers themselves rather than the objects to which they point.

Instead, you should use the 'isEqual:'

NSString *foo = @"Badger 123";
NSString *bar = [NSString stringWithFormat:@"Badger %i", 123];
BOOL equalA = (foo == bar); //< equalA = NO
BOOL equalB = [foo isEqual:bar]; //< equalB = YES
BOOL equalC = [foo isEqualToString:bar]; //< equalC = YES

 

 

Item 9: Use the Class Cluster Pattern to Hide Implementation Detail

 

id maybeAnArray = /* ... */;
if ([maybeAnArray class] == [NSArray class]) {
    // Will never be hit
}

Since instances returned from NSArray’s initializers are instances of internal types behind the class cluster public facade.

Instead of checking equality of class objects, you should do the following:

id maybeAnArray = /* ... */;
if ([maybeAnArray isKindOfClass:[NSArray class]]) {
    // Will be hit
}

 

 The Class Cluster pattern can be used to hide implementation detail behind a simple public facade.

 Class clusters are commonly used in the system frameworks.

 

 

Item 10: Use Associated Objects to Attach Custom Data to Existing Classes

 

Item 11: Understand the Role of objc_msgSend

 

 

- id 타입은 포인터 타입을 포함해서 정의한 타입

- objc의 모든 오브젝트 변수들은 모두 포인터 타입이기 때문에 id 타입으로는 어떤 것이든 지칭해서 사용할 수 있다.

 

 

Item 12: Understand Message Forwarding

 

Item 13: Consider Method Swizzling to Debug Opaque Methods

 

Item 14: Understand What a Class Object Is

 

Every Objective-C object instance is a pointer to a blob of memory. That's why you see the '*' next to the type when declaring a variable:

NSString *pointerVariable = @"Some string";

 

Then generic object type, 'id', is already a pointer in itself, so you use it like so:

id genericTypedString = @"Some string";

This definition is semantically the same as if the variable were of type NSString*. The only difference with specifying the type fully is that the compiler can help and warn if you attempt to call a method that doesn’t exist for instances of that class.

 

+ isa : which is a pointer to the class that the object is an instance of (인스턴스 객체는 isa로 자신이 속한 클래스 객체를 참조. "is a" 라는 의미를 갖고 있음). 'isa' pointer describes the type of an instance.

 

Class hierarchy for instances of  SomeClass , which inherits from  NSObject , including the metaclass hierarchy

 

 

 


https://www.oreilly.com/library/view/effective-objective-c-20/9780133386950/

 

Effective Objective-C 2.0: 52 Specific Ways to Improve Your iOS and OS X Programs

Write Truly Great iOS and OS X Code with Objective-C 2.0! Effective Objective-C 2.0 will help you harness all of Objective-C’s expressive power to write OS X or iOS code … - Selection from Effective Objective-C 2.0: 52 Specific Ways to Improve Your iOS

www.oreilly.com

 

이 책으로 공부 중이고, 기억하면 좋을 것 같은 부분을 발췌 + 공부 + 정리합니다.

'Objective-C 2.0' 카테고리의 다른 글

5. Memory Management  (0) 2022.05.10
4. Protocols and Categories  (0) 2022.03.04
3. Interface and API Design  (0) 2022.01.25
1. Accustoming Yourself to Objective-C  (0) 2022.01.17
[Objective-C 2.0] Pointer with Objects  (0) 2022.01.14
댓글