2014-01-08 22 views
0

我只想保持單個文件中的所有字符串值((例如)錯誤代碼,警報文本)。該文本可以在類似於android中的string.xml的項目中使用。我不想使用宏。如何保持單個文件中的字符串常量 - ios

這可能嗎?或者有沒有其他的方式在iOS中做到這一點?

+2

您可以使用'extern NSString * const stringV;' –

+0

創建一個NSString類並根據需求聲明方法,以便您可以在整個項目中使用它。如果你需要改變文字,它將很容易改變。 –

回答

1

您可以創建一個類,如:

.H

@interface MyConstants : NSObject 

extern NSString * const kCon; 

@end 

.M

@implementation MyConstants 

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

@end 

您可以將.h文件導入到任意位置,並且可以使用這些常量。

如果你不想導入頭文件NSUserDefaults將是一個不錯的選擇,但在NSUserDefaults存儲太多的數據不鼓勵。

+0

謝謝,我會嘗試你的。 – Dax

0

您可以使用NSUserdefault存儲字符串或任何對象值,並通過應用程序訪問它。 這樣的..

用於存儲值

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

而對於retriving數據

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

或者你可以不喜歡它thisthis

2

通常我在我的項目中添加一個名爲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 
+0

通常我會,你在說什麼。我聽說,使用Localization.string文件我們可以實現結果,我需要什麼。你知道這件事嗎? – Dax

+0

@Dax,看到我更新的答案。 – sunkehappy

0

兩種方式:

  1. 將它保存在文件夾的plist。
  2. 使用本地化
+0

如何通過本地化。你能解釋一下嗎? – Dax

0
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]; 
相關問題