0

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.

1
  • why cant you just save the day itself.. and also please post some code. Commented Dec 6, 2011 at 6:08

3 Answers 3

1

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: ;
        } 
Sign up to request clarification or add additional context in comments.

5 Comments

@Narayana i am getting confused that what is YouInt .Could you please explain in detail
@Rani for example if get 1 only from database just pass 1 to switch case or if you get 1 and 2 from database just run two times that switch case to times k
1st you have to store your database value in to the array than after put this array into the switch case like this NSmutableArray *YourInt; for(int i = 0;i>[yorInt count]li++) { swiitch([YourInt objectatindex:i]) { /*your all code which is above */ } } And keep all think as above
@Narayana i think 1st she has to put the value into the array than put that array in to switch case
@Narayana the value is getting displayed in label.But the problem is i have a column in my sqlite database name alarm_id which is a foreign key . i.e if i choose monday and tuesday for that particular alarm same alarmid is entered with values 2 and 3 for days in the database so how to fetch days depending on particular id.
1

Use objective C++.

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

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

Comments

1

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.

2 Comments

Rahul i am getting confused that you have define the enum name a WEEKDAYS and not used it anywhere.
I have updated my answer . Hope I could explain the usage of declaring the enum.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.