Objective-C의 선택기?
첫째, 선택기가 무엇인지 실제로 이해하지 못합니다. 내 이해에서, 그것은 메소드의 이름이며, 'SEL'유형의 클래스에 할당 한 다음 respondToSelector와 같은 메소드를 실행하여 수신자가 해당 메소드를 구현하는지 확인할 수 있습니다. 더 나은 설명을 제공 할 수 있습니까?
둘째, 지금까지 다음 코드가 있습니다.
NSString *thing = @"Hello, this is Craig";
SEL sel = @selector(lowercaseString:);
NSString *lower = (([thing respondsToSelector:sel]) ? @"YES" : @"NO");
NSLog (@"Responds to lowercaseString: %@", lower);
if ([thing respondsToSelector:sel]) //(lower == @"YES")
NSLog(@"lowercaseString is: %@", [thing lowercaseString]);
그러나 thing
분명히 NSString의 일종 이지만 소문자 문자열에 응답해야하지만 'yes'를 반환하는 조건부 'respondsToSelector'를 얻을 수 없습니다 ...
메소드 이름에 매우주의해야합니다. 이 경우 메소드 이름은 " lowercaseString
"가 아니라 " lowercaseString:
"입니다 (콜론이 없음에 유의하십시오). 개체가 메시지에 응답 하지만 메시지에는 응답 하지 NO
않기 때문에 반환되는 이유 입니다.NSString
lowercaseString
lowercaseString:
콜론 추가시기를 어떻게 알 수 있습니까? 콜론을 호출 할 때 콜론을 추가하려면 메시지 이름에 콜론을 추가하십시오. 인수가 하나 인 경우 발생합니다. 인수가 0 인 경우 (와 마찬가지로 lowercaseString
) 콜론이 없습니다. 둘 이상의 인수가 필요한 경우에서와 같이 추가 인수 이름을 콜론과 함께 추가해야합니다 compare:options:range:locale:
.
문서를 보고 후행 콜론의 유무를 확인할 수도 있습니다 .
선택기 는 컴파일 된 코드에서 직접 메소드를 참조하는 효율적인 방법입니다. 컴파일러는 실제로 SEL에 값을 지정합니다.
다른 사람들은 이미 q의 두 번째 부분을 다루었으므로 끝에있는 ':'는 찾고있는 것과 다른 서명 (이 경우 서명이 존재하지 않음)과 일치합니다.
당신이 원하는 때문 @selector(lowercaseString)
이 아니라 @selector(lowercaseString:)
. 미묘한 차이가 있습니다. 두 번째 것은 매개 변수를 의미하지만 (끝의 콜론에 유의하십시오) - [NSString lowercaseString]
매개 변수는 사용하지 않습니다.
이 경우 선택기의 이름이 잘못되었습니다. 여기서 콜론은 메소드 서명의 일부입니다. 메서드가 하나의 인수를 취한다는 것을 의미합니다. 나는 네가 원한다고 믿는다
SEL sel = @selector(lowercaseString);
NSString의 메소드는 lowercaseString
(1 개의 인수)가 아닌 lowercaseString:
( 0 개의 인수)입니다.
콜론을 함수 이름의 일부로 생각하지 말고 구분 기호로 생각하십시오. 분리 할 항목이 없으면 (함수와 함께 사용할 값이 없음) 필요하지 않습니다.
왜 그런지 잘 모르겠지만이 모든 OO 자료는 Apple 개발자에게 외국인 것 같습니다. Visual Studio Express를 잡고 그와 함께 놀아 보는 것이 좋습니다. 하나는 다른 것보다 낫기 때문에 디자인 문제와 사고 방식을 보는 좋은 방법입니다.
처럼
introspection = reflection
+ before functions/properties = static
- = instance level
문제를 다른 방식으로 보는 것이 항상 좋으며 프로그래밍은 궁극적 인 퍼즐입니다.
Apple 문서에 대한 나의 이해에서 선택기는 호출하려는 메소드의 이름을 나타냅니다. 선택기의 좋은 점은 호출 할 정확한 메소드가 다른 경우에 사용할 수 있다는 것입니다. 간단한 예로 다음과 같은 작업을 수행 할 수 있습니다.
SEL selec;
if (a == b) {
selec = @selector(method1)
}
else
{
selec = @selector(method2)
};
[self performSelector:selec];
As per apple docs: https://developer.apple.com/library/archive/documentation/General/Conceptual/DevPedia-CocoaCore/Selector.html
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 is compiled. A selector by itself doesn’t do anything. It simply identifies a method. The only thing that makes the selector method name different from a plain string is that the compiler makes sure that selectors are unique. What makes a selector useful is that (in conjunction with the runtime) it acts like a dynamic function pointer that, for a given name, automatically points to the implementation of a method appropriate for whichever class it’s used with. Suppose you had a selector for the method run, and classes Dog, Athlete, and ComputerSimulation (each of which implemented a method run). The selector could be used with an instance of each of the classes to invoke its run method—even though the implementation might be different for each.
Example: (lldb) breakpoint --set selector viewDidLoad
This will set a breakpoint on all viewDidLoad implementations in your app. So selector is kind of a global identifier for a method.
참고URL : https://stackoverflow.com/questions/738622/selectors-in-objective-c
'Programing' 카테고리의 다른 글
Spring MVC 컨트롤러 메소드가 값을 반환하지 않으면 무엇을 반환합니까? (0) | 2020.07.15 |
---|---|
JS를 사용하지 않고 이미지를 드래그하거나 선택하지 못하도록 방지 (0) | 2020.07.15 |
시스템 ( "일시 정지"); (0) | 2020.07.14 |
Oracle에서 예약어를 어떻게 피할 수 있습니까? (0) | 2020.07.14 |
새로운 (그리고 비어있는) "루트"브랜치를 만드는 방법? (0) | 2020.07.14 |