gpt4 book ai didi

ios - objc_setAssociatedObject 函数错误在 64 位模式下不在 32 位

转载 作者:可可西里 更新时间:2023-11-01 03:26:40 27 4
gpt4 key购买 nike

我在我的项目中使用了一个名为 SKSTableView 的简洁表格 View Controller ,它允许每个表格行都有许多子行。此代码在 32 位 模式下完美运行,但当我在我的 iPhone 5S 或模拟器中以 4 英寸 64 位模式运行它时,当您点击一行以获取子行时,它会崩溃。我对 64 位和 32 位 iOS 系统的区别一无所知。我很想了解这里发生的事情。

您会注意到 *SubRowObjectKey 设置为 void - 我得到的错误是:EXC_BAD_ACCESS_(代码=EXC_I386_GPFLT)
这是一个试图访问不存在的东西的一般保护错误(?)

当它崩溃时,Xcode 会突出显示这行代码:
objc_setAssociatedObject(self, SubRowObjectKey, subRowObj, OBJC_ASSOCIATION_ASSIGN);



<p>Also there is this:
Printing description of *(subRowObj):
(id) [0] =
Printing description of SubRowObjectKey:
</p>

<p>It seems to be working great in 32bit mode but some how in 64bit it seems to loose where it is. Here below is the section of the code I am looking at.</p>

#pragma mark - NSIndexPath (SKSTableView)
static void *SubRowObjectKey;
@implementation NSIndexPath (SKSTableView)
@dynamic subRow;
- (NSInteger)subRow
{
id subRowObj = objc_getAssociatedObject(self, SubRowObjectKey);
return [subRowObj integerValue];
}
- (void)setSubRow:(NSInteger)subRow
{
if (IsEmpty(@(subRow))) {
return;
}
id subRowObj = @(subRow);
objc_setAssociatedObject(self, SubRowObjectKey, subRowObj, OBJC_ASSOCIATION_ASSIGN);
}
+ (NSIndexPath *)indexPathForSubRow:(NSInteger)subrow inRow:(NSInteger)row inSection:(NSInteger)section
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
indexPath.subRow = subrow;
return indexPath;
}

您可以在此处下载并使用代码:https://github.com/sakkaras/SKSTableView

最佳答案

objc_setAssociatedObject() 似乎不适用于“标记指针”。标记指针是特殊指针,它们不指向某物,而是“值”存储在指针本身的(某些位)中。参见示例 Tagged pointers in Objective-C ,其中包含指向更多信息的链接。

标记指针仅用于 64 位代码,例如对于索引路径小行号和小节号,或小号对象。

很容易验证在标记指针上设置关联对象会因 EXC_BAD_ACCESS 而崩溃在 iOS 7/64 位上:

static void *key = &key;

id obj = [NSIndexPath indexPathForRow:1 inSection:1];
objc_setAssociatedObject(obj, key, @"345", OBJC_ASSOCIATION_RETAIN_NONATOMIC);
// --> EXC_BAD_ACCESS_(code=EXC_I386_GPFLT)

同样的事情发生在

id obj = @1;
id obj = [NSDate dateWithTimeIntervalSinceReferenceDate:0];

在 64 位模式下也是标记指针。

我找不到有关此限制的任何文档,我认为这是一个iOS 中的错误。此处报告了类似的问题:https://github.com/EmbeddedSources/iAsync/issues/22 .我认为这值得一个错误向 Apple 报告。

(备注:OS X/64位不存在此问题。)


更新(可能的解决方案/解决方法):“SKSTableView”项目(问题末尾的链接)使用类别NSIndexPath 和关联对象将第三个属性“子行”添加到索引路径。此子行临时设置在

tableView:cellForRowAtIndexPath:

SKSTableView 将当前子行传递给

tableView:cellForSubRowAtIndexPath:indexPath

ViewController 类中的委托(delegate)方法。

如果您改用“正确的”三级索引路径,则不需要关联对象,示例应用程序也在 64 位模式下运行。

我对示例项目进行了以下更改:

SKSTableView.m中:

+ (NSIndexPath *)indexPathForSubRow:(NSInteger)subrow inRow:(NSInteger)row inSection:(NSInteger)section
{
// Change:
//NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
//indexPath.subRow = subrow;
// To:
NSUInteger indexes[] = { section, row, subrow };
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexes length:3];

return indexPath;
}

属性访问器方法

- (NSInteger)subRow;
- (void)setSubRow:(NSInteger)subRow;

不再需要了。

ViewController.m中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForSubRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"UITableViewCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

// Change:
//cell.textLabel.text = [NSString stringWithFormat:@"%@", self.contents[indexPath.section][indexPath.row][indexPath.subRow]];
// To:
cell.textLabel.text = [NSString stringWithFormat:@"%@", self.contents[[indexPath indexAtPosition:0]][[indexPath indexAtPosition:1]][[indexPath indexAtPosition:2]]];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}

关于ios - objc_setAssociatedObject 函数错误在 64 位模式下不在 32 位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21561211/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com