1

I want to convert values of std::map to NSMutableArray element.
My CPP code is passing std::map (with one int and std::string) to an Objective-C function using interface function.

But i am not able to convert this std::map to NSMutableArray.

CPP Code:
------------------------------------------------------------------    
typedef std::map<int, std::string> TemList;
temList temList;
temList[10]="an element";
temList[23]="another element";
temList[545]="last";

passing this map to an objective-c code using interface function. 

Objective-c Code
------------------------------------------------------------------  
- (void) testListUpdate:(const TemList&) pList
{
    NSMutableArray *mArray = [NSMutableArray alloc]init];
    

    for (TemList::const_iterator ii = pList.begin(); ii != pList.end(); ++ii)
    {
        ListItem item = [[ListItem alloc] init]
        
        // PROBLEM **** I am Not able to convert this ******
        item.name = ii->second.c_str();
        item.mId = (int) ii->first;
    
        [mArray addObject:item];
    
        //printf("OBJC Value[%s]: Key[%d]\n", ii->second.c_str(), ii->first);
    }
}

// Interface function implementation 
void interfaceListUpdate(void *self, const TemList& pList)
{
    printf("ObjectiveC Interface Function size[%d]\n", pList.size());
    [(id) self testListUpdate:pList];
}

// Using this struct as Array item
@interface ListItem : NSObject
{
    NSString *name;
    int mId;
}

@end

Also i want to use mArray for displaying table view rows.
How should i do it.

2
  • What does c_str() return? And what should item.name be (what is it's class)? Dec 6, 2012 at 14:11
  • std::string::c_str() returns a plain C character array of the string represented by the C++ string object (since the internal storage of std::string is opaque)
    – Jay
    Dec 6, 2012 at 14:56

2 Answers 2

2

Just use

NSString *string = [NSString stringWithCString:cppString.c_str() 
                                      encoding:NSUTF8StringEncoding];

or whatever NSStringEncoding works for your particular content!

0

you have to convert your std::string to a NSString. Dont know the exact std::string methods to use but you should do something like this:

[[NSString alloc] initWithCharacters:ii->second.c_str() length:ii->second.length()]

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.