Rani
Rani

Reputation: 3453

How to map integer value to string in objective-C

I have an application in which I am entering values in database in integer type. I have a tableview with 7 rows in it from Sunday to Saturday. When user selects Sunday and clicks on the save button an integer value of 0 is entered in the days table with an id.

If the user selects Monday and Tuesday and clicks on the save button then integer values of 2 and 3 are entered in the database for that particular id. This is working fine. But the problem is I want to fetch these integer values and display string values in a label.

i.e for a particular id if 1 and 2 is entered in database then in the label Sunday, Monday should be displayed.

Upvotes: 0

Views: 1589

Answers (3)

Rahul Sharma
Rahul Sharma

Reputation: 3013

Rani , you have not mentioned What database you are using i.e core data or SQLite. As You have mentioned you have already saved integer values in database all you need to do is

int WEEKDAY_INTVALUE
while (sqlite3_step(statement) == SQLITE_ROW) {
      WEEKDAY_INTVALUE =  sqlite3_column_int(statement, 0);
     NSLog(@"The value is  %d ", fieldValue);
}

Once you grab the int value you can use a predefined Enum to get the string value using something like

typedef enum {

    SUNDAY = 0,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY
}WEEKDAYS;

You can pass this integer value grabbed using a switch case and assign the Weekday text by something like

switch (WEEKDAY_INTVALUE) {
    case SUNDAY:
        [lbl setText:@"Sunday"];
        break;
    default:
        break;
}

EDIT://Use of WEEKDAYS Enum

If you take use of datamodels such as follows

@interface Anything : NSObject {

    WEEKDAYS        savedWeekDay;
    NSString*   someField;
}

Now When you create Objects of this "Anything" class such as

NSMutableArray* arrAllObjects = [[[NSMutableArray alloc] initWithCapacity:0] autorelease];

 while (sqlite3_step(statement) == SQLITE_ROW) {
    Anything* object = [[Anything alloc] init];
    object.savedWeekDay = sqlite3_column_int(statement, 0);
     object.someField = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
    [arrAllObjects addObject:object];
    [object release];
}

You later pass this array from your DBCommunicator to your ViewController. Though this datamodel approach does not directly relate to your question statement but this is the way I use database handling in my applications. Hope you can take some hint from it.

Upvotes: 1

Narayana Rao Routhu
Narayana Rao Routhu

Reputation: 6323

You can enum like below

enum {SUNDAY, MUNDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY};

and use switch case in that pass that your integer

NSString *str_label = @"";
    switch (YourInt) {
            case SUNDAY: str_label=[str_label stringByAppendingString:@"SUNDAY"];break;
            case MUNDAY: str_label=[str_label stringByAppendingString:@"MUNDAY"]; break;
            case TUESDAY: str_label=[str_label stringByAppendingString:@"TUESDAY"]; break;
            default: ;
        } 

Upvotes: 1

Parag Bafna
Parag Bafna

Reputation: 22930

Use objective C++.

std::map<int, std::string> mapping;  

Refer how can I map an int to a corresponding string in C/C++ post.

Upvotes: 1

Related Questions