如何在objective-c中编写一个函数,我可以使用我的iPhone应用程序中的任何对象?

时间:2022-09-07 09:24:28

I want to create a function which I pass an object. Then, this function should print out some information for me.

我想创建一个传递对象的函数。然后,此功能应该为我打印出一些信息。

like:

analyzeThis(anyObject);

I know about methods, but not how to make a function. As I understand, a function works global in all methods and classes, right?

我知道方法,但不知道如何制作功能。据我所知,一个函数在所有方法和类中都是全局的,对吗?

3 个解决方案

#1


14  

You would do it the same way as any other function that you would write in C. So, if you have your function defined in a source file named myLibraryFunctions.m, anyone who wanted to use that function would include the header file where you define it (probably myLibraryFunctions.h, and then just make sure that they link with the object file (most of this will be done for you in Xcode if you simply create a file to house your "global" functions and include the header file for them in any source file that you access it from. Then just define it:

您将以与在C中编写的任何其他函数相同的方式执行此操作。因此,如果您在名为myLibraryFunctions.m的源文件中定义了函数,则任何想要使用该函数的人都将包含您定义的头文件它(可能是myLibraryFunctions.h,然后只是确保它们与目标文件链接(如果您只是创建一个文件来容纳您的“全局”函数并包含它们的头文件,那么大部分将在Xcode中为您完成)在您从中访问它的任何源文件中。然后只需定义它:

void analyzeThis(id anyObject);

void analyzeThis(id anyObject) {
    NSLog(@"Object %p: %zu, %@", anyObject, malloc_size(anyObject), anyObject);
}

But in reality, you would write whatever you wanted in your function. These functions don't have to be a part of any class definitions or anything like that. In this case, they're exactly the same as they would be in C.

但实际上,你会在你的功能中写下你想要的任何东西。这些函数不必是任何类定义的一部分或类似的东西。在这种情况下,它们与它们在C中完全相同。

#2


1  

Are you trying to reimplement NSObject's description method? If you send an NSObject message it will return a string containing information about itself. You can override -(NSString*)description and add more information to this string.

您是否尝试重新实现NSObject的描述方法?如果发送NSObject消息,它将返回一个包含有关其自身信息的字符串。您可以覆盖 - (NSString *)描述并向此字符串添加更多信息。

If you do this, you'll find that magic happens - for example when you print an array object in the debugger it will iterate all the members of the array and print their descriptions.

如果你这样做,你会发现魔术发生 - 例如,当你在调试器中打印一个数组对象时,它会迭代数组的所有成员并打印它们的描述。

#3


1  

Two ways exist for doing this:

有两种方法可以做到这一点:

  • write a global function, just as you would do in C
  • 编写一个全局函数,就像在C中一样

  • create a category of NSObject in which you implement a method which then will be available to all classes that have NSObject as ancestor.
  • 创建一个NSObject类,在其中实现一个方法,然后该方法可用于所有以NSObject为祖先的类。

#1


14  

You would do it the same way as any other function that you would write in C. So, if you have your function defined in a source file named myLibraryFunctions.m, anyone who wanted to use that function would include the header file where you define it (probably myLibraryFunctions.h, and then just make sure that they link with the object file (most of this will be done for you in Xcode if you simply create a file to house your "global" functions and include the header file for them in any source file that you access it from. Then just define it:

您将以与在C中编写的任何其他函数相同的方式执行此操作。因此,如果您在名为myLibraryFunctions.m的源文件中定义了函数,则任何想要使用该函数的人都将包含您定义的头文件它(可能是myLibraryFunctions.h,然后只是确保它们与目标文件链接(如果您只是创建一个文件来容纳您的“全局”函数并包含它们的头文件,那么大部分将在Xcode中为您完成)在您从中访问它的任何源文件中。然后只需定义它:

void analyzeThis(id anyObject);

void analyzeThis(id anyObject) {
    NSLog(@"Object %p: %zu, %@", anyObject, malloc_size(anyObject), anyObject);
}

But in reality, you would write whatever you wanted in your function. These functions don't have to be a part of any class definitions or anything like that. In this case, they're exactly the same as they would be in C.

但实际上,你会在你的功能中写下你想要的任何东西。这些函数不必是任何类定义的一部分或类似的东西。在这种情况下,它们与它们在C中完全相同。

#2


1  

Are you trying to reimplement NSObject's description method? If you send an NSObject message it will return a string containing information about itself. You can override -(NSString*)description and add more information to this string.

您是否尝试重新实现NSObject的描述方法?如果发送NSObject消息,它将返回一个包含有关其自身信息的字符串。您可以覆盖 - (NSString *)描述并向此字符串添加更多信息。

If you do this, you'll find that magic happens - for example when you print an array object in the debugger it will iterate all the members of the array and print their descriptions.

如果你这样做,你会发现魔术发生 - 例如,当你在调试器中打印一个数组对象时,它会迭代数组的所有成员并打印它们的描述。

#3


1  

Two ways exist for doing this:

有两种方法可以做到这一点:

  • write a global function, just as you would do in C
  • 编写一个全局函数,就像在C中一样

  • create a category of NSObject in which you implement a method which then will be available to all classes that have NSObject as ancestor.
  • 创建一个NSObject类,在其中实现一个方法,然后该方法可用于所有以NSObject为祖先的类。