안녕하세요 진코드93입니다~!
이제 드디어 밀려있던 회고록들의 끝이 보이네요...ㅎ
오늘은 저번 시간에 이어 Objective-C에 대해서 알아보려고 하는데요 항상 강사님께서 강조하시는게 이제는 아예 처음부터 Objective-C로 코드를 작성하는 일은 없을테니 혹시나 회사에서 이미 Objective-C로 만들어진 앱을 본다면 코드를 보고 어떠한 기능인지 파악할 수 있을 정도로만 익히라고 하셨어요 ㅎㅎ
그래서 앞으로 Objective-C 내용은 예제들을 보여드리려고합니다~!
오늘도 저번과 마찬가지로 코드블럭에 C#으로 표기되어있으나 Objective-C로 봐주시면 되겠습니다 ㅎㅎ
메서드 예제 1
#import <Foundation/Foundation.h>
@interface MyNumber: NSObject
- (int)getMaxiumNumberWithFirstNumber:(int)num1 secondNumber:(int)num2;
@end
@implementation MyNumber
- (int)getMaxiumNumberWithFirstNumber:(int)num1 secondNumber:(int)num2 {
int result;
if (num1 > num2) {
result = num1;
} else {
result = num2;
}
return result;
}
@end
int main (int argc, const char * argv[])
{
MyNumber *num = [[MyNumber alloc] init];
int maxNum = [num getMaxiumNumberWithFirstNumber:13 secondNumber:10];
printf("%d", maxNum);
return 0;
}
메서드 예제 2 (삼항식으로 단축)
#import <Foundation/Foundation.h>
@interface MyNumber: NSObject
- (int)getMaxiumNumberWithFirstNumber:(int)num1 secondNumber:(int)num2;
@end
@implementation MyNumber
- (int)getMaxiumNumberWithFirstNumber:(int)num1 secondNumber:(int)num2 {
return num1 > num2 ? num1: num2;
}
@end
int main (int argc, const char * argv[])
{
MyNumber *num = [[MyNumber alloc] init];
int maxNum = [num getMaxiumNumberWithFirstNumber:13 secondNumber:20];
printf("%d", maxNum);
return 0;
}
메서드 호출 예제
#import <Foundation/Foundation.h>
@interface MyNumber: NSObject
- (int)getMaxiumNumberWithFirstNumber:(int)num1 secondNumber:(int)num2;
- (NSString *)getHello;
@end
@implementation MyNumber
- (int)getMaxiumNumberWithFirstNumber:(int)num1 secondNumber:(int)num2 {
int result = num1 > num2 ? num1: num2;
return result; // call by value
}
- (NSString *)getHello {
NSString *hello = @"Hello World";
return hello; // call by reference
}
@end
int main (int argc, const char * argv[])
{
MyNumber *num = [[MyNumber alloc] init];
int maxNum = [num getMaxiumNumberWithFirstNumber:13 secondNumber:20];
NSLog(@"%d", maxNum);
NSString *hello = [num getHello];
NSLog(@"%@", hello);
return 0;
}
블럭 예제
#import <Foundation/Foundation.h>
// C언어 스타일의 함수
double multiplyTwoValuesOrigin(double fistValue, double seconValue) {
return fistValue * seconValue;
}
// 위 코드와 동일하게 작동하는 블록 표현식
// Block의 이름은 (^multiplyTwoValues)(double, double)
// Block의 실제 작동내용은 ^{ ... }으로 구현
double (^multiplyTwoValues)(double, double) = ^(double firstValue, double secondValue) {
return firstValue * secondValue;
};
int main (int argc, const char * argv[])
{
double myNum = multiplyTwoValues(2, 4);
NSLog(@"%f", myNum);
return 0;
}
NSNumber 예제
#import <Foundation/Foundation.h>
// Foundation이 제공하는 NSObject를 상속받은 SampleClass는 이러한 내용을 구현합니다.
@interface SampleClass : NSObject
- (NSNumber *)multiplyA:(NSNumber *)a withB:(NSNumber *)b;
@end
// SampleClass의 구체적인 내용은 다음과 같습니다.
@implementation SampleClass
- (NSNumber *)multiplyA:(NSNumber *)a withB:(NSNumber *)b {
// NSNumber 타입으로 받은 a와 b로부터 float 값들을 꺼내서 곱셈한 후
// NSNumber로 다시 만들어 반환한다.
float number1 = [a floatValue];
float number2 = [b floatValue];
float product = number1 * number2;
// 결과를 NSNumber로 다시 만들어 반환한다.
NSNumber *result = [NSNumber numberWithFloat:product];
return result;
}
@end
int main (int argc, const char * argv[])
{
@autoreleasepool {
// Swift였다면 ... var sampleClass: SampleClass = SampleClass()
SampleClass *sampleClass = [[SampleClass alloc] init];
NSNumber *a = [NSNumber numberWithFloat:10.5];
NSNumber *b = [NSNumber numberWithFloat:10.0];
NSNumber *result = [sampleClass multiplyA:a withB:b];
// 결과의 NSNumber로부터 NSString을 만들어 출력한다.
NSString *resultString = [result stringValue];
NSLog(@"The product is %@", resultString);
}
return 0;
}
배열 예제
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
@autoreleasepool {
int myArray[10]; // 10개의 int값이 들어갈 배열 선언
int i, j; // for 반복문에 활용
for (i = 0; i < 10; i++) { // i는 0..<10 구간으로 증가하며 반복
myArray[i] = i + 100;
}
for (j = 0; j < 10; j++) {
NSLog(@"Element[%d] = %d\n", j, myArray[j]);
}
}
return 0;
}
포인터 예제 1
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
int age = 13;
double height = 145.32;
// 변수에 담긴 값을 출력
NSLog(@"value of age : %d", age);
NSLog(@"value of hegith : %f", height);
// 변수가 위치한 메모리 안에서의 주소
NSLog(@"address of age : %x", &age);
NSLog(@"address of height : %x", &height);
// 포인터 정보 = 해당 데이터의 메모리 주소 + 메모리를 차지하는 크기
// 토지대장 = 주소지(경상북도 울릉군...) + 몇 평수 정보(임야 13평)
return 0;
}
포인터 예제 2
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
// 포인터 정보 = 해당 데이터의 메모리 주소 + 메모리를 차지하는 크기
// 토지대장 = 주소지(경상북도 울릉군...) + 몇 평수 정보(임야 13평)
int index = 17;
printf("i stores its value at %p\n", &index);
printf("this function starts at %p\n", &main);
return 0;
}
포인터 예제 3
#import <Foundation/Foundation.h>
@interface SampleClass : NSObject
- (void)sayHello;
@end
@implementation SampleClass
- (void)sayHello {
NSLog(@"Hello World");
}
@end
int main (int argc, const char * argv[]) {
// 포인터 정보 = 해당 데이터의 메모리 주소 + 메모리를 차지하는 크기
// 토지대장 = 주소지(경상북도 울릉군...) + 몇 평수 정보(임야 13평)
int age = 17;
// 변수 age의 주소를 확인해서, int 타입이 담길 거라고 생각하는 포인터 변수 만들기
int *infoOfAge = &age;
NSLog(@"age는 %p 주소에 위치했습니다.", infoOfAge);
// age는 0x16fdff3ac 주소에 위치했습니다.
NSLog(@"int 타입의 메모리 크기는 %zu 바이트 입니다.", sizeof(int));
// int 타입의 메모리 크기는 4 바이트 입니다.
NSLog(@"변수 age의 메모리 크기는 %zu 바이트 입니다.", sizeof(age));
// 변수 age의 메모리 크기는 4 바이트 입니다.
NSLog(@"처음 %p 주소에 가보니 %d 값이 들어있습니다.", infoOfAge, *infoOfAge);
// 처음 0x16fdff3ac 주소에 가보니 17 값이 들어있습니다.
*infoOfAge = 32;
NSLog(@"다시 %p 주소에 가보니 %d 값이 들어있습니다.", infoOfAge, *infoOfAge);
// 다시 0x16fdff3ac 주소에 가보니 32 값이 들어있습니다.
NSLog(@"int를 담은 데이터의 포인터정보 크기는 %zu 바이트 입니다.", sizeof(int *));
// int를 담은 데이터의 포인터정보 크기는 8 바이트 입니다.
NSLog(@"%p를 가르키는 정보의 메모리 크기 %zu 바이트 입니다.", infoOfAge, sizeof(infoOfAge));
// 0x16fdff3ac를 가르키는 정보의 메모리 크기 8 바이트 입니다.
// SampleClass의 인스턴스를 만들어서
// 그 메모리의 위치 정보를 SampleClass클래스에 맞춘 포인터 변수 sampleClass에 할당한다.
SampleClass *sampleClass = [[SampleClass alloc] init];
// sampleClass 포인터 정보가 가르키는 인스턴스 데이터를 찾아가서
// 데이터에 명시된 sayHello 메소드를 실행하라
[sampleClass sayHello];
return 0;
}
'멋쟁이 사자처럼 앱스쿨 1기 > 회고록' 카테고리의 다른 글
멋사 앱 스쿨 1기 18일차 회고록/TIL (22.10.11) - Objective-C (1) | 2022.10.18 |
---|---|
멋사 앱 스쿨 1기 16일차 회고록/TIL (22.10.06) - Objective-C (0) | 2022.10.17 |
멋사 앱 스쿨 1기 15일차 회고록/TIL (22.10.05) - 클로저, Objective-C 맛보기 (0) | 2022.10.14 |
멋사 앱 스쿨 1기 14일차 회고록/TIL (22.10.04) - 에러 핸들링, Swift 심화 (1) | 2022.10.13 |
멋사 앱 스쿨 1기 13일차 회고록/TIL (22.09.30) - DevOps, Git (0) | 2022.10.12 |