如何在一个空白的参数对象传递? [英] How to pass in an object for a void parameter?

查看:174
本文介绍了如何在一个空白的参数对象传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MusicPlayer API工作,我试图解决我与用户的回调有问题。

我有被放置在一个MIDI的每一个音符事件用户事件。当音符播放,这些用户事件的说明整数值传递给用户回调函数为* inEventData:

 无效noteUserCallback(无效* inClientData,MusicSequence inSequence中,MusicTrack的InTrack,MusicTimeStamp inEventTime,常量MusicEventUserData * inEventData,MusicTimeStamp inStartSliceBeat,MusicTimeStamp inEndSliceBeat)
{
    UserEvent *事件=(UserEvent *)inEventData;
    UInt32的大小=事件 - >的长度;
    UInt32的注=事件 - > playedNote;
    UInt32的时间戳=事件 - > TSTAMP;
    的NSLog(@尺寸:鲁%注:%lu个,时间戳:%录,尺寸,注意,时间戳);    开关(注){
        案例60:
            [whiteKey60 setImage:highlightA];
            打破;        默认:
            打破;
    }
}

根据这些笔记,我想更改了用户界面。也就是说,我有一个数字,我希望能够与基于音符的值不同的图像更新的UIImageViews(见上文中的switch语句)。

用户回调函数,像这样的序列相关的:

  MusicSequenceSetUserCallback(顺序,noteUserCallback,NULL);

与虚空连接上述功能的第三个参数*用户回调函数的参数inClientData

我的问题是,当我尝试对用户的回调函数内访问我的观点的IBOutlets,我不能。为了解决这个问题,我试图视图控制器传递到像这样的MusicSequenceSetUserCallback功能:

  MusicSequenceSetUserCallback(顺序,noteUserCallback,个体经营);

我遇到的问题是,这的 inClientData参数类型为void 的,我不能在的ViewController *对象传递。我试图简单地创建一个桥梁,当我设置的回调函数,然后用户回调函数内再次缩小回来,但它似乎没有工作。

如果任何人有我怎么能做到这一点的任何想法。即使它是一个完全不同的方法,我真的AP preciate它。

感谢。

修改

我试过如下:

  MusicSequenceSetUserCallback(顺序,noteUserCallback,(__bridge_retained无效*)个体经营);

 无效noteUserCallback(无效* inClientData,MusicSequence inSequence中,MusicTrack的InTrack,MusicTimeStamp inEventTime,常量MusicEventUserData * inEventData,MusicTimeStamp inStartSliceBeat,MusicTimeStamp inEndSliceBeat)
{
    PracticeViewController * PVC =(__bridge_transfer PracticeViewController *)inClientData;    [pvc.whiteKey21 setImage:pvc.highlightA];    UserEvent *事件=(UserEvent *)inEventData;
    UInt32的大小=事件 - >的长度;
    UInt32的注=事件 - > playedNote;
    UInt32的时间戳=事件 - > TSTAMP;
    的NSLog(@尺寸:鲁%注:%lu个,时间戳:%录,尺寸,注意,时间戳);    开关(注){
        案例60:
            打破;        默认:
            打破;
    }
}

不幸的是,它给了我这个错误:螺纹13 AURemoteIO :: IOThread:EXC_BAD_ACCESS(code = 2,地址= 0×10)

修改

同时更改桥梁__bridge沉默的错误,但它仍然不能正常工作;将呼叫转接至whiteKey60图像不工作的改变。这是不符合的UIImageView连接的问题,因为在viewDidLoad中做同样的工作正常。

什么似乎反正事情,对我来说,是自我是不是做给函数或者其断开连接时,它的通过呢?


解决方案

再看看在声明MusicSequenceSetUserCallback:

  OSStatus MusicSequenceSetUserCallback(
    MusicSequence inSequence中,
    MusicSequenceUserCallback inCallback,
    无效* inClientData
);

inClientData 的类型不是无效 - 这将没有任何意义,在C变量可从来没有一个类型无效的。相反,它是无效* - 大概,一个指向不明的东西。

您需要将变量转换为无效* 的方式,并从无效* 上出路。假设 MyClass的* ,则:

  MusicSequenceSetUserCallback(顺序,noteUserCallback,(void *的)个体经营);

