Swift和Objective-C中定义全局字符串的最佳实践
在Swift中,可以使用struct或enum来定义全局字符串。例如:
struct GlobalStrings {
static let welcomeMessage = "Welcome to my app!"
static let errorMessage = "An error occurred."
}
然后,你可以在任何需要使用这些全局字符串的地方,使用GlobalStrings.welcomeMessage或GlobalStrings.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.";
然后,你可以在任何需要使用这些全局字符串的地方,使用welcomeMessage或errorMessage来访问这些字符串。
原文地址: https://www.cveoy.top/t/topic/przw 著作权归作者所有。请勿转载和采集!