I have a generic header file that I include in every project. Among other things, it defines a preprocessor macro for easily obtaining a reference to the app delegate. The problem is, the class name of the app delegate changes from project to project, as it includes the product name (AppDelegate). Therefore I wonder if it's somehow possible to use ${PRODUCT_NAME}, or a similar macro construct, in header files?
Set Preprocessor Macros in Xcode Build Settings.
APPDELEGATE_CLASS=$(PRODUCT_NAME)AppDelegate
In xcconfig,
GCC_PREPROCESSOR_DEFINITIONS = APPDELEGATE_CLASS=$(PRODUCT_NAME)AppDelegate
Then you can use APPDELEGATE_CLASS macro in your code.
#interface APPDELEGATE_CLASS : NSObject <UIApplicationDelegate> {
Related
When I add a new file in Xcode 6.3.2 I see templates for the .m and .h file but how do you add both at once?
I'm sure there used to be a way of adding both at once.
The option has been moved into the more generic "Cocoa Touch Class" template, which allows you to make the class in either Objective-C or Swift.
Complete the name of your pair of files with the default prefix filled-in for you. Choose the superclass such as NSObject. Choose the Objective-C language. Click the Next button, then choose the folder location where to save your pair of new .h and .m files.
You can do this by add new file and "Cocoa Class"
What is happening when I specify the project (or test) when I click this selection?
I can't seem to find a a good resource explaining why I have to select the targets, only that in order to use my class, I must have a target associated with the class.
Targets basically specify the product that should be built. Your app could have multiple targets. I.e. a keyboard extension is a target and the main app for the keyboard extension is another target. If you have a class that is to be used in one or the other products, you must specify its target.
I am looking for a simple procedure for combining an Objective-C code from a shared library project with Swift code from an application project but have had no success so far with this sequence:
start Xcode 6.1.1
create workspace Test
create iOS Cocoa Touch Static Library project TestLibrary and add it to workspace
create iOS Single View Application project Test (language: Swift) and add it to workspace
add import TestLibrary to ViewController.swift
If I now build Test, I receive this error in ViewController.swift: No such module: ‘TestLibrary’.
Presumably two hurdles must be overcome:
Tell TestLibrary that is should "export" TestLibrary.h. What is the right syntax and procedure for adding the (presumably) required bridging header file?
Tell Test where TestLibrary is located. Since both the application and static library projects belong to the same workspace (and sit in the file system next to each other) I assume no explicit steps are required, or are there?
So in summary, my question is this: how can I overcome the build error even if I subsequently add let test = TestLibrary() to ViewController.swift, i.e. how can Test (Swift code base) make use of TestLibrary (Objective-C code base)?
This procedure seems to work (for single app target):
Do not add import TestLibrary, since both the Swift and Objective-C code will reside in the same app target
Create Test-Bridging-Header.h to Test and add #import "TestLibrary/TestLibrary.h"
Set build setting Objective-C Bridging Header: Test/Test-Bridging-Header.h for Test
Drag libTestLibrary.a from TestLibrary (under Products) into Test's Link Library With Binaries (under Build Phases)
Add let test = TestLibrary() e.g. inside viewDidLoadin ViewController.swift
Voila ...
In my case, I have to add all the .h file's of the Framework in the Bridging file, that fixed the issue. Also remove the import TestLibrary from the swift files
Is there anyway to make xcode (4.2) automatically add synthesize and the empty method bodies to the implementation file (for properties and methods defined in the interface) ?
There is no built in solution for that in 4.2. You could try creating a shell script that searches your header for properties and then adds the #synthesize lines at the right place.
Why don't you use the latest (4.4) version instead? 4.4 can automatically synthesize the ivars for you. All you have to do is declare a #property and Xcode takes care of the rest.
I have a basic Cocoa project open in Xcode including a unit test target.
I created a new test class to test one of my model classes. However, I can't add the header of the class I need to test to the unit testing target. The checkbox under "Target Membership" is not clickable for the unit test target (it is for the main target).
Because I can't add the header file to the unit test target, Xcode won't autosuggest method names of the class under test.
Am I missing something? Shouldn't I be able to add header files to a unit testing target?
I'm not an Xcode expert, but in my project, none of the header files have any target memberships set, none of the checkboxes are clickable - you can only add the .m files, and header files are somehow magically included. You should be able to just import the header file at the top of the test file and have method autocomplete work - unless you're using some weird macros in those tests, because I've noticed that Xcode gets confused with autocomplete while you're inside a macro call. If that's the case, try if the autocomplete works outside any macros.