class_getName
时间: 2023-06-23 07:56:37 浏览: 55
`class_getName`是Objective-C Runtime提供的一个函数,用于获取指定类的类名。其函数原型如下:
```objective-c
const char *class_getName(Class cls)
```
其中,`cls`参数是要获取类名的类。
该函数返回一个`const char *`类型的指针,指向指定类的类名。
例如,以下代码获取`NSString`类的类名:
```objective-c
const char *className = class_getName([NSString class]);
NSLog(@"Class name: %s", className);
```
输出结果为:
```
Class name: NSString
```
需要注意的是,如果要获取的类不存在,该函数返回`NULL`。此外,如果需要将`const char *`类型的类名转换为`NSString`类型,可以使用`[NSString stringWithUTF8String:]`方法。
相关问题
class_getInstanceMethod
`class_getInstanceMethod`是Objective-C Runtime提供的一个函数,用于获取指定类的实例方法。其函数原型如下:
```objective-c
Method class_getInstanceMethod(Class cls, SEL name)
```
其中,`cls`参数是要获取方法的类,`name`参数是要获取的方法的选择器。
该函数返回一个`Method`类型的指针,该指针指向指定类的指定实例方法的描述信息。如果未找到指定方法,则返回`NULL`。
例如,以下代码获取`NSString`类的`uppercaseString`方法:
```objective-c
Method method = class_getInstanceMethod([NSString class], @selector(uppercaseString));
```
需要注意的是,如果要获取的方法是一个类方法,需要使用`class_getClassMethod`函数。另外,还可以使用`method_getName`函数获取方法选择器,使用`method_getImplementation`函数获取方法实现函数指针等。这些函数都是Objective-C Runtime提供的API,用于在运行时操作Objective-C对象和方法。
class Pet {} class Dog extends Pet {} class Pug extends Dog {} class Cat extends Pet {} class Rodent extends Pet {} class Gerbil extends Rodent {} class Hamster extends Rodent {} class Main { static HashMap<Integer, Class<? extends Pet>> map = new HashMap<Integer, Class<? extends Pet>>(); static { map.put(Pet.class.getName().length(), Pet.class); map.put(Dog.class.getName().length(), Dog.class); map.put(Pug.class.getName().length(), Pug.class); map.put(Cat.class.getName().length(), Cat.class); map.put(Rodent.class.getName().length(), Rodent.class); map.put(Gerbil.class.getName().length(), Gerbil.class); map.put(Hamster.class.getName().length(), Hamster.class); } public static void main(String[] args) { for ( Integer i : map.keySet() ) System.out.println(map.get(i).getName()); }} The code below will print three lines.运行结果,为什么
运行结果会打印出以下三行:
```
Pet
Pug
Hamster
```
这是因为 `map` 是一个 `HashMap`,它按照键的顺序存储值。在这种情况下,键是类名的长度,值是对应的类。当遍历 `map` 并打印值时,它们按照键的升序进行打印。
首先,`Pet.class.getName().length()` 的值为3,所以打印出 `Pet`。
接下来,`Pug.class.getName().length()` 的值为3,所以打印出 `Pug`。
最后,`Hamster.class.getName().length()` 的值为7,所以打印出 `Hamster`。
其他类的长度不是3或7,所以它们不会被打印出来。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)