objective-c 기초

yb·2021년 4월 19일

iOS

목록 보기
1/2

목표

C언어의 경험이 전무한 Java 개발자가
objective-c 프로그램을 읽고 이해 및 분석을 할 수 있도록..

그게뭔데?

  • 범용 프로그래밍 언어
    -iOS 및 Mac OS 운영체제와 해당 응용프로그램을 개발하는데 사용한다.
  • C 언어 확장
  • 객체지향 프로그래밍 언어
    • 캡슐화
    • 정보 은닉
    • 상속
    • 다형성

이렇게 쓴다!

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

   NSLog (@"hello world");
   [pool drain];
   return 0;
}
  • Foundation Framework
    • NSArray, NSDictionary, NSSet와 같은 컬렉션 제공
    • 파일, 문자열 등을 조작 관련 기능 제공
    • URL 처리, 날짜, 데이터 처리, 에러 처리 등과 같은 유틸리티 기능 제공

xcode로 Hello, World!

obejctive-c는 The GCC Compiler를 통해서 컴파일하여야 한다.
window, linux에서 환경구성(https://www.tutorialspoint.com/objective_c/objective_c_environment_setup.htm)

Mac os에서 Xcode를 통한 helloworld 프로젝트 생성

  1. macOs - Command Line Tool을 선택
  2. 프로젝트 설정
  3. ⌘command + R 실행

objective-c 구성
기본적으로 아래와 같은 부분으로 구성된다.

  • Preprocessor Commands
  • Interface
  • Implementation
  • Method
  • Variables
  • Statements & Expressions
  • Comments
#import <Foundation/Foundation.h> // Line 1

@interface SampleClass:NSObject // Line 2
- (void)sampleMethod; // Line 3
@end // Line 4

@implementation SampleClass // Line 5

- (void)sampleMethod { // Line 6
   NSLog(@"Hello, World! \n"); // Line 7
}

@end

int main() {  // Line 8
   /* my first program in Objective-C */ // Line 9
   SampleClass *sampleClass = [[SampleClass alloc]init]; 
   [sampleClass sampleMethod]; // Line 10
   return 0;
}
  • Line 1: 전처리기 명령으로 컴파일 전 Foundation.h를 포함하도록
  • Line 2: @interface를 통해 interface를 생성
  • Line 3: method 정의
  • Line 4:@interface가 끝남
  • Line 5: @implementation을 통해 interface를 구성
  • Line 6: method 구현
  • Line 7: NSLog를 통해 콘솔을 출력
  • Line 8: 프로그램의 시작점
  • Line 9: 주석 처리
  • Line 10: method 실행

Keyword 제공

정수 타입

종류byte 단위길이
char1-128 ~ 127 or 0 ~255
unsigned char10 ~ 255
signed char1-128 ~ 127
int2 or 4-32,768 ~ 32,767 or -2,147,483,648 ~ 2,147,483,647
unsigned int2 or 40 ~ 65,535 ~ 0 ~ 4,294,967,295
short2-32,768 ~ 32,767
unsigned short20 ~ 65,535
long4-2,147,483,648 ~ 2,147,483,647
unsigned long40 ~ 4,294,967,295

실수 타입

종류byte 단위길이
float41.2E-38 ~ 3.4E+38
double82.3E-308 ~ 1.7E+308
long double103.4E-4932 ~ 1.1E+4932

void 타입

  • 값을 반환하지 않는 함수는 반환 유형이 void이다. e) void exit(int status);
  • 매개 변수가 없는 함수는 void로 받아 들일 수 있다. e) int rand(void);

변수 선언

int i, j, k;
char c, ch;
float f, s;
double d;

extern int d = 3, f = 5; // extern은 전역변수를 애기한다
int d = 3, f = 5;
byte z = 22;
char x = 'x';

상수 선언

#define

# import <Foundation/Foundation.h>

#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'

int main() {
  int area;
  area = LENGTH * WIDTH;
  NSLog(@"value of area : %d", area);
  NSLog(@"%c", NEWLINE);
  
  return 0;
}

const

#import <Foundation/Foundation.h>

int main() {
   const int  LENGTH = 10;
   const int  WIDTH  = 5;
   const char NEWLINE = '\n';
   int area;  
   
   area = LENGTH * WIDTH;
   NSLog(@"value of area : %d", area);
   NSLog(@"%c", NEWLINE);

   return 0;
}

둘다 동일한 결과

Method 정의

정의시

- (리턴타입) 메소드명:( 아규먼츠타입1 )아규먼츠명1 
추가아규먼츠2:( 아규먼츠타입2 )아규먼츠명2 ... 
{
   함수 내용
}

선언 시

- (리턴타입) 함수명:( 아규먼츠타입1 )아규먼츠명1
추가아규먼츠2: ( 아규먼츠타입2 )아규먼츠명2... ;

++ C를 사용해본적이 없어서 익숙하지않다..ㅠㅠ
기본적인 연산자는 JAVA와 동일하여 제외하였음
뒤에 내용을 추가할 예정이다.

Functions

Blocks

Numbers

Strings

Pointers

Structures

Preprocessors

Typedef

Classes & Objects

inheritance

emd

참조
https://www.tutorialspoint.com/objective_c

0개의 댓글