# 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); } ``` ### 协议 协议是一组方法声明(没有属性)。实现协议的类实现声明的方法。 方法有两种类型:**必需**和**可选**。默认情况下,方法是**必需**的(但您也可以使用**`@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 @protocol myVehicleProtocol - (void) startEngine; //mandatory @required - (void) addWheels:(int)value; //mandatory @optional - (void) makeLongTruck; //optional @end @interface MyVehicle : NSObject @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]; ``` 或者你也可以使用一个**可变**字符串类: ```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"]; ``` ### Blocks Blocks 是 **作为对象行为的函数**,因此可以传递给函数或 **存储** 在 **数组** 或 **字典** 中。此外,如果给定值,它们可以 **表示一个值**,因此类似于 lambdas。 ```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"); } ``` 也可以使用 **`NSURL` 对象而不是 `NSString` 对象** 来管理文件。方法名称类似,但 **使用 `URL` 而不是 `Path`**。 ```objectivec ``` {{#include ../../banners/hacktricks-training.md}}