From C++ to Objective-C – Syntax overview
원문 출처: http://ktd.club.fr/programmation/objective-c.php
간혹 오역이 있을 수 있으니 주의하세요!
3 Syntax overview 문법 개요
3.1 Keywords 키워드
- Objective-C is a superset of the C language. Like with C++, a well-written C program should be compile-able as Objective-C, as long as it is not using some of the bad practices allowed by C.
- Objective- C는 C 언어의 초집합이다. C++로 잘 작성된 C 프로그램은 C에서만 허용되는 나쁜 시도만 하지 않는다면 Objective-C로도 컴파일이 가능하다.
- Objective-C 는 몇가지 추가된 개념과 그에 관련된 키워드를 가지고 있다. (C와의)충돌을 피하기 위해, 이 키워드들는 문자앞에 @(at)를 붙여 시작한다. 여기 (짧지만) 모든 리스트가 있다:
@class,
@interface,
@implementation,
@public,
@private,
@protected,
@try,
@catch,
@throw,
@finally,
@end,
@protocol,
@selector,
@synchronized,
@defs,
@encode.
nil, Nil, 타입 id, SEL, BOOL, 불린(boolean)값인 YES, NO도 인용할 수 있다. 마지막으로 6가지의 키워드가 특정 컨텍스트에 사용가능하나, 외부에 공개되지 않은 예약어이다: in, out, inout, bycopy, byref, oneway. 프로토콜을 정의할 때 볼 수 있다. - 언어 키워드와 NSObject 루트 클래스(모든 클래스의 부모-최상위)에서 상속된 몇몇 메소드가 혼동될 수 있다. 예를 들어, alloc, retain, release, autorelease 란 메모리 관리와 유사하게 보이는 “키워드”는 실은 NSObject의 메소드들이다(키워드가 아닌 virtual 메소드). super와 self 단어는 키워드로도 이해될 수 있지만 self는 실은 각 매소드에 숨겨진 파라미터이며, super는 스스로를 개별적으로 사용하기 위해 컴파일러에 요청하는 명령어이다. 그러나, 가짜 키워드와 진짜 키워드간의 혼동은 정상적인 사용에서는 문제가 되지 않을 것이다(그냥 쓰면 된다는 이야기 같다..).
- Objective-C의 모든 키워드가 이 도큐먼트에 설명되는 것은 아니다. @synchronized, @defs , @encode 는 C++ 언어와 비교될 만한 것이 없기에 제외되었다. 그에 상응하는 것조차 없다. 그들은 표준 C++에서 Objective-C로 프로그램을 변환하기에 필요치 않다.
Objective-C has only added some concepts and their associated keywords. To avoid conflicts, these keywords begin with the @ (at) character.
Here is the (short) exhaustive list:
@class,
@interface,
@implementation,
@public,
@private,
@protected,
@try,
@catch,
@throw,
@finally,
@end,
@protocol,
@selector,
@synchronized,
@defs,
@encode.
Let us also quote the values nil and Nil, the types id, SEL and BOOL, the boolean values being YES and NO. At last, six more keywords are available in a particular context, and are not reserved outside: in, out, inout, bycopy, byref, oneway. They can be met when defining protocols (cf. section 6.4.3 on page 22).
There is an easy confusion between the language keywords and some methods inherited from the root class NSObject (the mother of every class, cf. section 5.1 on page 9). For instance, the similar- looking “keywords” for memory managament, named alloc, retain, release and autorelease, are in fact methods of NSObject. The words super and self (cf. section 5.3.1 on page 11), could also be understood as keywords, but self is in fact a hidden parameter to each method, and super an instruction asking the compiler to use self differently. However, the confusion between these false keywords and the true ones will not likely prove problematic in normal use.
Not all the keywords of Objective-C are presented in this document. @synchronized, @defs and @encode have been put aside because they are not relevant for comparing with the C++ language: they have no equivalent there. They are not required to translate a program from standard C++ to Objective-C.
3.2 Comments 주석
- The comments /* . . . */ and // are allowed.
- /* . . . */ 과 // 주석이 허용된다.
3.3 Mixing up code and declarations 코드와 선언 섞기
- Like in C++, it is possible to insert the declaration of a variable in the middle of a block of instructions.
- C++에서 처럼, 명령어 블럭 중간에 변수 선언을 삽입하는 가능하다.
3.4 New types and values
3.4.1 BOOL, YES, NO
- In C++, the boolean type is bool. In Objective-C, it is BOOL, which can be set to YES or NO.
- C++에서 불린 타입은 bool이다. Objective- C에서는 BOOL이며, YES나 NO로 설정될 수 있다.(C에서는 true,false 였다)
3.4.2 nil, Nil and id
- These three keywords are explained later in the document, but briefly:
- 이 도큐먼트의 후반부에 설명될 3가지 키워드가 있다. 간단하게
• 모든 객체는 id타입을 가지고 있다. 이는 weak-typing의 도구이다
.• nil은 객체를 가리키는 포인터의 NULL과 동일하다. nil과 NULL은 서로 교환 가능하지 않다.
• Nil은 클래스 포인터를 위한 nil 과 동일하다. Objective-C에서 클래스는 객체이다(메타 클래스의 인스턴스이다).
• Every object is of type id. This is a tool for weak-typing;
• nil is the equivalent of NULL for a pointer to an object. nil and NULL should not be interchangeable.
• Nil is the equivalent of nil for a class pointer. In Objective-C, a class is an object (it is the instance of a meta-class).
3.4.3 SEL
- The SEL type can store selectors values (method identifiers). These values can be computed by a call to @selector. It is the Objective-C implementation for pointers on member functions. See section 5.3.4 on page 13 for more details.
- SEL 타입은 셀렉터 값(메소드 식별자)를 저장할 수 있다. 이들 값은 @selector 를 호출함으로써 계산될 수 있다. 멤버 함수에 대한 포인터를 Objective-C로 구현하는 것이다(함수포인터와 비슷하다).
3.5 Class names: why NS? 클래스 이름: 왜 NS지?
- In this document, almost all class names begin with NS, like NSObject or NSString. The reason is simple: they are Cocoa classes, and most Cocoa classes begin with NS since they were initiated under NeXTStep.
- 이 도큐먼트에서, NSObject나 NSString같이 대부분 클래스 이름은 NS로 시작한다. 이유는 간단한다. 이것들이 코코아 클래스이며, NeXTStep 에서 초기 생성되었기에 대부분 코코아 클래스가 NS로 시작한다.클래스의 기원을 식별하기 위해 접두사를 사용하는 것은 흔한 예이다.
It is a common practice to use a prefix to identify the origin of a class.
3.6 Differencing functions and methods 함수와 메쏘드의 차이
- Objective-C is not a language with “function calls using square brackets”. This would be a legitimate thought when observing code like that :
- Objective-C는 “대괄호를 사용한 함수 호출”을 지닌 언어가 아니다. 다음과 같은 코드는 적법하다.
[object doSomething];
instead of
object.doSomething(); - 사실 Objective-C는 C의 초집합이라서 함수는 C의 선언, 구현, 호출에 동일 문법과 의미론적으로 일치한다. 이와 반대로 C에 존재치 않는 메소드는 대괄호를 포함하는 특별한 문법이 있다. 더욱이, 문법에만 차이가 있는 것이 아니라 의미에도 있다. 다음 페이지 섹션 5.2에 자세히 나온다. 이는 메소드 호출이 아니며 메세지 전송을 의미한다. 이는 단순한 학문적인 차이가 아니다. Objective-C 메카니즘의 함축을 지니고 있다. 특히 중첩된 호출에 대한 문법은 읽을 만 하다.
But in fact, Objective-C is a superset of C, so that functions match the same syntax and semantics as C for declaration, implementation and call. On the contrary, methods, which do not exist in C, have a special syntax, which includes square brackets. Moreover, the difference is not only in the syntax, but also the meaning. This is detailed further in Section 5.2 on the next page: this is not a method call, this is sending a message. This is not just a simple academic distinction; it has implications on the mechanism of Objective-C. The syntax is also more readable, especially with nested calls (cf. section 5.3.1 on page 11).

This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 2.0 Korea License.
Related posts:
- From C++ to Objective-C : 4.Organization of source code
- From C++ to Objective-C : Classes and objects_Class declaration
- From C++ to Objective-C : A short history of Objective-C
- From C++ to Objective-C – Intro


06. 7, 2010 







Author



















No comments yet... Be the first to leave a reply!