ios 如何#import <NSObjCRuntime.h> 使用 objc_msgSend

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4896510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:47:50  来源:igfitidea点击:

How to #import <NSObjCRuntime.h> to use objc_msgSend

objective-cios

提问by Piotr Czapla

I'd like to import runtime's header to use objc_msgSend but I'm getting:

我想导入运行时的标头以使用 objc_msgSend 但我得到:

error: NSObjCRuntime.h: No such file or directory

Should I add something to the header search path?

我应该在标题搜索路径中添加一些东西吗?

回答by u596219

You'll need to include <objc/message.h>(you'll find the related headers in /usr/include/objc) and link to the objc(/usr/lib/libobjc.dylib) library.

您需要包含<objc/message.h>(您将在 中找到相关标题/usr/include/objc)并链接到objc( /usr/lib/libobjc.dylib) 库。

回答by Maxim Kholyavkin

#import <Foundation/NSObjCRuntime.h>does work

#import <Foundation/NSObjCRuntime.h>确实有效

but you probably need

但你可能需要

#import <objc/runtime.h>

like this Apple example does

像这个苹果的例子一样

upd: since iOS 7 #import <Foundation/NSObjCRuntime.h>replaced to #import <objc/NSObjCRuntime.h>but i recommend to use #import <objc/runtime.h>anyway

UPD:由于iOS的7#import <Foundation/NSObjCRuntime.h>替换成#import <objc/NSObjCRuntime.h>,但我建议使用#import <objc/runtime.h>反正

回答by DawnSong

When using Xcode 6 and later, you will get an error after #include<objc/message.h>. It can be resolved like this

使用 Xcode 6 及更高版本时,您将在#include<objc/message.h>. 可以这样解决

#include <objc/message.h>
void foo(void *object) {
  typedef void (*send_type)(id, SEL, int);
  send_type func = (send_type)objc_msgSend;
  func(object, sel_getUid("foo:"), 5);
}

http://devstreaming.apple.com/videos/wwdc/2014/417xx2zsyyp8zcs/417/417_whats_new_in_llvm.pdf

http://devstreaming.apple.com/videos/wwdc/2014/417xx2zsyyp8zcs/417/417_whats_new_in_llvm.pdf