2

I'm using CocoaPods to import some things. Right now I'm trying to use TTTAttributedLabel, which is a rich subclass of UILabel, allowing for URL-clicking etc.

Now, I'm having trouble getting my app to recognise that my labels are actually instances of TTTAttributedLabel, and not regular UILabel's.

In my .xib, I have dragged a regular UILabel into my view, then edited the class of the label like this:

xib class

It did autocomplete the name for me, so I know it knows about the class. Then, I have dragged a connection from the xib to the view's class, like this:

connection It specifies itself as a TTTAttributedLabel automatically, because it recognises that that's what it is, or else it would've said UILabel.

Since I've specified use_frameworks! in my podfile, I have to import the class, so in the top of my view's class, I have import TTTAttributedLabel.

Now, when I try to use the label, it autocompletes likes this: autocomplete With the correct class, TTTAttributedLabel. But when I do this:

print("labBody: ", labBody)

it prints out this:

labBody:  <UILabel: 0x7fa754c86710; frame = (16 459; 42 21); text = 'Label'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60000029cf20>>

thinking it's a UILabel. If I try to access any of the TTTAttributedLabel-specific variables or functions, it still autocompletes them for me:

labBody.enabledTextCheckingTypes = NSTextCheckingResult.CheckingType.link.rawValue

But when using these on runtime, the entire app crashes, saying this:

 -[UILabel setEnabledTextCheckingTypes:]: unrecognized selector sent to instance 0x7fdfc7c59300

Am I blind? Why is this not working? This is what I've always been doing. I might be having a hard Tuesday.. I have tried all the regular stuff, cleaning, deleting derived data, restarting Xcode, deleting the app from the device before running again..

I have even used TTTAttributedLabel before, I know it works. The only thing that's new is that this is my first time using use_frameworks!.. Does that affect the .xib? Making it unable to know about the subclass, or something..?

Sti
  • 8,275
  • 9
  • 62
  • 124
  • 1
    Look at your console; are you getting a warning "unrecognised class TTTAttributedLabel in. In file" or something like that? What module is showing below the custom class in your nib file? – Paulw11 Mar 28 '17 at 21:15
  • @Paulw11 Oh damn, you're right! I didn't even notice. It says `Unknown class _TtC16MyApp18TTTAttributedLabel in Interface Builder file.`. What does this mean? – Sti Mar 28 '17 at 21:17
  • The reason you can easily see the auto completion for TTTAttributeLabel methods is: you have declared it as TTTAttributedLabel. Even IF the implementation of that variable was a UILabel, it would still be considered by the compiler as TTTAttributedLabel, thus you won't get a warning from your compiler. And of course you can assign a UILabel to TTTAttributedLabel, as this is the superclass. – Lepidopteron Mar 28 '17 at 21:19
  • Have you tried to Clean the entire project. Quit xCode and perform again a pod install and pod update? Make sure that the pod is included to your build target. Now reopen the project and build it first (build and run not required). Now head to your xib-file. Select the class for UILabel and press enter! Now click somwhere else in the interface builder to make sure the class change is used. Save xib and give it one more try. – Lepidopteron Mar 28 '17 at 21:22
  • @Lepidopteron Still not working, sadly.. And when I googled `Unknown class .. in Interface Builder file.`, I get a lot of suggestions to fix the `Module` in IB, but the module is correct. It says "Current - MyApp". In the dropdown, I can choose "MyApp", no difference. I have tried hitting enter inside both fields, I have removed TTTAttributedLabel from my podfile and reinstalled the pods back and forth now. I have removed the labels and re-added them. I have cleaned derived data, restarted xcode and restarted my computer. Now it's time for me to restart my head, so I'll try again tomorrow. – Sti Mar 28 '17 at 21:33
  • I would expect the module to be "TTTAttributedLabel" – Paulw11 Mar 28 '17 at 21:34
  • @Paulw11 Oh! I've never had to use another module than "MyApp" before. Then again, I've never used ’use_frameworks!’ either. Are they connected? It's weird though, I didn't get that module up in the drop down list.. maybe it will autocomplete the name if I start typing it.. I'll try it tomorrow! – Sti Mar 28 '17 at 21:48
  • @Paulw11 Tried now, still no joy. – Sti Mar 29 '17 at 05:52

1 Answers1

2

I have no idea exactly why this worked, but I assume it has something to do with this pod being in Objective-C and something about binding and loading Obj-C stuff..

Anyway, all I had to do was to wrap the object in a new local class. newclass and then change the class of my labels in the .xib-file to this class instead of TTTAttributedLabel (and the outlets in my view's class, obviously).

No need to do anything else than creating this class, it doesn't need to contain anything.

I did read something about setting a flag, like a load ObjC-flag, I didn't try it. I thought that that solution might also load a lot of other Object-C stuff I didn't need/want to load..

Community
  • 1
  • 1
Sti
  • 8,275
  • 9
  • 62
  • 124
  • This saved my day. I spent around 2-3 hours behind this issue. Creating an empty class done the job for me. – Anand Sep 01 '21 at 08:31