iPhone WiTap 示例上的 Bonjour 网络帮助
我可以遵循大多数 Apple 的 WiTap 示例,但我对 send 方法中的这一点感到有点困惑:
- (void) send:(const uint8_t)message
{
if (_outStream && [_outStream hasSpaceAvailable])
if([_outStream write:(const uint8_t *)&message maxLength:sizeof(const uint8_t)] == -1)
[self _showAlert:@"Failed sending data to peer"];
}
- (void) activateView:(TapView*)view
{
NSLog(@"ACTIVATE TAG: %d", [view tag]);
//[self send:[view tag] | 0x80];
[self send:[view tag]];
}
- (void) deactivateView:(TapView*)view
{
NSLog(@"DEACTIVATE TAG: %d", [view tag]);
//[self send:[view tag] & 0x7f];
[self send:[view tag]];
}
请注意,我已将 send: 参数更改为视图的标签,编号为 1-9。 最初,代码进行了按位“与”和“或”调整。
为什么?
我知道发送方法需要一个 uint8_t ,但这就是为什么存在按位的东西吗? 将 NSInteger 转换为 unint8_t?
该代码不适用于我上面的更改。 它将很好地记录并且在视觉上客户端将正常工作,但消息没有在客户端之间正确发送/接收。
有人能用简短的语言解释一下按位的东西在做什么吗? 或者我是对的?
谢谢! 这是我的第一个问题,所以请友善。
感谢您的回复。 我还是有点纳闷。 得到它?
基本上,为什么?
这只是一种传递标识符的极客方式吗? 每个视图都有一个标签#,为什么不直接传递它,并从视图类切换状态(向上/向下)?
这只是“编写它的人是如何做到这一点的”的情况,还是我错过了拼图的关键部分,因为这也是我应该如何构建我的代码。
我只想传递一个标签 # ,然后让该标签决定在清晰可读的函数中做什么,例如 toggleUpOrDownState
或其他东西。
我想,这种按位的东西总是让我觉得很愚蠢,除非有必要,等等。然后我觉得很愚蠢,但无论如何还是设法蒙混过关。 :)
I can follow most of Apple's WiTap sample, but am sort of stumped on this bit in the send method:
- (void) send:(const uint8_t)message
{
if (_outStream && [_outStream hasSpaceAvailable])
if([_outStream write:(const uint8_t *)&message maxLength:sizeof(const uint8_t)] == -1)
[self _showAlert:@"Failed sending data to peer"];
}
- (void) activateView:(TapView*)view
{
NSLog(@"ACTIVATE TAG: %d", [view tag]);
//[self send:[view tag] | 0x80];
[self send:[view tag]];
}
- (void) deactivateView:(TapView*)view
{
NSLog(@"DEACTIVATE TAG: %d", [view tag]);
//[self send:[view tag] & 0x7f];
[self send:[view tag]];
}
Note that I have changed the send: argument to just the tag of the views, which are numbered 1-9. Originally the code had the bitwise AND and OR adjustments.
WHY?
I get the fact that the send method needs a uint8_t
, but is that why the bitwise stuff is there? To turn a NSInteger into a unint8_t?
The code doesn't work with my changes above. It will log fine and visually the client will function correctly, but the messages aren't being sent/received correctly from client to client.
Can someone explain in small words what the bitwise stuff is doing? Or am I correct?
Thanks! This is my first question to SO so please be kind.
thanks for the response. I am still puzzled a bit. Get it?
Basically, why?
Is this just a geeky way of passing an identifier? Each of those views have a tag #, why not just pass that, and toggle the state (up/down) from the view class?
Is this just a case of "this is how the person who wrote it did it", or am I missing a crucial piece of the puzzle in that this is how I should also be structuring my code.
I would just want to pass a tag # and then have that tag decide what to do in a clearly readable function like toggleUpOrDownState
or something.
This bitwise stuff always makes me feel stupid I guess, unless it is necessary, etc. Then I feel stupid but manage to muddle through somehow anyway. : )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基本上,
[view tag] | 0x80
正在设置该值的高位(因此 00010011 将变为 10010011),并且[view tag] & 0x7f
正在删除它(10010011 -> 00010011)。看一下
[AppController stream:handleEvent:]
方法。 您将看到以下代码:因此,流的接收器正在检查该高位。
Basically,
[view tag] | 0x80
is setting the high bit in that value (so 00010011 would become 10010011) and[view tag] & 0x7f
is removing it (10010011 -> 00010011).Take a look at the
[AppController stream:handleEvent:]
method. You'll see this code:So, the receiver of the stream is checking for that high bit.
我相信原因是高位是从一个对等点传递到另一个对等点的另一部分数据。 它指定该消息是针对点击的“开始”还是针对点击的“结束”。
根据该位,接收器激活或停用点击视图。
因此,本质上,他们将两条信息放入单个无符号整数中 - 哪个方块被点击(低位),以及触摸开始或结束(高位)。
I believe the reason why is that the high-order bit is another piece of the data passed from one peer to the other. It specifies whether the message is for the "start" of the tap or the "end" of the tap.
Based on that bit, the receiver either activates the tap view or deactivates it.
So in essence, they are putting two pieces of information in that single unsigned integer - which square was tapped (the low bits), and were the touches beginning or ending (the high bit).
我认为消息可能会被丢弃,或者以不同的顺序到达。 因此,如果您不说明是向下点击还是向上点击,则切换可能会被取消。
I think it's possible for messages to get dropped, or arrive in a different order. So if you don't say if it's a tap down or tap up, it's possible that toggling would get thrown off.