1

std::mapの値をNSMutableArray要素に変換したいと思います。
私のCPPコードは、インターフェイス関数を使用してstd :: map(1つのintとstd :: stringを含む)をObjective-C関数に渡しています。

しかし、このstd::mapを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また、テーブルビューの行を表示するために使用したいと思います。
どうすればいいですか。

4

2 に答える 2

2

使用するだけ

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

またはNSStringEncodingあなたの特定のコンテンツのために働くものは何でも!

于 2012-12-06T14:55:23.933 に答える
0

std::stringをNSStringに変換する必要があります。使用する正確なstd::stringメソッドはわかりませんが、次のようにする必要があります。

[[NSString alloc] initWithCharacters:ii->second.c_str() length:ii->second.length()]
于 2012-12-06T14:41:35.173 に答える