gpt4 book ai didi

ios - objc_getAssociatedObject 返回 null

转载 作者:行者123 更新时间:2023-11-28 19:46:56 32 4
gpt4 key购买 nike

我正在尝试使用 objc_setAssociatedObject 传递对象

我正在尝试使用以下代码传递 Evento:

Evento *evento = (Evento*)[secAtt objectAtIndex:indexPath.row]; 
objc_setAssociatedObject(evento, @"evento", self, OBJC_ASSOCIATION_RETAIN);

稍后我尝试使用以下代码获取此 evento:

Evento *evento = objc_getAssociatedObject(self, "evento");

它返回nil

编辑:更多代码:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

NSString *sectionTitle = [sectionsTitles objectAtIndex:indexPath.section];
NSArray *secAtt = [attractions objectForKey:sectionTitle];

Evento *evento = (Evento*)[secAtt objectAtIndex:indexPath.row];
objc_setAssociatedObject(self, @"evento", evento, OBJC_ASSOCIATION_RETAIN);

NSString *tipoEntretenimento = evento.entretenimento.tipo.descricao;
NSLog(@"tipo %@", tipoEntretenimento);
UIImageView *imgCategoria = (UIImageView *)[cell.contentView viewWithTag:30];
//[imgCategoria setContentMode:UIViewContentModeScaleAspectFit];
if([tipoEntretenimento isEqualToString:@"Bares"])
[imgCategoria setImage:[UIImage imageNamed:@"bares.png"]];
else if ([tipoEntretenimento isEqualToString:@"Crianca"])
[imgCategoria setImage:[UIImage imageNamed:@"crianca.png"]];
else if ([tipoEntretenimento isEqualToString:@"Guloseimas"])
[imgCategoria setImage:[UIImage imageNamed:@"guloseimas.png"]];
else if ([tipoEntretenimento isEqualToString:@"Restaurantes"])
[imgCategoria setImage:[UIImage imageNamed:@"restaurantes.png"]];
else if ([tipoEntretenimento isEqualToString:@"Teatro"])
[imgCategoria setImage:[UIImage imageNamed:@"teatro.png"]];
else if ([tipoEntretenimento isEqualToString:@"Shows"])
[imgCategoria setImage:[UIImage imageNamed:@"shows"]];
else if ([tipoEntretenimento isEqualToString:@"Dança"])
[imgCategoria setImage:[UIImage imageNamed:@"danca.png"]];
else if ([tipoEntretenimento isEqualToString:@"Concertos"])
[imgCategoria setImage:[UIImage imageNamed:@"concerto.png"]];
else if ([tipoEntretenimento isEqualToString:@"Cinema"])
[imgCategoria setImage:[UIImage imageNamed:@"cinema.png"]];


UIFont *roboto = [UIFont fontWithName:@"Roboto-Bold" size:16];
UILabel *lblNomeEvento = (UILabel *)[cell.contentView viewWithTag:10];
lblNomeEvento.text = evento.entretenimento.nome;
lblNomeEvento.font = roboto;

Agenda *agenda = nil;
if([evento.listaAgendas count] > 0)
agenda = (Agenda *)[evento.listaAgendas objectAtIndex:0];

NSLog(@"agenda %@", agenda.description);

UIImageView *imgevento = (UIImageView *)[cell.contentView viewWithTag:20];
[imgevento setContentMode:UIViewContentModeScaleAspectFit];
[imgevento setImage:[UIImage imageNamed:@"pin.png"]];

UIButton *btnCompartilhar = (UIButton *)[cell.contentView viewWithTag:40];
[btnCompartilhar.titleLabel setHidden:YES];
btnCompartilhar.titleLabel.text = evento.entretenimento.nome;
[btnCompartilhar addTarget:self action:@selector(compartilharClick:) forControlEvents:UIControlEventTouchDown];


UIButton *btnFavoritar = (UIButton *)[cell.contentView viewWithTag:50];
[btnFavoritar addTarget:self action:@selector(favoritarClick:) forControlEvents:UIControlEventTouchDown];
return cell;
}

然后当我尝试获取对象时:

-(void) compartilharClick:(id)sender {
Evento *evento = (Evento *)objc_getAssociatedObject(self, "evento");

UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:@"Compartilhar:" delegate:self cancelButtonTitle:@"Cancelar" destructiveButtonTitle:nil otherButtonTitles:
@"Facebook",
@"Google+",
@"Twitter",
@"Outros",
nil];
popup.tag = 1;
[popup showInView:[UIApplication sharedApplication].keyWindow];
}

最佳答案

这里的问题可能是因为当函数需要 const void *key

时,您正在使用 NSString 作为键

OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)

尝试在您的实现之上添加 static char *EventToKey = "EventToKey";

然后将 objc_getAssociatedObjectobjc_setAssociatedObject 的用法更改为

objc_setAssociatedObject(self, EventToKey, evento, OBJC_ASSOCIATION_RETAIN);

objc_getAssociatedObject(self, EventToKey);

关于ios - objc_getAssociatedObject 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31594463/

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