【问题标题】:Objective-C: Adding objects to NSArray within a for loopObjective-C:在 for 循环中将对象添加到 NSArray
【发布时间】:2012-10-01 09:50:20
【问题描述】:

我正在尝试遍历一个数组(比较 Addr),并找到匹配的字符串(currentAddr)并尝试仅将匹配的字符串放入另一个数组,但我不断收到错误

“NSArray 没有可见的@Interface 声明选择器 addObject”

NSArray *matchedAddr;
//NOTE: multiple addresses from compareAddr[i] may match to multiple stored Addresses
for (NSUInteger i = 0; i < [compareAddr count]; i++)
{
    //Checking IF the obtained key (Mac Address) at compareAddr[i] matches one of the stored Addresses
    NSString *currentAddr = [compareAddr objectAtIndex:i];
    BOOL addrExists = ([[dictionaryOfAddresses     objectForKey:@"StoredAddr"]objectForKey:currentAddr] != nil);

    if (addrExists)
    {   
        NSLog (@"Match found at index %1u", i);
        [matchedAddr addObject:currentAddr];
    }

    else { NSLog(@"No Value matches at index %i \n", i); }
}
NSLog (@"Array of matched addresses for further processing %@", matchedAddr);

【问题讨论】:

    标签: objective-c loops nsarray


    【解决方案1】:

    将其定义为可变

    NSMutableArray *matchedAddr=[[NSMutableArray alloc]init];
    

    【讨论】:

    • 还应注意NSArrayNSMutableArray 是分开实现的,以便尽可能使用资源较少的NSArray
    • 另请注意,在尝试向其添加对象之前,他需要分配/初始化 NSMutableArray
    • 当我打印我的数组时它打印 null ! ! !
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多