38

I've upgraded XCode from 5.1.1 to XCode 6.0.1 recently. Now everytime I want to define a new UIImage object I get this error:

Unknown type name 'UIImage'

Code:
1. Create a new project
2. Add Image View control to the storyboard
3. Reference the Image View by adding IBOutlet
4. Add new class file
5. Add the following line of code to the header file of the new class:

@property (strong, nonatomic) UIImage *background;

Header file (.h) content:

#import <Foundation/Foundation.h>

@interface CCTile : NSObject

@property (strong, nonatomic) NSString *story;
@property (strong, nonatomic) UIImage *background; // Error: Unknown type name 'UIImage'

@end

However, if I add #import <UIKit/UIKit.h> to the header file (above) everything seems OK! Any ideas what am I missing here, please? Is this a change in XCode header files!

aenawi
  • 523
  • 1
  • 4
  • 8
  • Without importing UIKit the compiler doesn't know what UIImage is. UIImage is defined in UIKit so you need to import this. Probably Xcode didn't automatically import this because you are subclassing from NSObject and not UIViewController or a subclass thereof – Alain Stulz Sep 26 '14 at 11:21
  • This #import was not required in XCode 5 as I remember! UIImage was known somehow, maybe because it was defined in the (main.m), which is the case with XCode 6 however! Well, I think there is a change in XCode 6 that causing this behavior. – aenawi Sep 27 '14 at 02:10

3 Answers3

86

I also had the same problem and fixed it using

#import <UIKit/UIKit.h>

However, I dug around some more and compared a project made in XCode 6 compared to Xcode 5, and I noticed that Xcode 6 did not create a prefix header file. The prefix header file is implicitly imported into every class, and the prefix header file (.pch) includes UIKit as well as Foundation.

To create a pch file, go to File -> New -> File -> Other -> PCH file. Edit the name to "YourProject-prefix.pch". Add the following:

#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif

Then you need to set the build settings. Go to your project -> build settings -> search at the top: prefix header.

You will see Precompile Prefix Header -> change to yes. Also, right below is Prefix Header. Add the path of your prefix header. Should be like: "YourProject/YourProject-prefix.pch".

jdmorgenstein
  • 1,026
  • 7
  • 3
  • 1
    great stuff, like this you don't have to think about importing UIKit manually in each relevant file. thanks! – Irina Anastasiu Mar 18 '15 at 04:44
  • I am facing the same problem, but in a pod's file. Now I cann't change the pod file (because it will rechange it on next update). And I want to use the new Modulus provided by Xcode 6 too instead of the pch file. N – Chanchal Raj Dec 03 '15 at 11:55
  • its very helpful for me. – Anju Apr 13 '16 at 07:18
9

for Unknown type name 'UIImage' or any UI object which is required.

#import (UIKit)this will solve your problem. this is working for me....

BalKrishan Yadav
  • 467
  • 7
  • 21
  • 2
    I've noticed that! I'm wondering why this is happening with XCode 6! In XCode 5 I don't remember I had to to #import any extra headers for normal UI objects. – aenawi Sep 26 '14 at 11:09
6

Ran into the same issue. Resolved it by doing

#import <UIKit/UIKit.h>

Kudos apple :P

Nitesh
  • 1,389
  • 1
  • 15
  • 24