[ObjectiveC] id, selector, NS_ASSUME_NONNULL_BEGIN

Zion·2023년 8월 25일
0

ObjectiveC

목록 보기
1/1
post-thumbnail

Types

id

This type is defined as a pointer to an object.-in reality, a ponter to the instance variables of the object, the object's unique data.

id & instancetype

Docs - id & instancetype
-> instancetype은 class의 명확한 type을 returen하는 반면, id는 꽤 모호한 타입임.

Selector

A selector is the name used to select a method to execute for an object, or the unique identifier that replaces the name when the source code compiled.

A selector by itself doesn't do anything. It simply identifies a method.

The only thin that makes the slector method name differenct from a plain string is that the compiler makes sure that selectors are unique.

What makes a selector useful is that (in conjuction with the runtime) it acts like a dynamic func pointer that, for a given name, automatically points to the implementation of a method appropriate for whichever class it's used with

Getting a Selector(2)

Docs - Selector

  • At compile time.

SEL aSelector = @selector(methodName);

  • At runtime

SEL aSelector = NSSelctorFromString(@"methodName");

Using a Selector

SEL aSelector = @selector(run);
[aDog performSelector: aSelector];
[anAthlete performSelector: aSelector];
[aComputerSimulation performSelector: aSelector];

나는 Selector를 함수의 변수타입으로 이해했다.

Etc.

NS_ASSUME_NONNULL_BEGIN & NS_ASSUME_NONNULL_END

To ease adoption of the new annotations, you can mark certain regions of your ObC header files as audited for nullability.
Within these regions, any simple pointer type will be assumed to be nonnull.

  • ~_BEGIN으로 선언으로 구간시작해서 _END 선언 해서 구간 닫음.

함수 선언부에 왜 NONNULL이라고 가정해야하는지 잘 이해는 가지 않는다.

NONNULL이라 가정해야할 곳은 사용하는 부분이라고 생각하는데.. 이건 내가 ObjectiveC를 아직 잘 몰라서 의문이 생긴거 같다.

그래서 검색해봄


대답 :

참고) Nullability and Objective-C
요지 : .h에서 method선언 할 때 parameter non-null이어야하면 nonnull 적어줘야하는데, NS_ASSUME_NONNULL_BEGIN & NS_ASSUME_NONNULL_END로 범위 설정한 내부에서는 nonnull 붙이지 않아도 non-null이라고 가정함!

profile
어제보다만 나아지는

0개의 댓글