我遇到的唯一功能差异是我可以取消预定的消息performSelector:withObject:afterDelay:.我不知道取消提交的块的方法dispatch_after.(请告诉我是否有办法做到这一点,我不知道).
我想了解更多:
请考虑以下代码:
// t included so block1 is a stack block. See [1] below
int t = 1;
SimpleBlock block1 = ^{ NSLog(@"block1, %d", t); };
// copy block1 to the heap
SimpleBlock block1_copied = [block1 copy];
// block2 is allocated on the stack, and refers to
// block1 on the stack and block1_copied on the heap
SimpleBlock block2 = ^{
NSLog(@"block2");
block1_copied();
block1();
};
[block1_copied release];
// When the next line of code is executed, block2_copied is
// allocated at …Run Code Online (Sandbox Code Playgroud) 我已经提交了雷达(rdar:// 12311693,http://openradar.appspot.com/12311693)以下问题,但我想我会在这里发布,以及看看是否有人能发现在一个错误我的代码可能会导致崩溃.
以下代码示例由于在打开编译器优化(-Os)时构建过度而导致崩溃,但在关闭编译器优化(-O0)时不会崩溃.该项目正在使用Xcode 4.4.1(4F1003),Apple LLVM编译器4.0构建
当num2过度释放时,应用程序崩溃.启用Zombie Objects以确认是这种情况.
// This crashes under -Os, but not under -O0
NSNumber *num1 = @((float)arc4random() / (float)UINT32_MAX);
NSNumber *num2 = @((float)arc4random() / (float)UINT32_MAX);
NSNumber *foo1 = num1;
NSNumber *foo2 = num2;
for (NSUInteger i=0; i<2; i++) {
NSLog(@"foo1: %p %@", foo1, foo1);
NSLog(@"foo2: %p %@", foo2, foo2);
// swap foo1 and foo2
foo1 = num2;
foo2 = num1;
}
Run Code Online (Sandbox Code Playgroud) 请考虑以下内容:Objective-C类的实例由一个强引用和一个弱引用(在ARC下)引用.在线程X上,通过弱引用在实例上调用方法.在线程Y上,强引用被破坏,以便不再有对该实例的强引用,并且应该取消分配它.
这种情况是否可能,因为当方法在线程X上执行时,对象可能在线程Y上被释放?类似地,在方法返回之前,是否调用对象上的方法"保留"该对象?
假定即使在方法调用正在进行(链接)*时也可以释放对象,对象是否可以安全地注册和接收将在与其预期的线程不同的线程上传递的通知.释放?
作为参考,文档说明了这一点
在多线程应用程序中,通知始终在发布通知的线程中传递,这可能与观察者注册自己的线程不同.
同样重要的是,NSNotificationCenter不会对注册接收通知的对象保持强引用.
这是一个可能使情况更具体的例子:
- (id)init {
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:SomeNotification object:nil];
}
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)handleNotification:(NSNotification *)notification {
// do something
}
Run Code Online (Sandbox Code Playgroud)
具有此实现的对象在线程X上接收SomeNotification.在-handleNotification:return之前,对该对象的最后一个强引用(在我可以看到的代码中)被破坏.
我是否正确地认为:
一个.如果NSNotificationCenter在调用-handleNotification之前对该对象进行强引用,那么在-handleNotification:return之后,该对象将不会被释放,并且
湾 如果NSNotificationCenter在调用-handleNotification之前没有对该对象进行强引用:那么该对象可能会在-handleNotification之前被释放:返回
哪种方式(a或b)有效?我还没有在文档中找到这个主题,但在多线程环境中安全使用NSNotificationCenter似乎有些重要.
更新:上述链接中的答案已更新,表明"ARC保留并释放弱引用的调用".这意味着在方法调用过程中不应释放对象.
任何人都可以解释以下现象吗?
由于iPhone设备3.1 SDK中,我发现,如果UITableViewCell是一个风格UITableViewCellStyleValue1和它detailTextLabel.text是未分配的,则textLabel不会在小区的中心显示正如所预料的.
一个值得注意的警告是,当我在设备上测试时,这只会发生在我身上- iPhone 模拟器 3.1 SDK会正确显示单元格.此外,使用iPhone Device 3.0 SDK时也不会出现问题.
下面是一个简单的UITableViewController子类实现,它演示了这个问题.
@implementation BuggyTableViewController
#pragma mark Table view methods
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
var num = 0.03829585
let amountNumber = NSNumber(double: num)
let nFormatter = NSNumberFormatter()
nFormatter.numberStyle = .DecimalStyle
nFormatter.roundingMode = .RoundHalfUp
nFormatter.maximumFractionDigits = 8
var numStr = nFormatter.stringFromNumber(amountNumber)!
Run Code Online (Sandbox Code Playgroud)
预期结果numStr为"0.03829585".这就是我用我的测试设备得到的,但是在我的少量TestFlight用户的设备上,我得到了"0,03829585".这是为什么?如何解决这个问题,以便我总是得到正确的字符串表示?