Chatar Veer Suthar
Chatar Veer Suthar

Reputation: 15639

-[__NSCFConstantString size]: unrecognized selector sent to instance ...?

redSegmentedControl.m

redSegmentedControl.h

Now, I write code in my another controller

 // self.segmentedControl = [[redSegmentedControl alloc] initWithFrame:CGRectMake(0, 140, 320, 50)];

self.segmentedControl = [[redSegmentedControl alloc] initWithSectionImages:@[@"list.png", @"list.png", @"list.png"] sectionSelectedImages:@[@"list.png", @"list.png", @"list.png"]];


//[self.segmentedControl setSectionTitles:@[@"List", @"Activity", @"Top Tags"]];

If commented lines are removed , it works fine, but if this code is run, its gives error.

-[__NSCFConstantString size]: unrecognized selector sent to instance ...

I traced and I found issue in these lines of - (void)updateSegmentsRects method.

 for (UIImage *sectionImage in self.sectionImages) {
            CGFloat imageWidth = sectionImage.size.width + self.segmentEdgeInset.left + self.segmentEdgeInset.right;
            self.segmentWidth = MAX(imageWidth, self.segmentWidth);
        }

Here, it access the size of Images which are put in array, but gives error, can anyone replace code or tell me reason.

Upvotes: 3

Views: 8781

Answers (2)

Mike Mertsock
Mike Mertsock

Reputation: 12015

Your redSegmentedControl class expects sectionImages to be an array of UIImage objects, but your code above that initializes the object using initWithSectionImages passes it an array of NSStrings. Thus the error.

So presumably you want to either change the caller of the init method:

self.segmentedControl = [[redSegmentedControl alloc]
                         initWithSectionImages:@[[UIImage imageNamed:@"list.png"], ...]];

Or change the redSegmentedControl implementation so that it does expect sectionImages to be an array of NSStrings. Probably the former option would be better, so that the caller can be responsible for how to create the images, the control shouldn't have to care about that.

Upvotes: 6

Kevin
Kevin

Reputation: 1506

Your initWithSectionImages method expects an array of UIImages, but you are passing an array of NSStrings. And since a NSString object doesn't have a size property, it won't work.

So pass UIImages instead of NSStrings and it should work.

Upvotes: 1

Related Questions