0

I just want to maintain all the string value ((eg) error code, alert text) in single file. That text can be used all over the project similar to string.xml in android. And i don't want to use macros.

Is that possible? or Is there any other way to do it in iOS?

Dax
  • 6,908
  • 5
  • 23
  • 30

5 Answers5

2

Usually I add one file called Constant.h in my project and import it in my project's -Prefix.pch file so that I can access content in any file.

The drawback is when I modify the Constant.h file I need to compile the whole project file. A long time if your machine is not so fast.

Example:

#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

And the -Prefix.pch file:

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import "Constant.h"
    #import "AppDelegate.h"
    #import "MBProgressHUD.h"
#endif
sunkehappy
  • 8,970
  • 5
  • 44
  • 65
  • Usually i do, what you are saying. I heard, using Localization.string file we can achieve the result, what i need. Do you know anything about that? – Dax Jan 08 '14 at 13:08
1

You can create a class like:

.h

@interface MyConstants : NSObject

extern NSString * const kCon;

@end

.m

@implementation MyConstants

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

@end

You can import the .h file wherever you want and you can use the constants.

If you don't want to import header files NSUserDefaults will be a good choice, but storing too much data on NSUserDefaults in not encouraged.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
0

You can use NSUserdefault to store the String or any object value and access it all over the app. Like this..

For Storing the value

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

And for retriving the data

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

Or you can do it like this and this.

Community
  • 1
  • 1
Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
0

Two ways:

  1. save it as plist in documents folder.
  2. use localization
Praveen Matanam
  • 2,773
  • 1
  • 20
  • 24
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];
Charan Giri
  • 1,097
  • 1
  • 9
  • 15