1. 오전일정 : Objective-C
아래 링크를 기반으로 Objective-C를 강의해 주셨다.
https://www.tutorialspoint.com/objective_c
Objective-C Tutorial
Objective-C Tutorial Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. This is the main programming language used by Apple for the OS X and iOS operating systems and th
www.tutorialspoint.com
Objective-C란
- C프로그래밍 언어 위에 스몰토크 프로그래밍 언어의 기능을 추가해 객체지향 언어로 만든 범용 언어이다.
- Objective-C언어는 NeXT가 NeXTSTEP OS용으로 개발했으며, Apple이 인수해서 iOS 및 Mac OS 용으로 활용중이다.
- Objective-C는 객체지향의 캡슐화, 데이터 은닉, 상속, 다형성을 포함하여 객체 지향 프로그래밍을 완벽하게 지원한다.
#import <Foundation/Foundation.h>
int main() {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //NSAutoreleasePool=클래스이름, alloc=메소드, init=메소드 -> NSAutoreleasePool.alloc().init() 이런 형식으로 이해하면 됨
NSLog(@"hello world");
[pool drain];
return 0;
}
Objective-C Foundation Framework
- 기본으로 많이 다루는 class를 framework로 만들어서 제공해 주는 것이다.
- #import <Foundation>으로 정의해서 사용
- NSArray, NSDictionary, NSSet 등과 같은 확장 데이터 타입 목록 포함
- 파일(NSFileManager), 문자열(NSString) 등을 조작하는 풍부한 기능 세트로 구성됨
- URL 처리(NSURL), 날짜 형식 지정(NSDate), 데이터 처리(NSData), 오류 처리(NSError) 등과 같은 유틸리티를 위한 기능을 제공
Objective-C 프로그램 구조(structure)
- 전처리기 명령(Preprocessor Commands) -> 본격 코드 전에 먼저 처리할 것들
- 인터페이스 -> 이거만 봐도 호출해서 쓸 수 있게 하는 것을 목표로 함 (.h)
- 구현(Implementation) -> 인터페이스의 구현 (.m)
- 메소드
- 변수
- 서술 및 표현(Statements & Expressions)
- 주석
Objective-C 인터페이스 만들기
- 모든 객체의 기본 클래스인 NSObject를 상속(혹은 NSObject를 상속받은 class를 상속받아야 함)
#import <Foundation>
@interface SampleCalss:NSObject
- (void)sampleMethod; //메소드 선언 sampleMethod = 메소드이름
@end //인터페이스 종료
Objective-C 구현하기
- 인터페이스 SampleClass를 구현하는 방법을 보여줍니다.
@implementation SampleClass //SampleClass를 구현하는 방법을 보여줍니다
- (void)sampleMethod{
NSLog(@"hello world\n")
}
@end
int main() {
/* my first program in Objective-C */
SampleClass *sampleClass = [[SampleClass alloc]init];
[sampleClass sampleMethod];
return 0;
}
인터페이스와 구현이 모두 완료된 코드
#import <Foundation/Foundation.h>
@interface SampleClass:NSObject
- (void)sampleMethod;
@end
@implementation SampleClass
- (void)sampleMethod {
NSLog(@"Hello, World! \n");
}
@end
int main() {
/* my first program in Objective-C */
SampleClass *sampleClass = [[SampleClass alloc]init];
[sampleClass sampleMethod];
return 0;
}
인터페이스와 구현부를 따로 구현할 때, 파일 및 라이브러리 참조 경로
인터페이스와 구현부를 따로 구현한다면 총 파일이 2개로 생성함([1]인터페이스(.h) [2]구현(.m))
[1] 인터페이스 파일의 선언부(파일명 OOO.h)
#import <Foundation> -> 기본 라이브러리를 불러옴
[2] 구현 파일의 선언부(파일명 OOO.m)
#import "OOO.h" -> OOO.h 파일을 참조하도록 선언됨
예약어(keywords)
- 상수나 변수 또는 기타 식별자 이름으로 사용할 수 없다.
https://www.binpress.com/objective-c-reserved-keywords/
Learn Objective-C: Reserved Keywords
Objective-C reserves certain words, so that you can’t, or shouldn’t, use them in your own code. At best, you’ll get a compiler warning or error; at worst, you’ll get a nasty bug that you won’t discover until millions of people are already using y
www.binpress.com
데이터 타입
- 변수의 타입은 저장 공간에서 차지하는 공간과 저장된 비트 패턴이 해석되는 방식을 결정한다.
- 기본타입 : 산술 타입 | 정수 타입 및 부동 소수점 타입 두가지 타입으로 구성 -> 객체 아님
-> C언어 자체에 원래부터 있었던 타입(int, float, char ...) - 열거형 : 산술 타입 | 프로그램 전체에서 특정 불연속 정수 값만 할당할 수 있는 변수를 정의
- 타입 무효 : 형식 지정자 void는 사용할 수 있는 값이 없음을 나타냄 -> void도 타입이다. (swift의 Void는 객체)
- 파생 타입 : 포인터 타입, 배열 타입, 구조 타입, 통합 타입, 합수 타입
정수 타입
https://www.tutorialspoint.com/objective_c/objective_c_data_types.htm
Objective-C Data Types
Objective-C Data Types In the Objective-C programming language, data types refer to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pa
www.tutorialspoint.com
#import <Foundation/Foundation.h>
int main() {
char a;
unsigned char b;
signed char c;
int d;
unsigned int e;
short f;
unsigned short g;
long h;
unsigned long i;
float j;
double k;
long double l;
/* example of sizeof operator */
NSLog(@"char a = %d\n", sizeof(a) );
NSLog(@"unsigned char b = %d\n", sizeof(b) );
NSLog(@"signed char c = %d\n", sizeof(c) );
NSLog(@"int d = %d\n", sizeof(d) );
NSLog(@"insigned int e = %d\n", sizeof(e) );
NSLog(@"short f = %d\n", sizeof(f) );
NSLog(@"unsigned short g = %d\n", sizeof(g) );
NSLog(@"long h = %d\n", sizeof(h) );
NSLog(@"unsigned long i = %d\n", sizeof(i) );
NSLog(@"float j = %d\n", sizeof(j) );
NSLog(@"double k = %d\n", sizeof(k) );
NSLog(@"long double l = %d\n", sizeof(l) );
}
/*
2022-10-06 04:05:04.879 main[6453] char a = 1
2022-10-06 04:05:04.879 main[6453] unsigned char b = 1
2022-10-06 04:05:04.880 main[6453] signed char c = 1
2022-10-06 04:05:04.880 main[6453] int d = 4
2022-10-06 04:05:04.880 main[6453] insigned int e = 4
2022-10-06 04:05:04.880 main[6453] short f = 2
2022-10-06 04:05:04.880 main[6453] unsigned short g = 2
2022-10-06 04:05:04.880 main[6453] long h = 8
2022-10-06 04:05:04.880 main[6453] unsigned long i = 8
2022-10-06 04:05:04.880 main[6453] float j = 4
2022-10-06 04:05:04.880 main[6453] double k = 8
2022-10-06 04:05:04.880 main[6453] long double l = 16
*/
#import <Foundation/Foundation.h>
int main() {
NSLog(@"size of char : %d", sizeof(char));
NSLog(@"size of unsigned char : %d", sizeof(unsigned char));
NSLog(@"size of signed char : %d", sizeof(signed char));
NSLog(@"size of int : %d", sizeof(int));
NSLog(@"size of unsigned int : %d", sizeof(unsigned int));
NSLog(@"size of short : %d", sizeof(short));
NSLog(@"size of unsigned short : %d", sizeof(unsigned short));
NSLog(@"size of long : %d", sizeof(long));
NSLog(@"size of unsigned long : %d", sizeof(unsigned long));
NSLog(@"size of float : %d", sizeof(float));
NSLog(@"size of double : %d", sizeof(double));
NSLog(@"size of long double : %d", sizeof(long double));
return 0;
}
부동소수점 타입
void 타입
- void 타입은 사용 가능한 값이 없음을 지정합니다. (1. 함수가 void로 반환, 2. void로서의 함수 인수)
2. 오후일정 : Objective-C
Objective-C 변수
- 변수 이름은 문자, 숫자 및 밑줄 문자로 구성되며, 문자나 밑줄문자로 시작해야 한다. (대소문자 구분됨)
- 변수는 char, int float, double, void로 구성되어 있으며, string을 사용하고 싶은 경우 NSString을 상속받아 사용애햐 한다.
변수선언
- 여러 파일을 사용하고 파일 중 하나에 변수를 정의할 때 유용함.
- extern 키워드를 사용하여 다양한 파일에서 변수를 선언 가능하다.
- 변수를 여러번 선언할 수 있지만 파일, 함수 또는 코드블록에서는 한 번만 선언 가능하다.
#import <Foundation/Foundation.h>
// Variable declaration:
extern int a, b;
extern int c;
extern float f;
int main () {
/* variable definition: */
int a, b;
int c;
float f;
/* actual initialization */
a = 10;
b = 20;
c = a + b;
NSLog(@"value of c : %d \n", c);
f = 70.0/3.0;
NSLog(@"value of f : %f \n", f);
return 0;
}
L값 표현식, R값 표현식
- Lvalue - 메모리 위치를 참조하는 표현식, 할당의 왼쪽 또는 오른쪽으로 나타날 수 있음
- Rvalue - 메모리 일부 주소에 저장된 데이터 값, 할당된 값을 가질 수 없는 표현식
상수
- 프로그래밍이 실행 중에 변경할 수 없는 고정 값을 나타내며, 이러한 고정 값을 리터럴(type의 원형)이라고 합니다.
- 정수 상수, 부동 상수, 문자 상수 또는 문자열 리터럴과 같은 기본 데이터 유형 중 하나일 수 있다. + 열거형 상수
- 상수는 정의 후 값을 수정할 수 없다는 점을 제외하고 일반 변수처럼 취급된다.
정수 및 부동 소수점 리터럴
https://www.tutorialspoint.com/objective_c/objective_c_constants.htm
Objective-C Constants
Objective-C Constants The constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals. Constants can be of any of the basic data types like an integer constant, a floating constant, a cha
www.tutorialspoint.com
문자 상수
- 문자 리터럴은 작은 따옴표로 묶어 있으며, char 유형의 단순 변수에 저장할 수 있다.
- 문자 리터럴은 일반 문자('x'), 이스케이프 시퀀스('\t') 또는 범용문자('\u02C0') 일 수 있다.
연산자
- 연산자는 컴파일러에게 특정 수학적 또는 논리적 조작을 수행하도록 지시하는 기호다.
- Objective-C는 산술, 관계, 논리, 비트, 할당,기타 연산자를 제공한다.
sizeof와 삼항 연산자
- sizeof : 변수의 크기를 반환한다.
- & : 변수의 주소를 반환한다
- * : 변수에 대한 포인터
- ? : 삼항 연산자 조건식
반복문
1. while문
#import <Foundation/Foundation.h>
int main() {
int a = 10;
while(a < 20){
NSLog(@"value a : %d", a);
a ++;
}
return 0;
}
2. for문
#import <Foundation/Foundation.h>
int main() {
int a = 10;
for(a = 10, a < 20, a+=1){
NSLog(@"value a : %d", a)
}
return 0;
}
3. 오늘의 리뷰
오늘은 자습시간이 많았다! 자습시간에 swift 코드 문제를 풀었는데 0단계인데 왜 고민이 되는 문제들이 있는 것인가...흑흑 언젠가는 프로그래머스 마스터가 되기를 바라며!
'멋쟁이사자처럼 앱스쿨 1기' 카테고리의 다른 글
[멋쟁이사자처럼] 앱스쿨 1기 - Objective-C (17일차 22.10.11) (1) | 2022.10.11 |
---|---|
[멋쟁이사자처럼] 앱스쿨 1기 - Objective-C (16일차 22.10.07) (0) | 2022.10.07 |
[멋쟁이사자처럼] 앱스쿨 1기 - 클로저&이스케이프 클로저 (14일차 22.10.05) (0) | 2022.10.06 |
[멋쟁이사자처럼] 앱스쿨 1기 - Swift 에러 핸들링&열거형(Enum)&제너릭(Generics) (13일차 22.10.04) (0) | 2022.10.04 |
[멋쟁이사자처럼] 앱스쿨 1기 - 구조체(Struct)&Swift 컬렉션&property wrapper (12일차 22.09.29) (2) | 2022.09.30 |