将std :: map转换为Objective-C NSMutableArray元素

时间:2012-12-06 14:03:19

标签: objective-c xcode cocoa

我想将std :: map的值转换为NSMutableArray元素 我的CPP代码使用接口函数将std :: map(带有一个int和std :: string)传递给Objective-C函数。

但我无法将此std :: man转换为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

此外,我想使用mArray来显示表格视图行 我该怎么办

2 个答案:

答案 0 :(得分:2)

只需使用

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

或其他NSStringEncoding适用于您的特定内容!

答案 1 :(得分:0)

你必须将你的std :: string转换为NSString。不知道要使用的确切std :: string方法,但你应该这样做:

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