Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
36
Task/Break-OO-privacy/Objective-C/break-oo-privacy-1.m
Normal file
36
Task/Break-OO-privacy/Objective-C/break-oo-privacy-1.m
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface Example : NSObject {
|
||||
@private
|
||||
NSString *_name;
|
||||
}
|
||||
- (instancetype)initWithName:(NSString *)name;
|
||||
@end
|
||||
|
||||
@implementation Example
|
||||
- (NSString *)description {
|
||||
return [NSString stringWithFormat:@"Hello, I am %@", _name];
|
||||
}
|
||||
- (instancetype)initWithName:(NSString *)name {
|
||||
if ((self = [super init])) {
|
||||
_name = [name copy];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@end
|
||||
|
||||
int main (int argc, const char * argv[]) {
|
||||
@autoreleasepool{
|
||||
|
||||
Example *foo = [[Example alloc] initWithName:@"Eric"];
|
||||
|
||||
// get private field
|
||||
NSLog(@"%@", [foo valueForKey:@"name"]);
|
||||
|
||||
// set private field
|
||||
[foo setValue:@"Edith" forKey:@"name"];
|
||||
NSLog(@"%@", foo);
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
50
Task/Break-OO-privacy/Objective-C/break-oo-privacy-2.m
Normal file
50
Task/Break-OO-privacy/Objective-C/break-oo-privacy-2.m
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface Example : NSObject {
|
||||
@private
|
||||
NSString *_name;
|
||||
}
|
||||
- (instancetype)initWithName:(NSString *)name;
|
||||
@end
|
||||
|
||||
@implementation Example
|
||||
- (NSString *)description {
|
||||
return [NSString stringWithFormat:@"Hello, I am %@", _name];
|
||||
}
|
||||
- (instancetype)initWithName:(NSString *)name {
|
||||
if ((self = [super init])) {
|
||||
_name = [name copy];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@end
|
||||
|
||||
@interface Example (HackName)
|
||||
- (NSString *)getName;
|
||||
- (void)setNameTo:(NSString *)newName;
|
||||
@end
|
||||
|
||||
@implementation Example (HackName)
|
||||
- (NSString *)getName {
|
||||
return _name;
|
||||
}
|
||||
- (void)setNameTo:(NSString *)newName {
|
||||
_name = [newName copy];
|
||||
}
|
||||
@end
|
||||
|
||||
int main (int argc, const char * argv[]) {
|
||||
@autoreleasepool{
|
||||
|
||||
Example *foo = [[Example alloc] initWithName:@"Eric"];
|
||||
|
||||
// get private field
|
||||
NSLog(@"%@", [foo getName]);
|
||||
|
||||
// set private field
|
||||
[foo setNameTo:@"Edith"];
|
||||
NSLog(@"%@", foo);
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
38
Task/Break-OO-privacy/Objective-C/break-oo-privacy-3.m
Normal file
38
Task/Break-OO-privacy/Objective-C/break-oo-privacy-3.m
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
#import <objc/runtime.h>
|
||||
|
||||
@interface Example : NSObject {
|
||||
@private
|
||||
NSString *_name;
|
||||
}
|
||||
- (instancetype)initWithName:(NSString *)name;
|
||||
@end
|
||||
|
||||
@implementation Example
|
||||
- (NSString *)description {
|
||||
return [NSString stringWithFormat:@"Hello, I am %@", _name];
|
||||
}
|
||||
- (instancetype)initWithName:(NSString *)name {
|
||||
if ((self = [super init])) {
|
||||
_name = [name copy];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@end
|
||||
|
||||
int main (int argc, const char * argv[]) {
|
||||
@autoreleasepool{
|
||||
|
||||
Example *foo = [[Example alloc] initWithName:@"Eric"];
|
||||
|
||||
// get private field
|
||||
Ivar nameField = class_getInstanceVariable([foo class], "_name");
|
||||
NSLog(@"%@", object_getIvar(foo, nameField));
|
||||
|
||||
// set private field
|
||||
object_setIvar(foo, nameField, @"Edith");
|
||||
NSLog(@"%@", foo);
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue