iPhone에서 셀프 클래스 방법을 사용하는 방법은 무엇입니까? (개념적 질문)

StackOverflow https://stackoverflow.com/questions/605414

문제

ClassName.m에 인스턴스 메소드를 작성합니다.

-(void)methodName:(paraType)parameter
{...}

그리고 그것을 사용한다고 부릅니다

[self methodName:parameter]; 
경고가 나타나지 만 코드는 여전히 성공적으로 실행됩니다.

수업 인스턴스를 만들지 않았기 때문입니까? 메소드가 여전히 정상적으로 실행되는 이유는 무엇입니까? 그리고 경고를 방지하기 위해 셀프 방법을 호출하는 올바른 방법은 무엇입니까?

도움이 되었습니까?

해결책

경고에 대한 도움을받는 첫 번째 단계는 경고를 게시하는 것입니다. :)

나는 그것이 인식되지 않은 메시지에 관한 것이라고 가정하고 있습니까? 그렇다면 컴파일러가 "MethodName"으로 호출되는 것을 볼 수 있기 때문에 객체에 유효한 지 알 수 없습니다.

나는 당신의 코드가 보이는 것 같아요.

-(void) someFunc
{
  ...
  [self methodName:parameter]; 
  ...
}

-(void)methodName:(paraType)parameter
{
...
}

당신도 할 수 있습니다;

a) 컴파일러가 호출에 사용되기 전에 보았을 때 파일의 앞부분의 '메소드 이름'기능을 파일에 배치하십시오.

b) 클래스 인터페이스에서 선언하십시오. 예를 들어

// Foo.h
@interface Foo {
...
}
-(void) methodName:(paraType)parameter;
@end

다른 팁

What is the warning that you get?

Do you have a definition of the method in your header file?

The syntax you use is the propper way of calling method on self.

The method will work because Objective-C methods are resolved at run-time. I expect the warning you get is something like "Object Foo may not respond to -methodName:" and then it tells you that it's defaulting the return type to id. That's because the compiler hasn't seen a declaration or definition of -methodName: by the time it compiles the code where you call it. To remove the warning, declare the method in either the class's interface or a category on the class.

If you are getting a warning it might be because the method signature isn't in an interface.

@interface foo ....

-(void)method;

Once the implementation is written the warning should go away since it's not the first time the compiler has seen the method. It will work without doing this, but the warning message is annoying.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top