Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

size

uiimage

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.

like image 831
Chatar Veer Suthar Avatar asked Dec 13 '25 18:12

Chatar Veer Suthar


2 Answers

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.

like image 200
Mike Mertsock Avatar answered Dec 15 '25 12:12

Mike Mertsock


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.

like image 28
Kevin Avatar answered Dec 15 '25 14:12

Kevin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!