有时候,出于性能或可移植性的考虑,需要在iOS项目中使用到C++。
假设我们用C++写了下面的People类:
//
// People.h
// MixedWithCppDemo
//
// Created by Jason Lee on 12-8-18.
// Copyright (c) 2012年 Jason Lee. All rights reserved.
//
#ifndef __MixedWithCppDemo__People__
#define __MixedWithCppDemo__People__
#include <iostream>
class People
{
public:
void say(const char *words);
};
#endif /* defined(__MixedWithCppDemo__People__) */
//
// People.cpp
// MixedWithCppDemo
//
// Created by Jason Lee on 12-8-18.
// Copyright (c) 2012年 Jason Lee. All rights reserved.
//
#include "People.h"
void People::say(const char *words)
{
std::cout << words << std::endl;
}
然后,我们用Objective-C封装一下,这时候文件后缀需要为.mm,以告诉编译器这是和C++混编的代码:
//
// PeopleWrapper.h
// MixedWithCppDemo
//
// Created by Jason Lee on 12-8-18.
// Copyright (c) 2012年 Jason Lee. All rights reserved.
//
#import <Foundation/Foundation.h>
#include "People.h"
@interface PeopleWrapper : NSObject
{
People *people;
}
- (void)say:(NSString *)words;
@end
//
// PeopleWrapper.mm
// MixedWithCppDemo
//
// Created by Jason Lee on 12-8-18.
// Copyright (c) 2012年 Jason Lee. All rights reserved.
//
#import "PeopleWrapper.h"
@implementation PeopleWrapper
- (void)say:(NSString *)words
{
people->say([words UTF8String]);
}
@end
最后,我们需要在ViewController.m文件中使用PeopleWrapper:
PeopleWrapper *people = [[PeopleWrapper alloc] init];
[people say:@"Hello, Cpp.\n"];
[people release], people = nil;
结果发现编译通不过,提示“iostream file not found”之类的错误。
这是由于ViewController.m实际上也用到了C++代码,同样需要改后缀名为.mm。
修改后缀名后编译通过,可以看到输出“
Hello, Cpp.
”。
分享到:
相关推荐
C++和Objective-C混编 如何在xcode中用C++的STL 这是一个工程例子。我的blog有提到。
自己在Xcode4下编写的Objective-C和C语言混编的计算器
苹果的Objective-C编译器允许用户在同一个源文件里自由地混合使用C++和Objective-C,混编后的语言叫Objective-C++。有了它,你可以在Objective-C应用程序中使用已有的C++类库。 Objective-C和C++混编的要点 ...
《Objective-C 程序设计(第4版)》已经为iOS 5和Xcode4.2中的重大变更做了全面更新,最大的改动是引入了自动引用计数(ARC),并详细说明了如何在Objective-C编程过程中使用ARC提升和简化内存管理。
3.2 在面向对象的编程中使用间接24 3.2.1 过程式编程24 3.2.2 实现面向对象编程29 3.3 学习有关的术语33 3.4 Objective-C中的OOP34 3.4.1 @interface部分34 3.4.2 @implementation部分38 3.4.3 实例化对象40 ...
网上看到一些混合编程的文章,总是遇到一些问题。经过半天时间的摸索,自己终于可以顺利完成啦。以下步骤中,Hello类只是为Objective-C调用C++做的一个封装。讲解一下操作步骤.
可对 Xcode 项目工程所有的 objective-c 文件内包含的明文进行加密混淆,提高逆向分析难度
Xcode 使用技巧 Objective-C学习之路 iphone
iOS 7 Programming Fundamentals: Objective-C, Xcode, and Cocoa Basics by Matt Neuburg (Author) Publisher: O’Reilly Media (October 2013) Language: English ISBN-10: 1491945575 ISBN-13: 978-...
我就只要一分,小弟不才发现网络上很难找这种例子,而我查外文资料查了一天。给大家提供个方便吧!
Objective-C 分离 接口和实现文件,具体资料请参考苹果开发者Mike的博文http://blog.sina.com.cn/s/blog_7aa21f320100r7wn.html
Pro Objective-C The Objective-C programming language continues to grow in popularity and usage because of the power and ease-of-use of the language itself, along with the numerous features that ...
● 如何在其他平台上使用Objective-C 凭借深入的代码示例与清晰的解释说明,本书将帮助你轻松解决iOS开发人员每天都 会面临的挑战。通过使用书中介绍的知识与技能,读者能够快速且高效地构建出功能完备 的应用。书
从C++到Objective C 非常适合刚接触IOS开发的初学者 学习
Objective-C Programming: The Big Nerd Ranch Guide (2nd Edition).epub (epub 格式) Want to write iOS apps or desktop Mac applications? This introduction to programming and the Objective-C language is ...
Swift和Objective-C 混编注意事项整理: 前言 Swift已推出数年,与Objective-C相比Swift的语言机制及使用简易程度上更接地气,大大降低了iOS入门门槛。当然这对新入行的童鞋没来讲,的确算是福音,但对于整个iOS...
The flexibility of Apple’s developer tools: how to install Xcode and write programs in Objective-C The power of Cocoa and Cocoa touch: how to make Mac OS X applications or iOS apps that do cool stuff...
除了在 Mac系统上使用Objective-C,你还可以在Windows上搭建Objective-C 环境,虽然该环境与mac xcode无法比拟,用于初学者学习objective-c还是可以的
3.2 在面向对象的编程中使用间接24 3.2.1 过程式编程24 3.2.2 实现面向对象编程29 3.3 学习有关的术语33 3.4 Objective-C中的OOP34 3.4.1 @interface部分34 3.4.2 @implementation部分38 3.4.3 实例化对象40 3.4.4 ...