user1824518
user1824518

Reputation: 223

Converting from std::string to NSString

How can I convert from std::string to NSString in this case?

void Piles::imagePlacer(Card card, int xCoord, int yCoord) {
    string cardType = card.toString();

    UIImageView *cardImg = [[UIImageView alloc]
                 initWithImage:cardType];
    cardImg.frame = CGRectMake(xCoord, yCoord, 126, 190);
    [self.view addSubview:cardImg];
}

I want to use a string as the name of the image (assuming I have a list of UIImages somewhere with names that correspond to all possible strings stored in cardType)?

Upvotes: 2

Views: 4839

Answers (2)

Deepak Kumar
Deepak Kumar

Reputation: 1044

Here is example for this :

string str_simple = "HELLO WORLD";

//string to NSString

NSString *stringinObjC = [NSString stringWithCString:str_simple.c_str()
                            encoding:[NSString defaultCStringEncoding]];


NSLog(stringinObjC);

Upvotes: 0

newacct
newacct

Reputation: 122519

Just go through a C string:

[NSString stringWithUTF8String:cardType.c_str()]

or using the new syntax:

@(cardType.c_str())

Upvotes: 13

Related Questions