ios类和对象操作函数

ios类和对象操作函数

MyClass.h

@interface MyClass : NSObject<NSCopying, NSCoding>
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, copy) NSString *string;

- (void)method1;
- (void)method2;
+ (void)classMethod1;

@end

MyClass.m

@interface MyClass() {
    NSInteger _instance1;
    NSString *_instance2;
}
@property (nonatomic, assign) NSUInteger integer;

- (void)method3WithArg1:(NSInteger)arg1 arg2:(NSString *)arg2;

@end
@implementation MyClass

+ (void)classMethod1 {
    
}

- (void)method1 {
    NSLog(@"call method method1");
}

- (void)method2 {
    
}

- (void)method3WithArg1:(NSInteger)arg1 arg2:(NSString *)arg2 {
    NSLog(@"arg1: %ld, arg2: %@", arg1, arg2);
}

@end

operate:

- (void)viewDidLoad {
    [super viewDidLoad];
    // ios类和对象操作函数
    MyClass *myClass = [[MyClass alloc] init];
    unsigned int outCount = 0;
    Class cls = myClass.class;
    
    NSLog(@"class name: %s", class_getName(cls)); // 类名
    NSLog(@"super class name: %s", class_getName(class_getSuperclass(cls))); // 父类
    NSLog(@"MyClass is %@ a meta-class", (class_isMetaClass(cls) ? @"yes" : @"not")); // 是否元类
    
    Class meta_class = objc_getMetaClass(class_getName(cls));
    NSLog(@"%s's meta-class is %s", class_getName(cls), class_getName(meta_class));
    NSLog(@"instance size: %zu", class_getInstanceSize(cls)); // 变量实例大小
    
    // 获取成员变量
    Ivar *ivars = class_copyIvarList(cls, &outCount);
    for (int i=0;i<outCount;i++) {
        Ivar ivar = ivars[i];
        NSLog(@"instance variable's name: %s at index: %d", ivar_getName(ivar), i);
    }
    free(ivars);
    
    Ivar string = class_getInstanceVariable(cls, "_string");
    if (string != NULL) {
        NSLog(@"instance variable %s", ivar_getName(string));
    }
    
    // 属性操作
    objc_property_t *properties = class_copyPropertyList(cls, &outCount);
    for (int i=0;i<outCount;i++) {
        objc_property_t property = properties[i];
        NSLog(@"property's name: %s", property_getName(property));
    }
    free(properties);
    
    objc_property_t array = class_getProperty(cls, "array");
    if (array != NULL) {
        NSLog(@"property %s", property_getName(array));
    }
    
    // 方法操作
    Method *methods = class_copyMethodList(cls, &outCount);
    for (int i=0;i<outCount;i++) {
        Method method = methods[i];
        NSLog(@"method's signature: %s", method_getName(method));
    }
    free(methods);
    
    Method method1 = class_getInstanceMethod(cls, @selector(method1));
    if (method1 != NULL) {
        NSLog(@"method %s", method_getName(method1));
    }
    
    Method classMethod = class_getClassMethod(cls, @selector(classMethod1));
    if (classMethod != NULL) {
        NSLog(@"class method: %s", method_getName(classMethod));
    }
    
    NSLog(@"MyClass is %@ response to selector: method3WithArg1:arg2:", class_respondsToSelector(cls, @selector(method3WithArg1:arg2:)) ? @"yes" : @"not");
    
    IMP imp = class_getMethodImplementation(cls, @selector(method1));
    imp();
    
    // 协议
    Protocol * __unsafe_unretained *protocols = class_copyProtocolList(cls, &outCount);
    Protocol *protocol;
    for (int i=0;i<outCount;i++) {
        protocol = protocols[i];
        NSLog(@"protocol name: %s", protocol_getName(protocol));
    }
    NSLog(@"MyClass is %@ response to protocol %s", class_conformsToProtocol(cls, protocol) ? @"yes" : @"not", protocol_getName(protocol));
}

output:

