【问题标题】:How to maintain string constants in a single file - ios如何在单个文件中维护字符串常量 - ios
【发布时间】:2014-01-08 12:52:44
【问题描述】:

我只想在单个文件中维护所有字符串值((例如)错误代码、警报文本)。该文本可以在整个项目中使用,类似于 android 中的 string.xml。而且我不想使用宏。

这可能吗?或者在iOS中有没有其他方法可以做到这一点?

【问题讨论】:

  • 你可以使用extern NSString* const stringV;
  • 创建一个 NSString 类并根据要求声明方法,以便您可以在整个项目中使用它。如果您需要更改文本,它会很容易更改。

标签: ios string


【解决方案1】:

通常我会在我的项目中添加一个名为Constant.h 的文件,并将其导入我的项目的-Prefix.pch 文件中,以便我可以访问任何文件中的内容。

缺点是当我修改Constant.h 文件时,我需要编译整个项目文件。如果你的机器不是那么快的话,时间会很长。

例子:

#ifndef BadgeDiscount_Constant_h
#define BadgeDiscount_Constant_h

typedef enum {
    kErrorCodeTokenExpired = 61001,
    kErrorCodeServerInternalError = 500,
    kErrorCodeRequestTimeout = -1001,
    kErrorCodeCouldNotAccessServer = -1004,
    kErrorCodeInternetUnavailable = -1009,
} ErrorCode;

typedef enum {
    kRowTypeTopAndAlsoBottom = 1,
    kRowTypeTop,
    kRowTypeMiddle,
    kRowTypeBottom
} RowType;

extern CGFloat kScreenWidth;
extern CGFloat kScreenHeight;

static CGFloat const kStatusBarHeight = 20;
static CGFloat const kNavigationBarHeight = 44;

static NSString* const kErrorMessage = @"errMsg";
static NSString* const kErrorCode = @"errCode";

#endif

还有 -Prefix.pch 文件:

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import "Constant.h"
    #import "AppDelegate.h"
    #import "MBProgressHUD.h"
#endif

【讨论】:

  • 通常我会,你在说什么。我听说,使用 Localization.string 文件我们可以达到我需要的结果。你知道吗?
【解决方案2】:

你可以像这样创建一个类:

.h

@interface MyConstants : NSObject

extern NSString * const kCon;

@end

.m

@implementation MyConstants

NSString * const kCon = @"My Constant String";

@end

您可以在任意位置导入.h 文件,并且可以使用常量。

如果您不想导入头文件NSUserDefaults 将是一个不错的选择,但不鼓励在NSUserDefaults 上存储太多数据。

【讨论】:

  • 谢谢,我试试你的。
【解决方案3】:

您可以使用 NSUserdefault 来存储字符串或任何对象值并在整个应用程序中访问它。 像这样..

用于存储价值

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"name" forKey:@"Dilip"];
[defaults synchronize];

用于检索数据

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *name = [defaults objectForKey:@"name"];
NSLog(@"name : %@",name);

或者你可以像 thisthis 那样做。

【讨论】:

    【解决方案4】:

    两种方式:

    1. 将其作为 plist 保存在文档文件夹中。
    2. 使用本地化

    【讨论】:

    • 本地化怎么做。你能解释一下吗?
    【解决方案5】:
    Create an NSString Class and declare methods as per requirement so that you can use it through out your project. if you need to change text it will be easy to change.
    EX:
    .h file
    #import <Foundation/Foundation.h>
    @interface NSString(devicetype)
    
    + (NSString *)yesButWhichDeviceIsIt;
    + (NSString *)versionofiOS;
    
    @end
    
    .m file
    
    #import "devicetype.h"
    @implementation NSString(devicetype)
    
    // This method will generate device type
    
    + (NSString *)yesButWhichDeviceIsIt
    {
    BOOL hasRetina = NO;
    if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
        CGFloat scale = [[UIScreen mainScreen] scale];
        if (scale > 1.0) {
            hasRetina = YES;
        }
    }
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        if (hasRetina) {
            return @"iPad retina";
        } else {
            return @"iPad";
        }
    } else {
        if (hasRetina) {
            if ([[UIScreen mainScreen] bounds].size.height == 568){
                return @"iPhone5";
            }
            else
            {
                return @"iPhone4s";
            }
        } else {
            return @"iPhone";
        }
    }
    }
    // This method will generate device version
    + (NSString *)versionofiOS
    {
    NSString *deviceVerion=[[UIDevice currentDevice] systemVersion];
    return deviceVerion;
    }
    
    @end
    Example for Calling this method is import this class and write this method [NSString versionofiOS];
    

    【讨论】:

      猜你喜欢
      • 2023-03-18
      • 1970-01-01
      • 2011-02-03
      • 2015-05-26
      • 1970-01-01
      • 1970-01-01
      • 2010-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多