Categories in Objective-C aren't working

前端 未结 5 937
时光说笑
时光说笑 2020-12-16 18:51

I\'m developing an iOS application that needs to deploy to iOS 3.1.3. I need to extend some of the functionality of the NSData class and am using the following code inside N

相关标签:
5条回答
  • 2020-12-16 19:10

    I had the same issue with ARC project which was linking with non-ARC module having category extension.

    Fixed the issue by adding "Other Linker Flags: -all_load" in parent ARC project.

    0 讨论(0)
  • 2020-12-16 19:12

    It really seems like your category isn't being compiled or linked into the same target that you're using it from. You should make sure that NSData+Base64.m is marked to be compiled by the same target that it's being used from by getting info on the two files and comparing the targets they're assigned to.

    A test you can perform is to add a line with an #error error message to NSData+Base64.m, which will cause the build to fail when it gets to that file. Like this:

    #error We're now compiling NSData+Base64.m
    

    Then look and see which target fails to compile.

    0 讨论(0)
  • 2020-12-16 19:23

    There is a great post on The Carbon Emitter about about handling categories in iOS. It details an easy way to handle importing categories to your project.

    Make a file containing all of your category imports, in this example it is Extensions.h:

    #import "NSDate+Formatting.h"
    #import "UIFonts+MyFonts.h"
    #import "UIViewController+Tourbot.h"
    

    Add import your file in AppName-Prefix.pch:

    #import <Availability.h>
    
    #ifndef __IPHONE_3_0
    #warning "This project uses features only available in iPhone SDK 3.0 and later."
    #endif
    
    #ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import <QuartzCore/QuartzCore.h>
    #import <CoreText/CoreText.h>
    #import "Extensions.h" // Add import here
    #endif
    
    0 讨论(0)
  • 2020-12-16 19:23

    In My case when I got this error I simply added the .m file in the Compiled resources, and it get worked. This can be achieved by selecting target project->Build Phases->Compile Sources. Then you click on the + button from its bottom left. In this case you may add 'NSData+Base64.m' file to the compile sources. Then you clean your project and run. I guess this may help.

    0 讨论(0)
  • 2020-12-16 19:31

    Have you #imported the header file for your category? I know it sounds simple, but I forget nearly every time.

    0 讨论(0)
提交回复
热议问题