#import <Foundation/Foundation.h>
@interface ClassOne : NSObject{
NSString *_publicVar1;
NSString *_publicVar2;
}
@property(nonatomic,copy) NSString *publicProperty1;
@property(nonatomic,copy) NSString *publicProperty2;
- (void) testClassOneWithArg1:(NSString *)arg1;
@end
#import "ClassOne.h"
@interface ClassOne()
@property(nonatomic,copy) NSString *privateProperty1;
@property(nonatomic,copy) NSString *privateProperty2;
@end
@implementation ClassOne{
NSString *_privateVar1;
NSString *_privateVar2;
}
- (void)testClassOneWithArg1:(NSString *)arg1{
NSLog(@"this is CalssOne, arg1:%@",arg1);
}
- (void)testClassOneWithArg1:(NSString *)arg1 arg2:arg2{
NSLog(@"this is CalssOne, arg1:%@ arg2:%@",arg1,arg2);
}
@end
#import <Foundation/Foundation.h>
@interface ClassTwo : NSObject
- (void) testClassTwoWithArg1:(NSString *)arg1 arg2:(NSString *)arg2;
@end
#import "ClassTwo.h"
@implementation ClassTwo
- (void)testClassTwoWithArg1:(NSString *)arg1 arg2:(NSString *)arg2{
NSLog(@"this is ClassTwo arg1:%@,arg2:%@",arg1,arg2);
}
@end
ClassOne *one = [ClassOne new]; id onec1 = object_copy(one,sizeof(one));
ClassOne *one = [ClassOne new];
class_addMethod([ClassOne class], @selector(testClassOneWithArg1:arg2:arg3:), (IMP)testClassOne , "i@:@@@");
[one testClassOneWithArg1:@"arg1" arg2:@"arg2" arg3:@"arg3"];
//方法对应的C函数
int testClassOne(id self,SEL _cmd, NSString *arg1,NSString *arg2,NSString *arg3){
NSLog(@"this is a test function add to ClassOne as a methad with arg1:%@ arg2:%@ and arg3:%@",arg1,arg2,arg3);
return 10;
}
//属性类型
objc_property_attribute_t type = { "T", "@\"NSString\"" };
//访问类型
objc_property_attribute_t ownership = { "C", "" };
//对应成员变量名称
objc_property_attribute_t backingivar = { "V", "_testPropertyName" };
objc_property_attribute_t attrs[] = { type, ownership, backingivar };
class_addProperty([ClassOne class], "testPropertyName", attrs, 3);
class_addMethod([ClassOne class], @selector(testPropertyName), (IMP)testPropertyNameGetter , "@:@@");
class_addMethod([ClassOne class], @selector(setTestPropertyName:), (IMP)testPropertyNameSetter, "v:@@@");
//属性对应的Getter方法
NSString* testPropertyNameGetter(id self,SEL _cmd){
Ivar ivar = class_getInstanceVariable([ClassOne class], "_testPropertyName");
return object_getIvar(self, ivar);
}
//属性对应的Setter方法
void testPropertyNameSetter(id self,SEL _cmd,NSString *testPropertyNameValue){
Ivar ivar = class_getInstanceVariable([ClassOne class], "_testPropertyName");
object_setIvar(self, ivar, testPropertyNameValue);
}
ClassOne *one = [ClassOne new]; objc_setAssociatedObject(one, "objTag", @"value", OBJC_ASSOCIATION_COPY); NSString *value = objc_getAssociatedObject(one, "objTag"); NSLog(@"通过Associate设置:%@",value);
ClassOne *one = [ClassOne new]; const char *className = object_getClassName(one); NSLog(@"className:%@",[NSString stringWithUTF8String:className]);
UInt count;
Method *methods = class_copyMethodList([ClassOne class], &count);
for (int i = 0; i < count; i++) {
Method method = methods[i];
SEL sel = method_getName(method);
NSLog(@"方法名:%@",NSStringFromSelector(sel));
}
uint propertyCount;
objc_property_t *ps = class_copyPropertyList([ClassOne class], &propertyCount);
for (uint i = 0; i < propertyCount; i++) {
objc_property_t property = ps[i];
const char *propertyName = property_getName(property);
const char *propertyAttributes = property_getAttributes(property);
NSLog(@"propertyName:%@",[NSString stringWithUTF8String:propertyName]);
NSLog(@"propertyAttributes:%@",[NSString stringWithUTF8String:propertyAttributes]);
}
uint ivarCount;
Ivar *ivars = class_copyIvarList([ClassOne class], &ivarCount);
for (uint i = 0; i < ivarCount; i++) {
Ivar ivar = ivars[i];
const char *ivarName = ivar_getName(ivar);
NSLog(@"ivarName:%@",[NSString stringWithUTF8String:ivarName]);
}
uint ivarCount;
Ivar *ivars = class_copyIvarList([ClassOne class], &ivarCount);
for (uint i = 0; i < ivarCount; i++) {
Ivar ivar = ivars[i];
const char *ivarName = ivar_getName(ivar);
const char *type = ivar_getTypeEncoding(ivar);
NSLog(@"ivarName=%@,type=%@",[NSString stringWithUTF8String:ivarName],[NSString stringWithUTF8String:type]);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有