如何在Objective-C的头文件引用C++的头文件

最近做个项目需要用到C++与Objective C混合编程,下面的代码用于将NSString类型数据转换为string类型数据,NSDictionary类型数据转换为map类型数据,发现在objective C中引用C++头文件会出错,捣鼓了半天,发现这样做可以解决:

1.将.m文件扩展名改为.mm,或者修改.m文件的File Type,改为Objective C++ Source.

2.将.h文件File Type改为Objective C++ Preprocessed Source,默认C Header.

//
//  PlatformCommon.h
//  LibObject
//
//  Created by chen yang on 13-3-20.
//  Copyright (c) 2013年 admin. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <iostream>
#import <map>


#define NSStringToString(str)  (str != nil ? [str UTF8String] : "")

#define StringToNSString(str)  [NSString stringWithUTF8String:str.c_str()]


@interface PlatformCommon : NSObject

+ (std::map<std::string, std::string> )NSDictionaryToMap:(NSDictionary *)params;

@end

//
//  PlatformCommon.mm
//  LibObject
//
//  Created by chen yang on 13-3-20.
//  Copyright (c) 2013年 admin. All rights reserved.
//

#import "PlatformCommon.h"
#import <iostream>
#import <map>

using namespace std;

@implementation BlcPlatformCommon

+ (std::map<std::string, std::string> )NSDictionaryToMap:(NSDictionary *)params
{
    NSArray *allKeys = [params allKeys];
    
    std::map<std::string, std::string> mapParams;
    
    for (NSString *key in allKeys)
    {
        mapParams[NSStringToString(key)] = NSStringToString([params objectForKey:key]);
    }
    
    return mapParams;
}


@end


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值