如何将NSArray中的对象放入NSSet?

时间:2022-09-25 18:00:14

I have some NSDictionary objects stored in an NSArray called telephoneArray. I fetch the values for the key number and then replace the NSDictionary I've just read with a new object at the same index in the array. I then want to put these new objects into an NSSet. How can this be achieved? See my unsuccessful attempt below.

我有一些NSDictionary对象存储在名为telephoneArray的NSArray中。我获取键号的值,然后用数组中相同索引处的新对象替换我刚刚读取的NSDictionary。然后我想将这些新对象放入NSSet中。怎么能实现这一目标?看下面我的失败尝试。

    // Add all telephones to this branch
    for (int i=0; i<[telephoneArray count]; i++) {

        [newTelephone setBranch:newBranch];
        [newTelephone setNumber:[[telephoneArray objectAtIndex:i] valueForKey:@"number"]];

        NSLog(@"%@",[[telephoneArray objectAtIndex:i] valueForKey:@"number"]);
        [telephoneArray replaceObjectAtIndex:i withObject:newTelephone];
        NSLog(@"phone number %i = %@",i,[[telephoneArray objectAtIndex:i] valueForKey:@"number"]);

    }

    NSSet *telephoneSet = [NSSet setWithArray:telephoneArray];

    NSLog(@"telephoneArray=%i",[telephoneArray count]);
    NSLog(@"telephoneSet=%i",[[telephoneSet allObjects] count]);

OUTPUT:

OUTPUT:

2010-03-06 03:06:02.824 AIB[5160:6507] 063 81207
2010-03-06 03:06:02.824 AIB[5160:6507] phone number 0 = 063 81207
2010-03-06 03:06:02.825 AIB[5160:6507] 063 81624
2010-03-06 03:06:02.825 AIB[5160:6507] phone number 1 = 063 81624
2010-03-06 03:06:02.825 AIB[5160:6507] 063 81714
2010-03-06 03:06:02.826 AIB[5160:6507] phone number 2 = 063 81714
2010-03-06 03:06:02.826 AIB[5160:6507] 063 81715
2010-03-06 03:06:02.826 AIB[5160:6507] phone number 3 = 063 81715
2010-03-06 03:06:02.826 AIB[5160:6507] telephoneArray=4
2010-03-06 03:06:02.827 AIB[5160:6507] telephoneSet=1

With the code above, telephoneArray can have a count of between 1 and 5 but telephoneSet always has a value of 1. I assume there's an obvious mistake but I can't see where.

使用上面的代码,telephoneArray可以有1到5之间的计数,但phoneSet总是有一个值1.我假设有一个明显的错误,但我看不到在哪里。

3 个解决方案

#1


89  

This is not correct:

这不正确:

NSSet *telephoneSet = [[NSSet alloc] init];
[telephoneSet setByAddingObjectsFromArray:telephoneArray];

That method returns an NSSet which you are doing nothing with (it doesn't add the objects to telephoneSet, it creates a new NSSet). Do this instead:

该方法返回一个你不做任何事情的NSSet(它不会将对象添加到telephoneSet,它会创建一个新的NSSet)。改为:

NSSet *telephoneSet = [NSSet setWithArray:telephoneArray]

NSSet * telephoneSet = [NSSet setWithArray:telephoneArray]

Also, note that a set cannot contain duplicates unlike an array. So if you have duplicate objects in your array and you put them in a set, the duplicates will be removed which can affect the object count.

另请注意,与数组不同,集合不能包含重复项。因此,如果您在数组中有重复的对象并将它们放在一个集合中,则将删除重复项,这会影响对象计数。

#2


11  

Initially telephoneArray contains references to n distinct objects. After the loop ends, it does contain n references, but each one is pointing to the same newTelephone object.

最初,telephoneArray包含对n个不同对象的引用。循环结束后,它确实包含n个引用,但每个引用都指向同一个newTelephone对象。

Array can contain duplicates, so it doesn't matter. A Set cannot have duplicates, and your entire telephoneArray is composed of a single object basically, so you're seeing just one.

数组可以包含重复项,因此无关紧要。一个集合不能有重复,你的整个telephoneArray基本上由一个单独的对象组成,所以你只看到一个。

In your loop, you have to create a new object or get a telephone object from somewhere:

在循环中,您必须创建一个新对象或从某个地方获取电话对象:

for (int i=0; i<[telephoneArray count]; i++) {
    // Create the new object first, or get it from somewhere.
    Telephone *newTelephone = [[Telephone alloc] init];
    [newTelephone setBranch:newBranch];
    [newTelephone setNumber:[[telephoneArray objectAtIndex:i] valueForKey:@"number"]];
    [telephoneArray replaceObjectAtIndex:i withObject:newTelephone];
    // the array holds a reference, so you could let go of newTelephone
    [newTelephone release];
}

Also, like PCWiz said, you don't need to allocate a new NSSet object in your case. Just call the class method setWithArray:.

另外,就像PCWiz所说,你不需要在你的情况下分配一个新的NSSet对象。只需调用类方法setWithArray:。

NSSet *telephoneSet = [NSSet setWithArray:telephoneArray]

#3


2  

Swift 3 Version

You can create a new NSSet from an array with:

您可以从数组创建一个新的NSSet:

let mySet = NSSet(array : myArray)

Additionally, you can add objects from an array to an already existing NSMutableSet with.

此外,您可以将数组中的对象添加到已存在的NSMutableSet中。

myMutableSet =  myMutableSet.addingObjects(from: myArray)

#1


89  

This is not correct:

这不正确:

NSSet *telephoneSet = [[NSSet alloc] init];
[telephoneSet setByAddingObjectsFromArray:telephoneArray];

That method returns an NSSet which you are doing nothing with (it doesn't add the objects to telephoneSet, it creates a new NSSet). Do this instead:

该方法返回一个你不做任何事情的NSSet(它不会将对象添加到telephoneSet,它会创建一个新的NSSet)。改为:

NSSet *telephoneSet = [NSSet setWithArray:telephoneArray]

NSSet * telephoneSet = [NSSet setWithArray:telephoneArray]

Also, note that a set cannot contain duplicates unlike an array. So if you have duplicate objects in your array and you put them in a set, the duplicates will be removed which can affect the object count.

另请注意,与数组不同,集合不能包含重复项。因此,如果您在数组中有重复的对象并将它们放在一个集合中,则将删除重复项,这会影响对象计数。

#2


11  

Initially telephoneArray contains references to n distinct objects. After the loop ends, it does contain n references, but each one is pointing to the same newTelephone object.

最初,telephoneArray包含对n个不同对象的引用。循环结束后,它确实包含n个引用,但每个引用都指向同一个newTelephone对象。

Array can contain duplicates, so it doesn't matter. A Set cannot have duplicates, and your entire telephoneArray is composed of a single object basically, so you're seeing just one.

数组可以包含重复项,因此无关紧要。一个集合不能有重复,你的整个telephoneArray基本上由一个单独的对象组成,所以你只看到一个。

In your loop, you have to create a new object or get a telephone object from somewhere:

在循环中,您必须创建一个新对象或从某个地方获取电话对象:

for (int i=0; i<[telephoneArray count]; i++) {
    // Create the new object first, or get it from somewhere.
    Telephone *newTelephone = [[Telephone alloc] init];
    [newTelephone setBranch:newBranch];
    [newTelephone setNumber:[[telephoneArray objectAtIndex:i] valueForKey:@"number"]];
    [telephoneArray replaceObjectAtIndex:i withObject:newTelephone];
    // the array holds a reference, so you could let go of newTelephone
    [newTelephone release];
}

Also, like PCWiz said, you don't need to allocate a new NSSet object in your case. Just call the class method setWithArray:.

另外,就像PCWiz所说,你不需要在你的情况下分配一个新的NSSet对象。只需调用类方法setWithArray:。

NSSet *telephoneSet = [NSSet setWithArray:telephoneArray]

#3


2  

Swift 3 Version

You can create a new NSSet from an array with:

您可以从数组创建一个新的NSSet:

let mySet = NSSet(array : myArray)

Additionally, you can add objects from an array to an already existing NSMutableSet with.

此外,您可以将数组中的对象添加到已存在的NSMutableSet中。

myMutableSet =  myMutableSet.addingObjects(from: myArray)