【问题标题】:NSArray of NSSet shows nothing in NSLogNSSet 的 NSArray 在 NSLog 中没有显示任何内容
【发布时间】:2011-11-28 10:41:41
【问题描述】:
NSMutableSet *intersection = [NSMutableSet setWithArray:newsmall];

//this shows an array of newsmall as expected
NSLog(@"intersection %@", intersection);

[intersection intersectSet:[NSSet setWithArray:newnewbig]];

//this shows nothing
NSLog(@"intersection %@", intersection);

//this shows an array of newnewbig as expected
NSLog(@"newnewbig %@", newnewbig);

NSArray *arrayfour = [intersection allObjects];

//this shows nothing
NSLog(@"arrayfour %@", arrayfour);

newsmall 和 newnewbig 有一些匹配的字符串,所以我希望 arrayfour 显示几个字符串。

我做错了什么?

【问题讨论】:

    标签: ios nsarray intersection nsset


    【解决方案1】:

    问题在于您对intersectSet 工作原理的理解。

    我认为您希望它比较来自 newsmall 和 newnewbig 的字符串的内容,但它真正做的是比较对象地址。

    在拨打intersectSet 之前执行此操作:

    NSUInteger index = 0;
    for(NSString * aString in newsmall)
    {
        NSLog( @"newsmall string %d is %p %@", index++, aString, aString );
    } 
    
    index = 0;
    for(NSString * aString in newnewbig)
    {
        NSLog( @"newnewbig string %d is %p %@", index++, aString, aString );
    }
    

    intersectSet 仅在地址(上面格式中的 %p)匹配时才有效。字符串内容可能匹配,但 intersectSet 关心的是字符串地址。

    确实,您的解决方案是您需要采用不同的方式来比较集合之间的字符串。

    【讨论】:

      【解决方案2】:

      当您调用intersectSet 时,我认为这是在比较指针,而不是您的NSString 的内容。

      看看这里,它可能会有所帮助:SO Question

      【讨论】:

        猜你喜欢
        • 2016-04-01
        • 2014-09-29
        • 2020-03-24
        • 2014-07-21
        • 2016-02-19
        • 2020-06-16
        • 2021-02-05
        • 2018-10-10
        • 1970-01-01
        相关资源
        最近更新 更多