小编Joh*_*ett的帖子

从imp调用Objective-C方法

我试图在运行时获取一个方法,然后使用其数据结构来调用它的实现.只是为了澄清,这是出于学习目的,而不是出于任何实际原因.所以这是我的代码.

#import <Foundation/Foundation.h>
#import <stdio.h>

#import <objc/runtime.h>

@interface Test : NSObject 
-(void)method;
@end

@implementation Test

-(void)method {
    puts("this is a method");
}

@end 

int main(int argc, char *argv[]) {

    struct objc_method *t = (struct objc_method*) class_getInstanceMethod([Test class], @selector(method));
    Test *ztest = [Test new];

    (t->method_imp)(ztest, t->method_name);

    [ztest release];
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

定义struct objc_method如下(在objc/runtime.h中定义)

typedef struct objc_method *Method;

....

struct objc_method {
    SEL method_name                                          OBJC2_UNAVAILABLE;
    char *method_types                                       OBJC2_UNAVAILABLE;
    IMP method_imp                                           OBJC2_UNAVAILABLE;
}                                                            OBJC2_UNAVAILABLE;
Run Code Online (Sandbox Code Playgroud)

但是当我尝试编译我的代码时,我得到了这个错误.

error: dereferencing pointer to incomplete type …

methods objective-c objective-c-runtime data-structures

4
推荐指数
2
解决办法
3423
查看次数

线程中的CADisplayLink

我正在制作游戏,我想在不同的线程上运行游戏逻辑代码和渲染代码,以期提高性能.但是对于我的生活,我无法弄清楚如何CADisplayLink在一个衍生的线程中运行.现在我正在使用NSThread多线程.我已经读到了NSOperationQue,但我并不是那么理解.有人能指出我正确的方向吗?这是我现在使用的代码,-logicLoop并且-animationLoop都在不同的线程上运行,因此我认为它们将获得单独的运行循环,并且CADisplayLinks将位于不同的线程上.

-(void)logicLoop {
    NSLog(@"adding");
    logicTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateModel)];
    logicTimer.frameInterval = kFrameInterval;
    [logicTimer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

-(void)animationLoop {
    NSLog(@"animation");
    animationTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(renderScene)];
    animationTimer.frameInterval = kFrameInterval;
    [animationTimer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

我应该使用NSOperationQue这个吗?如果是这样,我该怎么办呢?我真正需要的是一种有效的方法来在不同的线程上运行游戏循环和动画循环,这样它们就不会相互减慢速度,游戏玩法会显得更加平滑.

谢谢!

iphone multithreading objective-c

3
推荐指数
1
解决办法
3071
查看次数

我要加一个$后?在vi的所有行中.我怎么能用正则表达式做到这一点?

我想在任何后加一个$?在vi的所有行中.我怎么能用正则表达式做到这一点?

regex vim

0
推荐指数
1
解决办法
83
查看次数