Created
April 9, 2021 09:41
-
-
Save liaogang/671aa9aadeb95625edb7f9a637cb50c9 to your computer and use it in GitHub Desktop.
get or set a ivar of NSObject
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSIvarOffset.h | |
// whatshook | |
// | |
// Created by minisj.net on 2020/11/17. | |
// | |
NS_ASSUME_NONNULL_BEGIN | |
@interface Objc : NSObject | |
+(id)getVar:(id)target atOffset:(int)offset; | |
+(void)setTarget:(id)target var:(id)var atOffset:(int)offset; | |
+(uint64_t)getVarUint64:(id)target atOffset:(int)offset; | |
@end | |
NS_ASSUME_NONNULL_END | |
#import "NSIvarOffset.h" | |
#if __has_feature(objc_arc) | |
#error "please turn arc off, using -fno-objc-arc" | |
#else | |
#endif | |
@implementation Objc | |
+(id)getVar:(id)target atOffset:(int)offset | |
{ | |
if (target == nil) { | |
return nil; | |
} | |
void *p = (__bridge void*)target; | |
p = (char*)p + offset; | |
void **o = (void**)p; | |
id aa = (__bridge id)*o; | |
return aa; | |
} | |
+(uint64_t)getVarUint64:(id)target atOffset:(int)offset | |
{ | |
void *p = (__bridge void*)target; | |
p = (char*)p + offset; | |
uint64_t **o = (uint64_t**)p; | |
uint64_t aa = *o; | |
return aa; | |
} | |
+(void)setTarget:(id)target var:(id)var atOffset:(int)offset | |
{ | |
if (target == nil) { | |
return; | |
} | |
[var retain]; | |
void *p = (__bridge void*)target; | |
p = (char*)p + offset; | |
uint64_t *var2 = (uint64_t*)(__bridge void*)var; | |
((uint64_t*)p)[0] = var2; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment