clement
clement

Reputation: 4266

try to perform a sort of concatenation

I have trouble by doing a sort of concatenation;

I receive 4 different datas that I get with "[dico objectForKey:@"alertSerie"]" (for example). The datas must be shown with a String that introduce datas on the screen. I have to put those datas in one UITextField.

Example :

[dico objectForKey:@"alertfriend"] = Beth
[dico objectForKey:@"alertmom"] = Lise
[dico objectForKey:@"alertgirlf"] = Angela


text for Beth will be "This is my friend :"
text for Lise will be "This is my mom :"
text for Angela will be "This is my girlfriend :"

and in the UITextField, I want to show:

This is my friend :Beth
This is my mom : Lise
This is my girlfriend : Angela

Until now it's OK, the code looks like :

field.text = [NSString stringWithFormat:@"NThis is my friend : %@\nTHis is my mom : %@\nThis is my girlfriend : %@\n",[dico objectForKey:@"alertfriend"], [dico objectForKey:@"alertmom"], [dico objectForKey:@"alertgfriend"] ];

Now, it could be possible that one person have no friend or no mom or no girlfriend...

How can I do???

I tried a lot of possibilities but the fact that we cannot concatenate object isn't usual for me and it cause me a problem in this situation...

Thanks a lot for your attention!!!

EDIT:

I tried that code but there are mistakes of ; ] or something else...

    if ([dico objectForKey:@"alertSerie"]) {
        NSString *serie = [NSString stringWithFormat:@"Numéro de série : %@\n", [dico objectForKey:@"alertSerie"] ];}
    else {
        NSString *serie = [NSString stringWithFormat:@" "] ];}

    if ([dico objectForKey:@"alertDate"]) {
        NSString *date = [NSString stringWithFormat:@"Date de mise en service : %@\n", [dico objectForKey:@"alertDate"] ];}
    else {
        NSString *date = [[NSString stringWithFormat:@" "] ];}

    if ([dico objectForKey:@"alertCli"]) {
        NSString *cli = [NSString stringWithFormat:@"Nom du client associé : %@\n", [dico objectForKey:@"alertCli"] ];}
    else {
        NSString *cli = [NSString stringWithFormat:@" "] ];}

Upvotes: 0

Views: 95

Answers (3)

Man of One Way
Man of One Way

Reputation: 3980

You need to check wether the person has a friend, girlfriend and mom. This means that you must use conditions. This could be done by using some simple if-statements.

NSMutableString *text = [[NSString alloc] init];

if([dico objectForKey:@"alertmom"] != nil)
{
   [text appendFormat:@"This is my Mom: %@ \n", [dico objectForKey:@"alertmom"]];
}
if([dico objectForKey:@"alertfriend"]!= nil)
{
   [text appendFormat:@"This is my friend: %@ \n", [dico objectForKey:@"alertfriend"]];
}
if([dico objectForKey:@"alertgirlf"] != nil)
{
   [text appendFormat:@"This is my girlfriend: %@ \n", [dico objectForKey:@"alertgirlf"]];
}

field.text = text;

Upvotes: 1

Praveen S
Praveen S

Reputation: 10393

NSMutableString *string = [[NSMutableString alloc] init];
if([dico objectForKey:@"alertfriend"] != nil)
{
[string appendString:[NSString stringwithformat:@"This is my friend: %@ \n",[dico objectForKey:@"alertfriend"]];
}

In the similar way append the other strings if the return from dictionary is not equal to nil.

Upvotes: 1

Sascha Galley
Sascha Galley

Reputation: 16091

You can make a simple check if the key exists, otherwise use an empty string:

NSString *mom = [dico objectForKey:@"alertmom"];
if (mom == nil)
    mom = @"";

You can also go further and create a simple function:

-(NSString *) getValueFromKey:(NSString* )theKey inDictionary:(id)theDic {
    if([theDic objectForKey:theKey] == nil)
        return @"";
    else
        return [theDic objectForKey:theKey];
}

Upvotes: 1

Related Questions