和回调:

 无效noteUserCallback(无效* inClientData,MusicSequence inSequence中,MusicTrack的InTrack,MusicTimeStamp inEventTime,常量MusicEventUserData * inEventData,MusicTimeStamp inStartSliceBeat,MusicTimeStamp inEndSliceBeat)
{
    MyClass的*自我=(MyClass的*)inClientData;
    ...
}

I am working with the MusicPlayer API and I am trying to solve a problem I am having with user callback.

I have user events that are placed at every note event of a MIDI. When the notes are played, these user events pass the integer value of the note to a user callback function as *inEventData:

void noteUserCallback (void *inClientData, MusicSequence inSequence, MusicTrack inTrack, MusicTimeStamp inEventTime, const MusicEventUserData *inEventData, MusicTimeStamp inStartSliceBeat, MusicTimeStamp inEndSliceBeat)
{
    UserEvent* event = (UserEvent *)inEventData;
    UInt32 size = event->length;
    UInt32 note = event->playedNote;
    UInt32 timestamp = event->tStamp;
    NSLog(@"Size: %lu Note: %lu, Timestamp: %lu", size, note, timestamp);

    switch (note) {
        case 60:
            [whiteKey60 setImage:highlightA];
            break;

        default:
            break;
    }
}

Based upon these notes, I would like to makes changes to the UI. Namely, I have a number of UIImageViews that I would like to be able to update with different images based upon the value of the note (see above in switch statement).

The user callback function is associated with the sequence like this:

MusicSequenceSetUserCallback(sequence, noteUserCallback, NULL);

The third parameter of the above function connected with the void *inClientData parameter of the user callback function.

My problem is, that when I try to access the IBOutlets of my view from within the user callback function, I cannot. To fix this, I tried to pass the view controller into the MusicSequenceSetUserCallback function like this:

MusicSequenceSetUserCallback(sequence, noteUserCallback, self);

The problem I am having is that this inClientData parameter is of type void and I cannot pass in a ViewController* object. I tried simply creating a bridge when I set the callback function and then bridging back again within the user callback function but it didn't seem to work.

If anyone has any thoughts on how I could accomplish this. Even if it's a completely different methods, I would really appreciate it.

Thanks.

EDIT

I tried the following:

MusicSequenceSetUserCallback(sequence, noteUserCallback, (__bridge_retained void *)self);

and

void noteUserCallback (void *inClientData, MusicSequence inSequence, MusicTrack inTrack, MusicTimeStamp inEventTime, const MusicEventUserData *inEventData, MusicTimeStamp inStartSliceBeat, MusicTimeStamp inEndSliceBeat)
{   
    PracticeViewController* pvc = (__bridge_transfer PracticeViewController *)inClientData;

    [pvc.whiteKey21 setImage:pvc.highlightA];

    UserEvent* event = (UserEvent *)inEventData;
    UInt32 size = event->length;
    UInt32 note = event->playedNote;
    UInt32 timestamp = event->tStamp;
    NSLog(@"Size: %lu Note: %lu, Timestamp: %lu", size, note, timestamp);

    switch (note) {
        case 60:
            break;

        default:
            break;
    }
}

Unfortunately, it gives me this error: Thread 13 AURemoteIO::IOThread: EXC_BAD_ACCESS (code=2, address=0x10)

EDIT

Changing both bridges to __bridge silences the error but it is still not working properly; the call to change in the image in whiteKey60 does not work. It isn't a problem with the UIImageView connection because doing the same in viewDidLoad works fine.

What seems to be going on, to me anyways, is that self isn't making it to the function or perhaps its breaking the connection when it's passed?

解决方案

Take a look at the declaration for MusicSequenceSetUserCallback again:

OSStatus MusicSequenceSetUserCallback (
    MusicSequence              inSequence,
    MusicSequenceUserCallback  inCallback,
    void                       *inClientData
);

inClientData's type isn't void -- that wouldn't make sense, variables in C can never have a type of void. Rather, it's void * -- roughly, a pointer to something unspecified.

You need to cast the variable to a void * on the way in, and from a void * on the way out. Assuming that self is a MyClass *, then:

MusicSequenceSetUserCallback(sequence, noteUserCallback, (void *)self);

and in the callback:

void noteUserCallback (void *inClientData, MusicSequence inSequence, MusicTrack inTrack, MusicTimeStamp inEventTime, const MusicEventUserData *inEventData, MusicTimeStamp inStartSliceBeat, MusicTimeStamp inEndSliceBeat)
{
    MyClass* self = (MyClass *)inClientData;
    ...
}

这篇关于如何在一个空白的参数对象传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