使用全局数组或其他更好的设计

时间:2015-10-21 02:41:48

标签: ios objective-c design-patterns global-variables

对于app的一致性,我在global.h中声明了一些const字符串:

# Convert some money to an appropriate collection of cents
penny = 1
nickel = 5
dime = 10
quarter = 25

quarters = 0
dimes = 0
nickels = 0
pennys = 0

cents = int(input("Please enter an amount of money you have in cents: "))

if cents >= 25:
    quarters = cents / quarter
    cents % quarter
if cents >= 10:
    dimes = cents/dime
    cents % dime
if cents >= 5:
    nickels = cents /nickel
    cents % nickel
if cents > 0:
    pennys = cents / penny
    cents = 0

print ("The coins are: quarters", quarters,\
",dimes", dimes, ",nickels", nickels, ", and pennys.", pennys)

并在global.m中定义它们:

extern NSString *const GDD_UI_COMFIRM_TITLE;//comfirm button title
extern NSString *const GDD_UI_CANCELL_TITLE;//cancell button title

现在,我需要一个全局数组,如:

NSString *const GDD_UI_COMFIRM_TITLE = @"ok";
NSString *const GDD_UI_CANCELL_TITLE = @"cancel";

我希望像全局const字符串一样访问数组,以便代码看起来一致,但如果我在global.h / .m中声明和定义数组,那将是错误的。 有人建议使用类来包装数组,但它符合我的一致要求。 顺便说一句,我听说有人说使用全局变量不是一个好主意,是否有全局变量的替代品?

THX。

0 个答案:

没有答案