我有一个正在后台线程上执行的方法.从那个方法我试图dispatch_async在主线程上的块.该块使用本地C++对象,该对象应该根据Apple参考进行复制.我得到了一个分段错误,从跟踪中我看到一些非常粗略的事情正在发生.这是我的代码的简化版本.
struct A
{
A() { printf("0x%08x: A::A()\n", this); }
A(A const &that) { printf("0x%08x: A::A(A const &%p)\n", this, &that); }
~A() { printf("0x%08x: A::~A()\n", this); }
void p() const { printf("0x%08x: A::p()\n", this); }
};
- (void)runs_on_a_background_thread
{
A a;
a.p();
dispatch_async(dispatch_get_main_queue(), ^{
printf("block begins\n");
a.p();
printf("block ends\n");
});
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
0xbfffc2af: A::A()
0xbfffc2af: A::p()
0xbfffc2a8: A::A(A const &0xbfffc2af)
0x057ae6b4: A::A(A const &0xbfffc2a8)
0xbfffc2a8: A::~A()
0xbfffc2af: A::~A()
0xbfffdfcf: A::A(A const &0x57ae6b4)
0xbfffdfcf: A::~A()
block begins …Run Code Online (Sandbox Code Playgroud) 我在块ivar中得到了一些具有明显参考周期的代码.以下代码导致引用循环,并且永远不会调用dealloc:
__block MyViewController *blockSelf = self;
loggedInCallback = ^(BOOL success, NSError *error){
if (success)
{
double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void)
{
[blockSelf.delegate loginDidFinish];
});
}
};
Run Code Online (Sandbox Code Playgroud)
但是,如果我创建另一个__block变量来保存对我的委托的引用以捕获块的范围,则引用周期消失:
__block id <MyViewControllerDelegate> blockDelegate = self.delegate;
loggedInCallback = ^(BOOL success, NSError *error){
if (success)
{
double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void)
{
[blockDelegate loginDidFinish];
});
}
};
Run Code Online (Sandbox Code Playgroud)
只是想了解这里发生了什么.
memory-management objective-c grand-central-dispatch objective-c-blocks automatic-ref-counting
我有一些代码,我一直用来获取当前的键盘布局,并将虚拟键码转换为字符串.这在大多数情况下效果很好,但我在某些特定情况下遇到了麻烦.引起这种情况的是德国QWERTZ键盘上退格键旁边的重音键.http://en.wikipedia.org/wiki/File:KB_Germany.svg
该密钥生成我期望的VK代码,kVK_ANSI_Equal但在使用QWERTZ键盘布局时,我没有得到任何描述.它最终成为死键,因为它应该由另一个键组成.有没有办法捕捉这些情况并进行适当的转换?
我目前的代码如下.
TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource();
CFDataRef uchr = (CFDataRef)TISGetInputSourceProperty(currentKeyboard, kTISPropertyUnicodeKeyLayoutData);
const UCKeyboardLayout *keyboardLayout = (const UCKeyboardLayout*)CFDataGetBytePtr(uchr);
if(keyboardLayout)
{
UInt32 deadKeyState = 0;
UniCharCount maxStringLength = 255;
UniCharCount actualStringLength = 0;
UniChar unicodeString[maxStringLength];
OSStatus status = UCKeyTranslate(keyboardLayout,
keyCode, kUCKeyActionDown, 0,
LMGetKbdType(), kUCKeyTranslateNoDeadKeysBit,
&deadKeyState,
maxStringLength,
&actualStringLength, unicodeString);
if(actualStringLength > 0 && status == noErr)
return [[NSString stringWithCharacters:unicodeString length:(NSInteger)actualStringLength] uppercaseString];
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在iPad应用程序中支持多个方向,而设计者提出的视图无法使用纯粹的弹簧/支柱模型进行设置.我已经在两个单独的nib文件中布局了视图,目前使用以下...
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration;
{
if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
{
[[NSBundle mainBundle] loadNibNamed:@"ConfigureViewController-Portrait" owner:self options:nil];
}
else
{
[[NSBundle mainBundle] loadNibNamed:@"ConfigureViewController" owner:self options:nil];
}
}
Run Code Online (Sandbox Code Playgroud)
这是有效的,但正如预期的那样,有点慢.有没有办法缓存这个结果?或者是否有某种方法可以完成我缺少的整个过程.只是拿着从它回来的顶级对象并没有什么帮助,因为我需要NIB知道如何连接所有东西.
嗨,我正在使用UITableView来显示列表.我已对其进行了自定义并在单元格中添加了4个UILabel,但是当选择该行时,行选择颜色(自定义为绿色)和标签文本颜色(也为绿色)保持不变.因此,很难看到细胞的文本.我想在选择行时将标签的颜色更改为白色,否则为绿色.
谁能帮我吗?
谢谢,
根据这个块编程教程:
以下嵌套块:
void (^(^myblockptr)(void (^)()))();
Run Code Online (Sandbox Code Playgroud)
是指向块返回块的块的指针
它说它相当于:
typedef void (^Block)();
Block (^myblockptr)(Block);
Run Code Online (Sandbox Code Playgroud)
我猜这(void (^)())对应于参数,即(Block).但是,我无法看到嵌套表达式的哪一部分对应于返回的块.
您是否能够在嵌套表达式中标识返回的块?
objective-c ×4
c++ ×1
cocoa ×1
cocoa-touch ×1
ios ×1
ipad ×1
iphone ×1
keyboard ×1
localization ×1
macos ×1
macos-carbon ×1
nib ×1
text ×1
uitableview ×1