在Swift中,可以使用structenum来定义全局字符串。例如:

struct GlobalStrings {
    static let welcomeMessage = "Welcome to my app!"
    static let errorMessage = "An error occurred."
}

然后,你可以在任何需要使用这些全局字符串的地方,使用GlobalStrings.welcomeMessageGlobalStrings.errorMessage来访问这些字符串。

在Objective-C中,你可以使用extern关键字来定义全局字符串。例如:

// GlobalStrings.h
extern NSString * const welcomeMessage;
extern NSString * const errorMessage;

// GlobalStrings.m
NSString * const welcomeMessage = @"Welcome to my app!";
NSString * const errorMessage = @"An error occurred.";

然后,你可以在任何需要使用这些全局字符串的地方,使用welcomeMessageerrorMessage来访问这些字符串。


原文地址: https://www.cveoy.top/t/topic/przw 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录