2022-02-16 23:56:40.772652+0800 Test[56833:11021466] class name: MyClass
2022-02-16 23:56:40.772701+0800 Test[56833:11021466] super class name: NSObject
2022-02-16 23:56:40.772724+0800 Test[56833:11021466] MyClass is not a meta-class
2022-02-16 23:56:40.772747+0800 Test[56833:11021466] MyClass's meta-class is MyClass
2022-02-16 23:56:40.772766+0800 Test[56833:11021466] instance size: 48
2022-02-16 23:56:40.772786+0800 Test[56833:11021466] instance variable's name: _instance1 at index: 0
2022-02-16 23:56:40.772804+0800 Test[56833:11021466] instance variable's name: _instance2 at index: 1
2022-02-16 23:56:40.772821+0800 Test[56833:11021466] instance variable's name: _array at index: 2
2022-02-16 23:56:40.772837+0800 Test[56833:11021466] instance variable's name: _string at index: 3
2022-02-16 23:56:40.772853+0800 Test[56833:11021466] instance variable's name: _integer at index: 4
2022-02-16 23:56:40.772870+0800 Test[56833:11021466] instance variable _string
2022-02-16 23:56:40.772886+0800 Test[56833:11021466] property's name: integer
2022-02-16 23:56:40.772904+0800 Test[56833:11021466] property's name: array
2022-02-16 23:56:40.772920+0800 Test[56833:11021466] property's name: string
2022-02-16 23:56:40.772936+0800 Test[56833:11021466] property array
2022-02-16 23:56:40.772952+0800 Test[56833:11021466] method's signature: method1
2022-02-16 23:56:40.772976+0800 Test[56833:11021466] method's signature: method2
2022-02-16 23:56:40.773216+0800 Test[56833:11021466] method's signature: method3WithArg1:arg2:
2022-02-16 23:56:40.773334+0800 Test[56833:11021466] method's signature: array
2022-02-16 23:56:40.773442+0800 Test[56833:11021466] method's signature: setArray:
2022-02-16 23:56:40.773543+0800 Test[56833:11021466] method's signature: string
2022-02-16 23:56:40.773650+0800 Test[56833:11021466] method's signature: setString:
2022-02-16 23:56:40.773753+0800 Test[56833:11021466] method's signature: integer
2022-02-16 23:56:40.773881+0800 Test[56833:11021466] method's signature: setInteger:
2022-02-16 23:56:40.774018+0800 Test[56833:11021466] method's signature: .cxx_destruct
2022-02-16 23:56:40.774132+0800 Test[56833:11021466] method method1
2022-02-16 23:56:40.774249+0800 Test[56833:11021466] class method: classMethod1
2022-02-16 23:56:40.774394+0800 Test[56833:11021466] MyClass is yes response to selector: method3WithArg1:arg2:
2022-02-16 23:56:40.774497+0800 Test[56833:11021466] call method method1
2022-02-16 23:56:40.774612+0800 Test[56833:11021466] protocol name: NSCopying
2022-02-16 23:56:40.774691+0800 Test[56833:11021466] protocol name: NSCoding
2022-02-16 23:56:40.774924+0800 Test[56833:11021466] MyClass is yes response to protocol NSCoding
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个说法是不准确的。在使用`open()`函数之前需要定义文件流对象,但并不一定要使用流对象操作文件。`open()`函数可以使用文件流对象的成员函数操作文件,也可以使用全局函数操作文件,例如: ```c++ #include <fstream> using namespace std; int main() { ofstream outfile; outfile.open("example.txt"); outfile << "Hello World!" << endl; outfile.close(); return 0; } ``` 在这个例子,我们使用`ofstream`定义了一个文件流对象`outfile`,然后使用其成员函数`open()`打开名为`example.txt`的文件,并通过`outfile << "Hello World!" << endl;`将`"Hello World!"`写入文件。最后使用`close()`函数关闭文件。 除了使用文件流对象操作文件,我们还可以使用全局函数操作文件,如下所示: ```c++ #include <fstream> using namespace std; int main() { ofstream outfile; outfile.open("example.txt"); outfile << "Hello World!" << endl; outfile.close(); ofstream outfile2; outfile2.open("example.txt", ios::app); outfile2 << "Hello C++!" << endl; outfile2.close(); return 0; } ``` 在这个例子,我们先使用`ofstream`定义了一个文件流对象`outfile`,然后使用其成员函数`open()`打开名为`example.txt`的文件,并通过`outfile << "Hello World!" << endl;`将`"Hello World!"`写入文件,最后使用`close()`函数关闭文件。接着我们再次定义一个`ofstream`对象`outfile2`,使用全局函数`ios::app`打开文件,将`"Hello C++!"`写入文件。最后同样使用`close()`函数关闭文件。 因此,可以看出,在使用`open()`函数之前我们需要定义文件流对象,但并不一定要使用流对象操作文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值