访问 NSMutableArray 中的对象的语法

Syntax to access objects in my NSMutableArray

提问人:KiloOne 提问时间:5/11/2022 最后编辑:KiloOne 更新时间:5/11/2022 访问量:40

问:

我在 NSMutableArray 中有 2 个对象,一个是 CBPeripheral 对象,另一个是 NSString。

在这里,您可以看到 NSMutableArray 包含两个对象:enter image description here

然后,在第 1249 行中,我尝试将“periph”对象分配给 CBPeripheral,在第 1252 行中,我尝试将“ADVTname”分配给文本标签。

第 1249 行和第 1252 行都不是我尝试执行的操作的正确语法(第 1252 行甚至无法编译)。

有人可以告诉我如何访问我的“thisrow”NSMutableArray 中的对象吗?

谢谢 山谷

编辑#1:我试图理解“thisrow”如何成为NSDictionay,但我被难住了,这里是将对象放入“thisrow”的所有代码片段。也许我的整个想法存在一个缺陷,即试图记住添加到_foundbleHalos的所有外围设备的 ADVTname。

//AFTER @interface foundblehalos was defined
@property (strong,nonatomic) NSMutableArray* foundbleHalos;

//in (void)centralManager > didDiscoverPeripheral
[_foundbleHalos addObject:@{@"periph": peripheral, @"ADVTname": ADVRTperipheralName}]; //2022-b70-3


//when I try to populate a TableView with all the ADVTnames we found:
NSMutableArray *thisrow = [_foundbleHalos objectAtIndex:indexPath.row];

blecell.textLabel.text = [NSString stringWithFormat:@"%@",thisrow.lastObject];

CBPeripheral* HaloForRow = (CBPeripheral*) thisrow[0];

编辑#2:好吧,我认为以下代码是我的主题问题的答案,但我不明白它,因为我不知道为什么当我最初将其定义为NSMutableArray时,“thisrow”变成了字典类型:

//AFTER @interface foundblehalos was defined
@property (strong,nonatomic) NSMutableArray* foundbleHalos;

//in (void)centralManager > didDiscoverPeripheral
[_foundbleHalos addObject:@{@"periph": peripheral, @"ADVTname": ADVRTperipheralName}]; //2022-b70-3


//when I try to populate a TableView with all the ADVTnames we found:
NSDictionary *thisrow = [_foundbleHalos objectAtIndex:indexPath.row];

blecell.textLabel.text = thisrow[@"ADVTname"];

CBPeripheral* HaloForRow = (CBPeripheral*) thisrow[@"periph"];
iOS Objective-C Xcode nsmutableArray

评论

3赞 Paulw11 5/11/2022
thisrow不是数组。这是一本字典
0赞 KiloOne 5/11/2022
但我在第 1243 行将其定义为 NSMutableArray,它是如何成为字典的?
0赞 Larme 5/11/2022
您将其声明为 ,但实际上它是一个 NSDictionary。这就像在橙子上贴上标有“香蕉 1 美元”的标签价格。它仍然是一个橙子......NSMutableArray
0赞 Larme 5/11/2022
你清楚地写着:,所以这是一本字典。[_foundbleHalos addObject:aDictionary]
0赞 KiloOne 5/11/2022
啊哈,我没有意识到通过使用我为添加对象所做的语法_foundbleHalos创建了一个“aDictionary”,谢谢我想我现在很高兴,因为我的 EDIT #2 代码似乎让我可以在表视图填充时在适当的_foundbleHalos索引处访问“periph”和“ADVTname”。

答: 暂无答案