mirror of
https://github.com/HackTricks-wiki/hacktricks.git
synced 2025-10-10 18:36:50 +00:00
313 lines
9.7 KiB
Markdown
313 lines
9.7 KiB
Markdown
# macOS Objective-C
|
|
|
|
{{#include ../../banners/hacktricks-training.md}}
|
|
|
|
## Objective-C
|
|
|
|
> [!CAUTION]
|
|
> Objective-C로 작성된 프로그램은 [Mach-O binaries](macos-files-folders-and-binaries/universal-binaries-and-mach-o-format.md)로 **컴파일**될 때 **클래스 선언**을 **유지**합니다. 이러한 클래스 선언에는 다음의 이름과 유형이 **포함**됩니다:
|
|
|
|
- 클래스
|
|
- 클래스 메서드
|
|
- 클래스 인스턴스 변수
|
|
|
|
이 정보를 [**class-dump**](https://github.com/nygard/class-dump)를 사용하여 얻을 수 있습니다:
|
|
```bash
|
|
class-dump Kindle.app
|
|
```
|
|
이 이름들은 이진 파일의 리버싱을 더 어렵게 만들기 위해 난독화될 수 있습니다.
|
|
|
|
## 클래스, 메서드 및 객체
|
|
|
|
### 인터페이스, 속성 및 메서드
|
|
```objectivec
|
|
// Declare the interface of the class
|
|
@interface MyVehicle : NSObject
|
|
|
|
// Declare the properties
|
|
@property NSString *vehicleType;
|
|
@property int numberOfWheels;
|
|
|
|
// Declare the methods
|
|
- (void)startEngine;
|
|
- (void)addWheels:(int)value;
|
|
|
|
@end
|
|
```
|
|
### **클래스**
|
|
```objectivec
|
|
@implementation MyVehicle : NSObject
|
|
|
|
// No need to indicate the properties, only define methods
|
|
|
|
- (void)startEngine {
|
|
NSLog(@"Engine started");
|
|
}
|
|
|
|
- (void)addWheels:(int)value {
|
|
self.numberOfWheels += value;
|
|
}
|
|
|
|
@end
|
|
```
|
|
### **객체 및 메서드 호출**
|
|
|
|
클래스의 인스턴스를 생성하기 위해 **`alloc`** 메서드가 호출되어 각 **속성**에 대한 **메모리**를 **할당**하고 해당 할당을 **제로**로 설정합니다. 그런 다음 **`init`**이 호출되어 **속성**을 **필요한 값**으로 **초기화**합니다.
|
|
```objectivec
|
|
// Something like this:
|
|
MyVehicle *newVehicle = [[MyVehicle alloc] init];
|
|
|
|
// Which is usually expressed as:
|
|
MyVehicle *newVehicle = [MyVehicle new];
|
|
|
|
// To call a method
|
|
// [myClassInstance nameOfTheMethodFirstParam:param1 secondParam:param2]
|
|
[newVehicle addWheels:4];
|
|
```
|
|
### **클래스 메서드**
|
|
|
|
클래스 메서드는 인스턴스 메서드에 사용되는 하이픈 (-) 대신 **플러스 기호** (+)로 정의됩니다. **NSString** 클래스 메서드 **`stringWithString`**와 같이:
|
|
```objectivec
|
|
+ (id)stringWithString:(NSString *)aString;
|
|
```
|
|
### Setter & Getter
|
|
|
|
속성을 **설정**하고 **가져오기** 위해, **점 표기법**을 사용하거나 **메서드를 호출**하는 것처럼 할 수 있습니다:
|
|
```objectivec
|
|
// Set
|
|
newVehicle.numberOfWheels = 2;
|
|
[newVehicle setNumberOfWheels:3];
|
|
|
|
// Get
|
|
NSLog(@"Number of wheels: %i", newVehicle.numberOfWheels);
|
|
NSLog(@"Number of wheels: %i", [newVehicle numberOfWheels]);
|
|
```
|
|
### **인스턴스 변수**
|
|
|
|
setter 및 getter 메서드 대신 인스턴스 변수를 사용할 수 있습니다. 이러한 변수는 속성과 동일한 이름을 가지지만 "\_"로 시작합니다:
|
|
```objectivec
|
|
- (void)makeLongTruck {
|
|
_numberOfWheels = +10000;
|
|
NSLog(@"Number of wheels: %i", self.numberOfLeaves);
|
|
}
|
|
```
|
|
### 프로토콜
|
|
|
|
프로토콜은 메서드 선언의 집합입니다(속성 없이). 프로토콜을 구현하는 클래스는 선언된 메서드를 구현합니다.
|
|
|
|
메서드는 **필수**와 **선택적**의 2가지 유형이 있습니다. **기본적으로** 메서드는 **필수**입니다(하지만 **`@required`** 태그로도 표시할 수 있습니다). 메서드가 선택적임을 나타내려면 **`@optional`**을 사용하십시오.
|
|
```objectivec
|
|
@protocol myNewProtocol
|
|
- (void) method1; //mandatory
|
|
@required
|
|
- (void) method2; //mandatory
|
|
@optional
|
|
- (void) method3; //optional
|
|
@end
|
|
```
|
|
### 모두 함께
|
|
```objectivec
|
|
// gcc -framework Foundation test_obj.m -o test_obj
|
|
#import <Foundation/Foundation.h>
|
|
|
|
@protocol myVehicleProtocol
|
|
- (void) startEngine; //mandatory
|
|
@required
|
|
- (void) addWheels:(int)value; //mandatory
|
|
@optional
|
|
- (void) makeLongTruck; //optional
|
|
@end
|
|
|
|
@interface MyVehicle : NSObject <myVehicleProtocol>
|
|
|
|
@property int numberOfWheels;
|
|
|
|
- (void)startEngine;
|
|
- (void)addWheels:(int)value;
|
|
- (void)makeLongTruck;
|
|
|
|
@end
|
|
|
|
@implementation MyVehicle : NSObject
|
|
|
|
- (void)startEngine {
|
|
NSLog(@"Engine started");
|
|
}
|
|
|
|
- (void)addWheels:(int)value {
|
|
self.numberOfWheels += value;
|
|
}
|
|
|
|
- (void)makeLongTruck {
|
|
_numberOfWheels = +10000;
|
|
NSLog(@"Number of wheels: %i", self.numberOfWheels);
|
|
}
|
|
|
|
@end
|
|
|
|
int main() {
|
|
MyVehicle* mySuperCar = [MyVehicle new];
|
|
[mySuperCar startEngine];
|
|
mySuperCar.numberOfWheels = 4;
|
|
NSLog(@"Number of wheels: %i", mySuperCar.numberOfWheels);
|
|
[mySuperCar setNumberOfWheels:3];
|
|
NSLog(@"Number of wheels: %i", mySuperCar.numberOfWheels);
|
|
[mySuperCar makeLongTruck];
|
|
}
|
|
```
|
|
### 기본 클래스
|
|
|
|
#### 문자열
|
|
```objectivec
|
|
// NSString
|
|
NSString *bookTitle = @"The Catcher in the Rye";
|
|
NSString *bookAuthor = [[NSString alloc] initWithCString:"J.D. Salinger" encoding:NSUTF8StringEncoding];
|
|
NSString *bookPublicationYear = [NSString stringWithCString:"1951" encoding:NSUTF8StringEncoding];
|
|
```
|
|
기본 클래스는 **불변**하므로 기존 문자열에 문자열을 추가하려면 **새 NSString을 생성해야 합니다**.
|
|
```objectivec
|
|
NSString *bookDescription = [NSString stringWithFormat:@"%@ by %@ was published in %@", bookTitle, bookAuthor, bookPublicationYear];
|
|
```
|
|
또는 **mutable** 문자열 클래스를 사용할 수도 있습니다:
|
|
```objectivec
|
|
NSMutableString *mutableString = [NSMutableString stringWithString:@"The book "];
|
|
[mutableString appendString:bookTitle];
|
|
[mutableString appendString:@" was written by "];
|
|
[mutableString appendString:bookAuthor];
|
|
[mutableString appendString:@" and published in "];
|
|
[mutableString appendString:bookPublicationYear];
|
|
```
|
|
#### 번호
|
|
```objectivec
|
|
// character literals.
|
|
NSNumber *theLetterZ = @'Z'; // equivalent to [NSNumber numberWithChar:'Z']
|
|
|
|
// integral literals.
|
|
NSNumber *fortyTwo = @42; // equivalent to [NSNumber numberWithInt:42]
|
|
NSNumber *fortyTwoUnsigned = @42U; // equivalent to [NSNumber numberWithUnsignedInt:42U]
|
|
NSNumber *fortyTwoLong = @42L; // equivalent to [NSNumber numberWithLong:42L]
|
|
NSNumber *fortyTwoLongLong = @42LL; // equivalent to [NSNumber numberWithLongLong:42LL]
|
|
|
|
// floating point literals.
|
|
NSNumber *piFloat = @3.141592654F; // equivalent to [NSNumber numberWithFloat:3.141592654F]
|
|
NSNumber *piDouble = @3.1415926535; // equivalent to [NSNumber numberWithDouble:3.1415926535]
|
|
|
|
// BOOL literals.
|
|
NSNumber *yesNumber = @YES; // equivalent to [NSNumber numberWithBool:YES]
|
|
NSNumber *noNumber = @NO; // equivalent to [NSNumber numberWithBool:NO]
|
|
```
|
|
#### 배열, 집합 및 사전
|
|
```objectivec
|
|
// Inmutable arrays
|
|
NSArray *colorsArray1 = [NSArray arrayWithObjects:@"red", @"green", @"blue", nil];
|
|
NSArray *colorsArray2 = @[@"yellow", @"cyan", @"magenta"];
|
|
NSArray *colorsArray3 = @[firstColor, secondColor, thirdColor];
|
|
|
|
// Mutable arrays
|
|
NSMutableArray *mutColorsArray = [NSMutableArray array];
|
|
[mutColorsArray addObject:@"red"];
|
|
[mutColorsArray addObject:@"green"];
|
|
[mutColorsArray addObject:@"blue"];
|
|
[mutColorsArray addObject:@"yellow"];
|
|
[mutColorsArray replaceObjectAtIndex:0 withObject:@"purple"];
|
|
|
|
// Inmutable Sets
|
|
NSSet *fruitsSet1 = [NSSet setWithObjects:@"apple", @"banana", @"orange", nil];
|
|
NSSet *fruitsSet2 = [NSSet setWithArray:@[@"apple", @"banana", @"orange"]];
|
|
|
|
// Mutable sets
|
|
NSMutableSet *mutFruitsSet = [NSMutableSet setWithObjects:@"apple", @"banana", @"orange", nil];
|
|
[mutFruitsSet addObject:@"grape"];
|
|
[mutFruitsSet removeObject:@"apple"];
|
|
|
|
|
|
// Dictionary
|
|
NSDictionary *fruitColorsDictionary = @{
|
|
@"apple" : @"red",
|
|
@"banana" : @"yellow",
|
|
@"orange" : @"orange",
|
|
@"grape" : @"purple"
|
|
};
|
|
|
|
// In dictionaryWithObjectsAndKeys you specify the value and then the key:
|
|
NSDictionary *fruitColorsDictionary2 = [NSDictionary dictionaryWithObjectsAndKeys:
|
|
@"red", @"apple",
|
|
@"yellow", @"banana",
|
|
@"orange", @"orange",
|
|
@"purple", @"grape",
|
|
nil];
|
|
|
|
// Mutable dictionary
|
|
NSMutableDictionary *mutFruitColorsDictionary = [NSMutableDictionary dictionaryWithDictionary:fruitColorsDictionary];
|
|
[mutFruitColorsDictionary setObject:@"green" forKey:@"apple"];
|
|
[mutFruitColorsDictionary removeObjectForKey:@"grape"];
|
|
```
|
|
### 블록
|
|
|
|
블록은 **객체처럼 동작하는 함수**로, 함수에 전달되거나 **배열**이나 **사전**에 **저장**될 수 있습니다. 또한, 값이 주어지면 **값을 나타낼 수** 있어 람다와 유사합니다.
|
|
```objectivec
|
|
returnType (^blockName)(argumentType1, argumentType2, ...) = ^(argumentType1 param1, argumentType2 param2, ...){
|
|
//Perform operations here
|
|
};
|
|
|
|
// For example
|
|
|
|
int (^suma)(int, int) = ^(int a, int b){
|
|
return a+b;
|
|
};
|
|
NSLog(@"3+4 = %d", suma(3,4));
|
|
```
|
|
함수에서 **매개변수로 사용될 블록 유형을 정의하는** 것도 가능합니다:
|
|
```objectivec
|
|
// Define the block type
|
|
typedef void (^callbackLogger)(void);
|
|
|
|
// Create a bloack with the block type
|
|
callbackLogger myLogger = ^{
|
|
NSLog(@"%@", @"This is my block");
|
|
};
|
|
|
|
// Use it inside a function as a param
|
|
void genericLogger(callbackLogger blockParam) {
|
|
NSLog(@"%@", @"This is my function");
|
|
blockParam();
|
|
}
|
|
genericLogger(myLogger);
|
|
|
|
// Call it inline
|
|
genericLogger(^{
|
|
NSLog(@"%@", @"This is my second block");
|
|
});
|
|
```
|
|
### 파일
|
|
```objectivec
|
|
// Manager to manage files
|
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
|
|
// Check if file exists:
|
|
if ([fileManager fileExistsAtPath:@"/path/to/file.txt" ] == YES) {
|
|
NSLog (@"File exists");
|
|
}
|
|
|
|
// copy files
|
|
if ([fileManager copyItemAtPath: @"/path/to/file1.txt" toPath: @"/path/to/file2.txt" error:nil] == YES) {
|
|
NSLog (@"Copy successful");
|
|
}
|
|
|
|
// Check if the content of 2 files match
|
|
if ([fileManager contentsEqualAtPath:@"/path/to/file1.txt" andPath:@"/path/to/file2.txt"] == YES) {
|
|
NSLog (@"File contents match");
|
|
}
|
|
|
|
// Delete file
|
|
if ([fileManager removeItemAtPath:@"/path/to/file1.txt" error:nil]) {
|
|
NSLog(@"Removed successfully");
|
|
}
|
|
```
|
|
파일을 **`NSString`** 객체 대신 **`NSURL`** 객체를 사용하여 관리하는 것도 가능합니다. 메서드 이름은 비슷하지만 **`Path`** 대신 **`URL`**을 사용합니다.
|
|
```objectivec
|
|
|
|
|
|
```
|
|
{{#include ../../banners/hacktricks-training.md}}
|