How can I add an Object to a NSOrderedSet at the last position?

1.2k Views Asked by At

How the Title says - I want to be sure that the object is on the last position but I cant find any functions for NSOrderedSet. Is it possible to set it simply at data[data.count]?

3

There are 3 best solutions below

0
On BEST ANSWER

NSOrderedSet is immutable. You'll need NSMutableOrderedSet, in particular, you'll want to use addObject(_:), such as:

data.addObject(myData)

NSOrderedSet and NSMutableOrderedSet are both a part of the Foundation framework, and do not conform to the CollectionType protocol, so you cannot use the [] operator like in your question.

0
On

NSMutableOrderedSet *mutableData=[data mutableCopy];

[mutableData addObject:theObjectToAdd];

data=mutableData;

0
On

No. You need to use NSMutableOrderedSet.

In that case, you can just use:

  • Swift: func addObject(_ object: AnyObject)
  • Objective-C: - (void)addObject:(ObjectType)object