gpt4 book ai didi

ios - 从任何类实例访问实用程序方法?

转载 作者:行者123 更新时间:2023-11-29 02:51:50 25 4
gpt4 key购买 nike

(这可能已经在其他地方得到了回答,但我不知道要搜索什么才能找到它)

我发现下面的 printErrorMessage 方法非常有用。我一直将它包含在我所有的类(class)中,但就复制代码而言,这有点愚蠢。我可以将其定义为单独的 Utility 类中的类方法吗?

如果重要的话,这是在 iOS 上。

- (void) printErrorMessage: (NSString *) errorString withStatus: (OSStatus) result
{
char str[20];
// see if it appears to be a 4-char-code
*(UInt32 *)(str + 1) = CFSwapInt32HostToBig(result);
if (isprint(str[1]) && isprint(str[2]) && isprint(str[3]) && isprint(str[4])) {
str[0] = str[5] = '\'';
str[6] = '\0';
} else
// no, format it as an integer
sprintf(str, "%d", (int)result);

NSLog (@"*** %@ error: %s\n", errorString, str);
}

最佳答案

Can I just define this as a Class method in a separate Utility class?

当然可以。这就是类方法的用途。或者,通过类别将其注入(inject)到某个现有类中。

关于ios - 从任何类实例访问实用程序方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24397566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com