2

I am creating a label (CorePlot) object in a for loop and trying to add it to NSMutableSet which I need to pass a a parameter.

Strangely only one object if added to the NSMutableSet (first one) and others are not added.

Looks like I am missing something very basic.

Any advice?

I am attaching screenshots of the code as I want to show the values held by the NSSet object.

Image 1 - Objects gets added to NSMutableArray but not to NSSet Forming From that array

Objects gets added to NSMutableArray but not to NSSet Forming From that array

Code used in Image 1 -

    NSArray *months = [NSArray arrayWithObjects:@"Oct",@"Nov",@"Dec",@"Jan",@"Feb",nil];
NSMutableArray *xLabels = [[NSMutableArray alloc] init];
for (NSString *month in months) {
    CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:month textStyle:axisTextStyle];
    [xLabels addObject:label];
}
NSSet *xLabelSet = [NSSet setWithArray:xLabels];
x.axisLabels = xLabelSet;

Image 2 - Objects not getting added to NSMutableSet Objects not getting added to NSMutableSet

Code used in Image 2 -

    NSArray *months = [NSArray arrayWithObjects:@"Oct",@"Nov",@"Dec",@"Jan",@"Feb",nil];
//NSMutableArray *xLabels = [[NSMutableArray alloc] init];
NSMutableSet *xLabelSet = [[NSMutableSet alloc] initWithCapacity:[months count]];
for (NSString *month in months) {
    CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:month textStyle:axisTextStyle];
    [xLabelSet addObject:label];
}
//NSSet *xLabelSet = [NSSet setWithArray:xLabels];
x.axisLabels = xLabelSet;
Dev
  • 6,207
  • 4
  • 27
  • 32
  • 3
    Does the class implement `isEqual` and `hash` ? – Wain Apr 10 '15 at 10:58
  • In the future you can paste the values as well. It is difficult to copy & paste code from image. – Lajos Arpad Apr 10 '15 at 10:58
  • In the first code you are not using xLabels at all. In the second code you are initializing with [[NSMutableSet alloc] initWithCapacity:[month count]] and the next thing you are doing is assigning it to x.axisLabels. – Lajos Arpad Apr 10 '15 at 11:03
  • @Wain - Yes it does. Checked in the source files of the framework. This is the class - http://core-plot.googlecode.com/hg/documentation/html/iOS/_c_p_t_axis_label_8m.html – Dev Apr 10 '15 at 11:18
  • @LajosArpad - Sorry about that. Will post the code. To answer your questions ... In first code I am adding the objects to xLabels (NSMutableArray) at line 195 and trying to create NSSet (xLabelSet) at line 197 from xLabels. In second code, I am adding objects to xLabelSet at line 196. – Dev Apr 10 '15 at 11:22
  • Don't post code images. Use copy/'paste. – Hot Licks Apr 10 '15 at 12:31

2 Answers2

3

The documentation for the isEqual method for the CPTAxisLabel says -

Axis labels are equal if they have the same tickLocation.

As you aren't specifying the the tickLocation property for the labels you are adding they will all have the same tickLocation - 0.

Since isEqual returns true for all of your labels you only end up with the first one in your NSSet - the addition of the subsequent label is skipped as an equal object is already in the set.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
0

I don't see the error on your code but I would suggest using CFMutableSetRef since it has a Toll-Free Bridging capability. Using it will enable you to give it as the parameter you need.

Maybe using the messages (methods) from CFMutableSetRef will enable you to add more objects.

Another thing I would try is simply doing an array with the CPTAxisLabels and using the method addObjectsFromArray from the NSMutableSet and check if that works.

Jose Luis
  • 3,307
  • 3
  • 36
  • 53
  • Thanks @Joze ... can you give some sample code for CFMuableSetRef? I tried using it but theres no initialisation methods. Tried initialising with CFMutableSet but it says implicit conversion not allowed in ARC. Also had already tried addObjectsFromArray but its not working. – Dev Apr 10 '15 at 11:57
  • @Dev I think you have to use `CFDataCreateMutable` to get it initialized. Like `CFDataCreateMutable(NULL,0)`. I now think that the answer of Paulw11 Is more to the point maybe. – Jose Luis Apr 10 '15 at 12:02