Programing

Objective C에서 방법 옆의 더하기 및 빼기 기호는 무엇을 의미합니까?

lottogame 2020. 5. 23. 08:54
반응형

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의 결정적인 설명은 여기 '방법 및 메시징'섹션에 있습니다.

https://developer.apple.com/library/mac/referencelibrary/GettingStarted/RoadMapOSX/books/WriteObjective-CCode/WriteObjective-CCode/WriteObjective-CCode.html

간단히 :

+는 '클래스 방법'을 의미합니다

클래스의 인스턴스를 인스턴스화하지 않고 메소드를 호출 할 수 있습니다. 그래서 당신은 이것을 다음과 같이 부릅니다.

[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";

참고URL : https://stackoverflow.com/questions/2097294/what-do-the-plus-and-minus-signs-mean-in-objective-c-next-to-a-method

반응형