什么是Objective C++,我可以在Xcode中使用这种语言吗?
带NDK的 Android 支持C/C++代码和iOS,Objective-C++也支持,那么我如何用Android和iOS之间共享的本机C/C++代码编写应用程序?
我有一个NSString对象,并希望将其转换为std::string.
我如何在Objective-C++中执行此操作?
嗨,我正在尝试将标准std::string转换成一个,NSString但我没有太多运气.
我可以使用以下代码成功地从NSStringa 转换为astd::string
NSString *realm = @"Hollywood";
std::string REALM = [realm cStringUsingEncoding:[NSString defaultCStringEncoding]];
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试以下操作时,我收到编译时错误
NSString *errorMessage = [NSString stringWithCString:REALM encoding:[NSString defaultCStringEncoding]];
Run Code Online (Sandbox Code Playgroud)
我得到的错误是
Cannot convert 'std::string' to 'const char*' in argument passing
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么吗?
提前致谢.
在我一直在研究的项目中,我们必须将C++子项目中的Cocoa通知发送到它上面的主项目.为此,我们构造一个映射,作为通知的userInfo字典的键值存储.
在其中一个项目中,以下代码编译得很好:
std::map<std::string, std::string> *userInfo = new std::map<std::string, std::string>;
char buffer[255];
sprintf(buffer, "%i", intValue1);
userInfo->insert(std::pair<std::string, std::string>("intValue1", std::string(buffer)));
sprintf(buffer, "%i", intValue2);
userInfo->insert(std::pair<std::string, std::string>("intValue2", std::string(buffer)));
if(condition)
userInfo->insert(std::pair<std::string, std::string>("conditionalValue", "true"));
PostCocoaNotification("notificationName", *userInfo);
Run Code Online (Sandbox Code Playgroud)
但是,当将其复制到另一个子项目中的相同文件中时,编译器会在userInfo-> insert调用上抛出以下内容:
"Implicit instantiation of undefined template 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >'"
Run Code Online (Sandbox Code Playgroud)
..并且找不到PostCocoaNotification的功能:
No matching function for call to 'PostCocoaNotification'
Run Code Online (Sandbox Code Playgroud)
此外,它会在系统标头中引发以下错误:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/usr/include/c++/4.2.1/bits/stl_pair.h:73:11: Implicit instantiation of undefined template 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >'
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/usr/include/c++/4.2.1/bits/stl_pair.h:74:11: Implicit instantiation of undefined template 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >'
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/usr/include/c++/4.2.1/bits/stl_pair.h:73:11: Implicit instantiation of undefined template 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >'
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:1324:13: …Run Code Online (Sandbox Code Playgroud) 具有一些Objective-C的项目具有一些C类,其实现文件具有.mm扩展名.
file.h
file.mm
Run Code Online (Sandbox Code Playgroud)
什么是.mm是什么意思?不应该只是.m?
我正在尝试将Objective-C与C++混合使用.当我编译代码时,我得到了几个错误.
啊
#import <Cocoa/Cocoa.h>
#include "B.h"
@interface A : NSView {
B *b;
}
-(void) setB: (B *) theB;
@end
Run Code Online (Sandbox Code Playgroud)
上午
#import "A.h"
@implementation A
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect {
// Drawing code here.
}
-(void) setB: (B *) theB {
b = theB;
}
@end
Run Code Online (Sandbox Code Playgroud)
BH
#include <iostream>
class B {
B() {
std::cout << "Hello from C++";
}
};
Run Code Online (Sandbox Code Playgroud)
以下是错误:
/Users/helixed/Desktop/Example/B.h:1:0 /Users/helixed/Desktop/Example/B.h:1:20: …Run Code Online (Sandbox Code Playgroud) 我一直在通过Hillegass书籍学习Objective-C和Cocoa,我觉得使用Objective-C++可能会更好.Objective-C似乎是开发UI的明确选择,但我有很强的C++背景,并且喜欢用C++开发应用程序后端并使用Objective-C++来进行UI集成.但我想知道Apple是否会继续开发Objective-C++,还是会成为死胡同.
是否有人使用Objective-C++?
在创建新类时,我倾向于默认使用.mm扩展名,以便我可以在以后使用ObjC++.
这样做有什么不利吗?你.m什么时候喜欢?并.m编译成可执行更快(这是由于C通常比C++更快)?
我有一个奇怪的链接器问题.我的代码看起来像这样:
double given_amount = self.modelController.levelCompleteRewardAmount;
swrve_currency_given(swrve, (CFStringRef)@"currencyName", given_amount);
Run Code Online (Sandbox Code Playgroud)
我在两个不同的地方有这个代码:在objective-c和objective-c ++文件中.它在objective-C land中编译很好,但是swrve_currency_given()函数在我的WGController.mm文件中导致以下内容:
Undefined symbols for architecture armv7:
"swrve_currency_given(Swrve*, __CFString const*, double)", referenced from:
-[WGController giveTheUserSomeCashForPlayingThisLevel] in WGController.o
ld: symbol(s) not found for architecture armv7
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
我不完全确定这个错误是否与Obj-C与C++有关,但感觉就像它.我的理论是它可能认为它是Obj-C类的一个函数?'swrve'代码是第三方代码,一个.h和.c文件,我像这样导入:
#import "swrve.h"
任何帮助表示赞赏!谢谢
objective-c++ ×10
objective-c ×7
c++ ×4
cocoa ×1
ios ×1
iphone ×1
java ×1
nsstring ×1
stdstring ×1
xcode ×1