Kumbuka kwamba programu zilizoandikwa kwa Objective-C zinahifadhi matangazo yao ya darasa wakatizinapokanzwa kuwa Mach-O binaries. Matangazo kama hayo ya darasa yanajumuisha jina na aina ya:
Kumbuka kwamba majina haya yanaweza kufichwa ili kufanya kurudi nyuma kwa binary kuwa ngumu zaidi.
Madarasa, Mbinu & Vitu
Kiolesura, Mali & Mbinu
// 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
Darasa
@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
Object & Call Method
Ili kuunda mfano wa darasa, njia ya alloc inaitwa ambayo inagawanya kumbukumbu kwa kila sifa na kuweka sifahizi kuwa sifuri. Kisha init inaitwa, ambayo inaanzisha sifa kwa thamani zinazohitajika.
// 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];
Njia za Darasa
Njia za darasa zinaelezewa kwa kutumia ishara ya kuongeza (+) si alama ya kupunguza (-) inayotumika na njia za mfano. Kama njia ya darasa ya NSStringstringWithString:
+ (id)stringWithString:(NSString *)aString;
Setter & Getter
Ili kueka & kupata mali, unaweza kufanya hivyo kwa alama ya nukta au kama ungekuwa ukitaja njia:
// Set
newVehicle.numberOfWheels = 2;
[newVehicle setNumberOfWheels:3];
// Get
NSLog(@"Number of wheels: %i", newVehicle.numberOfWheels);
NSLog(@"Number of wheels: %i", [newVehicle numberOfWheels]);
Mabadiliko ya Kiholela
Badala ya mbinu za setter & getter unaweza kutumia mabadiliko ya kiholela. Mabadiliko haya yana jina sawa na mali lakini yanaanza na "_":
Protocols ni seti ya matamko ya mbinu (bila mali). Darasa linalotekeleza protokali linafanya kazi za kutangazwa.
Kuna aina 2 za mbinu: lazima na hiari. Kwa kawaida mbinu ni lazima (lakini unaweza pia kuashiria hivyo kwa lebo ya @required). Kuashiria kwamba mbinu ni hiari tumia @optional.
Darasa za msingi ni zisizoweza kubadilishwa, hivyo ili kuongeza mfuatano wa herufi kwenye moja iliyopo NSString mpya inahitaji kuundwa.
NSString *bookDescription = [NSString stringWithFormat:@"%@ by %@ was published in %@", bookTitle, bookAuthor, bookPublicationYear];
Au unaweza pia kutumia darasa la mutable string:
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];
Nambari
// 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]
Blocks ni kazi zinazofanya kama vitu hivyo zinaweza kupitishwa kwa kazi au kuhifadhiwa katika mifumo au kamusi. Pia, zinaweza kuwakilisha thamani ikiwa zitatolewa thamani hivyo ni sawa na lambdas.
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));
Ni pia inawezekana kufafanua aina ya block inayotumika kama parameter katika kazi:
// 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");
});
Faili
// 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");
}
Ni pia inawezekana kusimamia faili ukitumia vitu vya NSURL badala ya vitu vya NSString. Majina ya mbinu ni sawa, lakini pamoja na URL badala ya Path.
<div data-gb-custom-block data-tag="hint" data-style='success'>
Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
<details>
<summary>Support HackTricks</summary>
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
</details>
</div>
</details>
</div>
</details>
</div>
</details>
</div>
</details>
</div>
</details>
</div>
</details>
</div>
</details>
</div>
</details>
</div>
</details>
</div>
</details>
</div>
</details>
</div>
hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
</div>
</details>
</div>
</details>
</div>