Need help understanding Xcode *.pch files - xcode

I'm working on a 3rd party UIKit replacement for iOS. I have it building as a framework using a seriously helpful project from GitHub. (Not mine, but if you have interest, it's here.
I'm trying to use my library in other projects I'm writing. Like I said, it's basically a drop-in replacement for much of UIKit, so I decided to import the framework in my project's *.pch file instead of everywhere I might wish to use a button, action sheet, alert view, etc...
When I DON'T have an #import directive in a header file and declare a property of type MBMButton, the compiler gives me an error of "Unknown type name 'MBMButton'; did you mean 'UIButton'?" Oddly enough, the code will still run (even though this is an error, not a warning). Adding #class MBMButton or #import <MBMUIKit/MBMUIKit.h> resolves this compiler complaint.
When I DON'T have an #import directive in an implementation file (or its header) and call a method that exists in MBMUIButton but NOT in UIButton, I get a compiler error of "No visible #interface for 'UIButton' declares the selector...". As before, the code will actually run, since it's a valid call.
Now, I've done some digging, and I've changed my project's settings. Where I didn't have any value in the GCC_PREFIX_HEADER, I added the file name. Noting the Quick Help description by Apple, I tried both "ProjectName-Prefix.pch" and "./ProjectName-Prefix.pch". Neither seemed to resolve the problem. I eventually figured out that the Target-level settings override the Project-level settings, and that the Target-level settings already specified "ProjectName/ProjectName-Prefix.pch". So that was a dead end. (Nice to learn exactly what the relative path settings are, though!)
I'm OK with not using the *.pch file. It's only a convenience, and I can definitely use appropriate #class and #import directives. What's bugging me is not understanding the issue. How should one use the *.pch file?

The prefix header file is automatically included in every .m file in your project but not in any .h files.
Therefore, any references to classes will require the header to be included or a forward declaration:
#class MyClass;
However for #protocols you'll need the full header, a forward declaration won't work:
#protocol MyProtocol; //this won't work
#interface MyController : UIViewController <MyProtocol>
…
#end

Related

Swift class not appearing in generated <Project_Name>Swift.h header file

I'm having problems importing a class (which is written in Swift and from a pod) into my Objective-C code. I think the problem is due to the class failing to appear in my autogenerated -Swift.h header file, but I don't know how to fix this.
My Podfile has the use_frameworks! declaration and I have the pod installed and I'm able to use the pod's class in any of my Swift code but not in Obj-C.
The Swift class I'm trying to import is an extension of UITableViewCell, which is declared as a public class. It didn't have the class declared with the #objc annotation in its source but adding it myself didn't seem to make a difference either.
In my Obj-C source I have #import "ProjectName-Swift.h", (with my actual project name) but my Obj-C file doesn't still doesn't recognise the class after a clean and build. I have checked that the Swift header file exists (and it does) but upon inspection it doesn't seem to have any mention of the Swift class that I'm trying to import.
I tried putting in a forward #class declaration to the class in my Obj-C source, but this only removed the warning and delayed the error until I try to build. Modules are enabled in my build settings. If anyone has some insight, your help would be much appreciated.
Edit:
If I manually add the source file to Build Phases -> Compile Sources for my project, then everything works - but of course this defeats the purpose of using a pod. So this seems to be an XCode search path issue.
Only public classes and methods are published to be used from other modules. To ensure Objective-C compatibility, you can annotate your classes with #objc and inherit NSObject.
For example, change your class definition from:
class MyClass {
func sayHello() {}
}
To:
#objc public class MyClass: NSObject {
public func sayHello() {}
}
To use Swift frameworks (iOS 8+) in Objective-C on XCode 7, you just have to import them like you would in Swift using the #import statement:
#import MyModule;
And all your public classes will be available.
More information in 'Importing External Frameworks' and 'Using Swift from Objective-C' sections of the Swift and Objective-C in the Same Project apple docs.
I ended up finding a solution. It seems that Swift pods aren't added to the Project-Name-Swift.h, but instead a separate Swift header is generated for each module.
What I needed to do was import the following Swift header file.
#import "Module_Name/Module_Name-Swift.h"
An alternate solution that eluded me was pointed out by redent84. You just need to put the following in your Objective-C source as you would in Swift.
#import Module_Name;
which is probably equivalent to the swift header import once the sugar is stripped away.

How can I use Chartboost with Cocoapods in Swift?

