Objective C에서 방법 옆의 더하기 및 빼기 기호는 무엇을 의미합니까?
나는 객관적인 c와 xcode에서 매우 새롭습니다. 메소드 정의 옆 의 +
및 -
기호 가 무엇을 의미하는지 알고 싶습니다 .
- (void)loadPluginsAtPath:(NSString*)pluginPath errors:(NSArray **)errors;
+
클래스 메서드 -
를위한 것이고 인스턴스 메서드를위한 것입니다.
예 :
// Not actually Apple's code.
@interface NSArray : NSObject {
}
+ (NSArray *)array;
- (id)objectAtIndex:(NSUInteger)index;
@end
// somewhere else:
id myArray = [NSArray array]; // see how the message is sent to NSArray?
id obj = [myArray objectAtIndex:4]; // here the message is sent to myArray
// Btw, in production code one uses "NSArray *myArray" instead of only "id".
클래스와 인스턴스 메소드의 차이점을 다루는 또 다른 질문 이 있습니다 .
클래스 메소드의 경우 (+), 인스턴스 메소드의 경우 (-),
(+) 수업 방법 :-
정적로 선언 된 메소드입니다. 클래스의 인스턴스를 만들지 않고 메서드를 호출 할 수 있습니다. 클래스 메서드는 인스턴스 멤버를 알지 못하기 때문에 클래스 메서드는 클래스 멤버에서만 작동 할 수 있으며 인스턴스 멤버에서는 작동하지 않습니다. 클래스의 인스턴스 메소드는 해당 클래스의 인스턴스에서 호출되지 않는 한 클래스 메소드 내에서 호출 할 수 없습니다.
(-) 인스턴스 메소드 :-
반면에 클래스를 호출하려면 클래스의 인스턴스가 있어야하므로 새 키워드를 사용하여 클래스의 인스턴스를 작성해야합니다. 인스턴스 메소드는 특정 클래스 인스턴스에서 작동합니다. 인스턴스 메소드는 정적으로 선언되지 않았습니다.
만드는 방법?
@interface CustomClass : NSObject
+ (void)classMethod;
- (void)instanceMethod;
@end
사용하는 방법?
[CustomClass classMethod];
CustomClass *classObject = [[CustomClass alloc] init];
[classObject instanceMethod];
+ 메소드는 클래스 메소드입니다. 즉, 인스턴스 특성에 액세스 할 수없는 메소드입니다. 인스턴스 변수에 액세스 할 필요가없는 클래스의 alloc 또는 helper 메소드와 같은 메소드에 사용됩니다.
-메소드는 인스턴스 메소드-오브젝트의 단일 인스턴스와 관련됩니다. 일반적으로 클래스의 대부분의 메소드에 사용됩니다.
자세한 내용은 언어 사양 을 참조하십시오 .
Apple의 결정적인 설명은 여기 '방법 및 메시징'섹션에 있습니다.
간단히 :
+는 '클래스 방법'을 의미합니다
클래스의 인스턴스를 인스턴스화하지 않고 메소드를 호출 할 수 있습니다. 그래서 당신은 이것을 다음과 같이 부릅니다.
[className classMethod];
- '인스턴스 방법'을 의미
You need to instantiate an object first, then you can call the method on the object). You can manually instantiate an object like this:
SomeClass* myInstance = [[SomeClass alloc] init];
(this essentially allocates memory space for the object then initalises the object in that space - an oversimplification but a good way to think about it. You can alloc and init the object seperately but never do this - it can lead to nasty issues related to pointers and memory management)
Then call the instance method:
[myInstance instanceMethod]
An alternative way to get an instance of an object in Objective C is like this:
NSNumber *myNumber = [NSNumber numberWithInt:123];
which is calling the 'numberWithInt' class method of the NSNumber class, which is a 'factory' method (i.e. a method that provides you with a 'ready made instance' of an object).
Objective C also allows the creation of certain object instances directly using special syntax, like in the case of a string like this:
NSString *myStringInstance = @"abc";
'Programing' 카테고리의 다른 글
모든 재귀를 반복으로 변환 할 수 있습니까? (0) | 2020.05.23 |
---|---|
플롯 배경색을 변경하는 방법은 무엇입니까? (0) | 2020.05.23 |
시퀀스 다이어그램에 "if"조건을 표시하는 방법은 무엇입니까? (0) | 2020.05.23 |
객체 또는 메소드의 Java 동기화 메소드 잠금? (0) | 2020.05.23 |
Integer.toString (int i) 및 String.valueOf (int i) (0) | 2020.05.23 |