【问题标题】:Cocoa threads and class declaration connectionCocoa 线程和类声明连接
【发布时间】:2014-09-29 07:29:58
【问题描述】:

我无法在任何 stackoverflow 主题中找到答案。我遇到过线程和类,我想在全局 *.m 文件中使用类控制器变量和操作。没有附加的第二类。 这是代码:

Controller.h

#import <Cocoa/Cocoa.h>

@interface Controller : NSObject
{
IBOutlet NSWindow *Main; 
IBOutlet NSButton *myButton;
}
- (void)awakeFromNib;
- (IBAction)action:(id)sender;
- (IBAction)Display(id)sender;

@end

Controller.m

#import "Controller.h"

@implementation Controller

- (void)awakeFromNib
{

    //At this point i can reach any variable from Controller.h

}

- (IBAction)Display:(id)sender

{

    //Same, i can reach any variable from Controller.h 

}

//关闭“}”并从新行开始后,我不能从Controller.h调用[Display]或其他任何东西,我的线程代码在这里,我想编写线程来调用Display,或者使用awakeFromNib 变量、字符串、动作。

线程代码

#include assert.h
#include pthread.h

void* PosixThreadMainRoutine(void* data)
{

    // I want to call here, example [Display click:self];
    // but i only see [Controller]... 
    int ac = 0;
    while (ac < 8)
    {
        sleep(1);
        printf("Test");
        ac++;
    }
    return NULL;
}
void LaunchThread()
{

    // Create the thread using POSIX routines.
    pthread_attr_t  attr;
    pthread_t       posixThreadID;
    int             returnVal;

    returnVal = pthread_attr_init(&attr);
    assert(!returnVal);
    returnVal = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    assert(!returnVal);

    int     threadError = pthread_create(&posixThreadID, &attr, &PosixThreadMainRoutine, NULL);

    returnVal = pthread_attr_destroy(&attr);
    assert(!returnVal);
    if (threadError != 0)
    {
        // Report an error.
    }
}

@end

线程代码 100% 工作,但我无法在 PosixThreadMainRoutine 中调用任何变量或函数,有人可以解释一下如何做吗? `

【问题讨论】:

    标签: multithreading cocoa class xcode5 void


    【解决方案1】:

    如果你想在单独的线程中执行代码并访问@interface 方法/变量,最好使用:

    [NSThread detachNewThreadSelector:@selector(Display:) toTarget:self withObject:t];

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_apply(10, queue, ^(size_t i) { ///Thread code here });

    在您的示例中不清楚 - 什么是 [Display click:self];正如我所看到的 Display - 它的方法而不是接口。 PosixThreadMainRoutine() 也不是实例或类方法。这就是为什么你不能调用 call 方法的原因。一般来说,您无权访问,因为编译器不知道名称为 Dispaly 的接口,也不知道 click: 方法的声明。

    我建议你下一篇文章:From C++ to Objective-C

    【讨论】:

    • 我仍然面临同样的问题,以显示为例,我想从线程执行 [self Display:(id)sender]; ,抱歉我忘了在controller.h 行添加:- (IBAction)Display:(id)sender;
    • 尝试在 pthread_create 的最后一个参数中传递 self,然后在 PosixThreadMainRoutine(void* data) 中,您可以使用 [(Controller*)data Display: nil] 调用方法。但它不是“可可风格”的开发。
    • 无法传递 self,我得到未声明的标识符“self”,并且您的行 [(Controller*)data Display: nil] 不执行任何操作。您能否解释在 pthread_create 中插入或修改的位置
    • 这是不可能的,有很多语法错误和很多未定义变量要处理,我知道有间接调用方法,我可以使用 [(Controller*)data Display: nil]; ,但它没有执行,我尝试了 SEL 方法 = @selector(Display); [控制器 performSelector:method];,但我得到语法错误
    【解决方案2】:

    Controller.m

    I deleted thread, and i used this example:
     [NSThread detachNewThreadSelector:@selector(Display:) toTarget:self withObject:nil]; 
    
    
    - (IBAction)action:(id)sender{
    [NSThread detachNewThreadSelector:@selector(Display:) toTarget:self withObject:nil]; 
    }
    

    它就像魅力一样!我会保存代码,如果有人知道答案,我会在当前情况下尝试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-14
      • 2017-05-31
      • 2011-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 2013-04-16
      相关资源
      最近更新 更多