'선언되지 않은 선택기'경고를 제거하는 방법
구현 된 프로토콜 없이 NSObject 인스턴스에서 선택기를 사용하고 싶습니다 . 예를 들어, 호출 된 NSObject 인스턴스가 지원하는 경우 오류 속성을 설정해야하는 범주 메서드가 있습니다. 이것은 코드이며 코드는 의도 한대로 작동합니다.
if ([self respondsToSelector:@selector(setError:)])
{
[self performSelector:@selector(setError:) withObject:[NSError errorWithDomain:@"SomeDomain" code:1 userInfo:nil]];
}
그러나 컴파일러는 setError : 시그니처와 관련된 메소드를 찾지 못하므로 @selector(setError:)
스 니펫 이 포함 된 각 줄에 대해 경고를 표시합니다 .
Undeclared selector 'setError:'
이 경고를 없애기 위해 프로토콜을 선언하고 싶지는 않습니다. 왜냐하면 이것을 사용하여 특별한 것을 구현하기 위해 모든 클래스를 원하지 않기 때문입니다. 관습에 따라 setError:
메소드 또는 속성 을 갖기를 원합니다 .
이것이 가능합니까? 어떻게?
건배,
EP
다른 옵션은 다음을 사용하여 경고를 비활성화하는 것입니다.
#pragma GCC diagnostic ignored "-Wundeclared-selector"
이 줄은 경고가 발생하는 .m 파일에 배치 할 수 있습니다.
최신 정보:
다음과 같이 LLVM에서도 작동합니다.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
... your code here ...
#pragma clang diagnostic pop
NSSelectorFromString을 살펴 보십시오 .
SEL selector = NSSelectorFromString(@"setError:");
if ([self respondsToSelector:selector])
@selector
키워드를 통해 컴파일 타임 대신 런타임에 선택기를 작성할 수 있으며 컴파일러는 불평 할 기회가 없습니다.
나는 이것이 이상한 이유로 선택기가 런타임에 등록되지 않았기 때문이라고 생각합니다.
다음을 통해 선택기를 등록하십시오 sel_registerName()
.
SEL setErrorSelector = sel_registerName("setError:");
if([self respondsToSelector:setErrorSelector]) {
[self performSelector:setErrorSelector withObject:[NSError errorWithDomain:@"SomeDomain" code:1 userInfo:nil]];
}
메소드를 사용하여 파일을 #include '하여 메시지를 제거했습니다. 그 파일에서 다른 것은 사용되지 않았습니다.
이 스레드에 약간 늦었지만 완료를 위해 대상 빌드 설정을 사용 하여이 경고를 전체적으로 끌 수 있습니다.
'Apple LLVM 경고-Objective-C'섹션에서 다음을 변경하십시오.
Undeclared Selector - NO
클래스가 setError : 메소드를 구현하는 경우 (최종 오류 특성의 setter를 동적으로 선언하여도) 인터페이스 파일 (.h)에 선언하거나이를 표시하지 않으려는 경우 PrivateMethods 까다로운 트릭으로 시도하십시오.
@interface Yourclass (PrivateMethods)
- (void) yourMethod1;
- (void) yourMethod2;
@end
@implementation 직전에 경고가 숨겨져 있어야합니다.;).
당신 .pch
이나 Common.h
당신이 원하는 곳에 넣을 수있는 정말 편안한 매크로 :
#define SUPPRESS_UNDECLARED_SELECTOR_LEAK_WARNING(code) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wundeclared-selector"\"") \
code; \
_Pragma("clang diagnostic pop") \
비슷한 문제에 대한 이 질문 의 편집입니다 ...
스크린 샷과 같이 Xcode에서 끌 수 있습니다.
이 경고를 피하는 또 다른 방법은 선택기 방법이 다음과 같은지 확인하는 것입니다.
-(void) myMethod :(id) sender{
}
발신자를 수락하거나 원하는 경우 발신자 객체의 유형을 지정하려면 "(id) 발신자"를 잊지 마십시오.
You can also cast the object in question to an id first to avoid the warning:
if ([object respondsToSelector:@selector(myMethod)]) {
[(id)object myMethod];
}
While the correct answer likely lies in informing Xcode through imports or registering the selector that such a selector exists, in my case I was missing a semi-colon. Make sure before you "fix" the error that perhaps, the error is correct and your code isn't. I found the error in Apple's MVCNetworking sample, for instance.
I was able to get the warning to go away by adding thenothing method (disclosure: I didn't think of this but found it by googling on scheduledtimerwithtimeinterval)
[NSTimer scheduledTimerWithTimeInterval:[[NSDate distantFuture] timeIntervalSinceNow]
target:self
selector:@selector(donothingatall:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] run];
HTTPLogVerbose(@"%@: BonjourThread: Aborted", THIS_FILE);
}
}
+ (void) donothingatall:(NSTimer *)timer
{
}
While I appreciate knowing how to hide the warning, fixing it is better and neither Sergio's nor Relkin's techniques worked for me, for unknown reasons.
참고URL : https://stackoverflow.com/questions/6224976/how-to-get-rid-of-the-undeclared-selector-warning
'Programming' 카테고리의 다른 글
Intellij IDEA는 for-each / for 키보드 단축키를 생성합니다. (0) | 2020.06.02 |
---|---|
HttpContent 사용 방법을 찾을 수 없습니다 (0) | 2020.06.02 |
file_get_contents () : 코드 1로 SSL 작업 실패, 암호화 사용 실패 (0) | 2020.06.02 |
NavigationView get / find 헤더 레이아웃 (0) | 2020.06.02 |
XSD와 WSDL의 차이점은 무엇입니까 (0) | 2020.06.01 |