This is my Podfile
platform :ios, 8.0
use_frameworks!
pod "ChartboostSDK"
pod "SVProgressHUD"
SVProgressHUD is being turning into a framework, but for some reason Chartboost is not.
That of course makes the import for Chartboost not working in my code.
The library ChartboostSDK is not supported for Swift directly using CocoaPods, you need to integrate it manually in the following way:
1.First, unzip the SDK package and drop the Chartboost Framework into your Xcode project. Be sure you've also linked these frameworks:
StoreKit
Foundation
CoreGraphics
UIKit
2.Then you need to add a Swift bridging header allows you to communicate with your old Objective-C classes from your Swift classes.You will need one if you plan to keep portions of your codebase in Objective-C. You can create it manually like in the following way:
Add a new file to Xcode (File > New > File), then select “Source” and click “Header File“.
Name your file “YourProjectName-Bridging-Header.h”.
Navigate to your project build settings and find the “Swift Compiler – Code Generation” section. You may find it faster to type in “Swift Compiler” into the search box to narrow down the results. Note: If you don’t have a “Swift Compiler – Code Generation” section, this means you probably don’t have any Swift classes added to your project yet. Add a Swift file, then try again.
Next to “Objective-C Bridging Header” you will need to add the name/path of your header file. If your file resides in your project’s root folder simply put the name of the header file there. Examples: “ProjectName/ProjectName-Bridging-Header.h” or simply “ProjectName-Bridging-Header.h”.
Open up your newly created bridging header and import your Objective-C classes using #import statements. Any class listed in this file will be able to be accessed from your swift classes.
You bridging header must be have inside the following lines:
#import <UIKit/UIKit.h>
#import <Chartboost/Chartboost.h>
#import <Chartboost/CBNewsfeed.h>
#import <CommonCrypto/CommonDigest.h>
#import <AdSupport/AdSupport.h>
You can read more about the next steps using Charboost in his iOS Integration help manual.
I hope this help you.

Xcode - Parse & facebook header file(SDK) error

I want to use both Parse & facebook SDK's with my app, I did everything and added every library from both Parse & facebook and edited my App Delegate file according to the docs, but I keep getting errors in my bridging-header file:
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <ParseFacebookUtilsV4/PFFacebookUtils.h>
#import <FacebookSDK/FacebookSDK.h>
#import <Parse/Parse.h>
Error:
'FBSDKCoreKit/FBSDKCoreKit.h' file not found
The errors are not specific to any statement, I delete one and the next gets an error. This file is called "MyApp-Bridging-Header.h"
The first thing I would try is to add the AVFoundation and CoreLocation frameworks to your target. Then delete the derived data from your project by going to window-->projects. Do this then clean and build your project.
Sidenote: Make sure when you type in "#import " the FBSDKCoreKit is autofilled in after you have typed a few letters. If the framework has been added already, it should give you the option to autofill the import. If it is not added this may not show up, and if this is the case, make sure your framework is added to your project in finder.
Hope this helps!

Header file is not being created when a new objective-c class is created in xcode 4.4

I am trying to create and objective-c class (subclassed UIViewController) with xcode 4.4. I go to File->new and select an objective-c class template under ios/Cocoa Touch. It says that this selection will create "An Objective-C class, with implementation and header files.". But it only creates an implementation file. The implementation file has a #import for the missing header file.
I am using storyboards. I did not select "With XIB for user interface".
Am I doing something wrong? If this is a bug, any suggestions about how to get around it?
ok, I found a solution. First I deleted the header file that I saw with Finder in the project directory. Then I went into xcode and deleted the implementation file. Then I started over and re-created the objective-c class. This time the header file was created. Weird

Creating an ABPerson object

This problem is almost certainly gonna have a really simple answer, but I just can't see it. I'm programming an app for the iPhone in Xcode and I'm trying to create an instance of an ABPerson object, but can't. In my .h file, I am importing as follows:
#import <AddressBook/AddressBook.h>
#import <AddressBook/ABPerson.h>
Then when I try to create it using "ABPerson *person;", it gives the error, "Unknown type name 'ABPerson'".
I have searched the internet and there doesn't seem to be much on the use of ABPerson and where I have seen it used, they have done it like this and its worked fine.
Ultimately, I want to create a VCard containing the details of somebody which the user can then save to their phone so if you know of another way of doing this which would eliminate this problem, that would also be great.
Thanks,
Matthew
Have you added the AddressBook framework to your app? Look in XCode4, look at your project file -> Build Phases -> Link Binary with Libraries.
Try this.
#import <AddressBook/AddressBook.h>
#import <AddressBook/ABAddressBook.h>

Resources