How to create a .m file corresponding to a .h file in xcode - objective-c

When I try to create a .m file from the file menu, it prompts me for choosing a template from the several types mentioned - Objective-C class, Objective-C category, Objective-C protocol, etc.. Which one should be preferred?
I am fairly new to Objective-C as well as Xcode, so pardon me if this question is too obvious.

You may have to add Objective-C class and rename the new.m alone
to match your old.h and remove the newly added new.h file.
Create a new Objective-C class and copy your code from old.h to new.h file
Update as per comments: I see you are trying to add .m for the MAC sdk library, which is not possible.
You may have to consider using Categories extending the existing class methods.
What is “category
Customizing Existing Classes

If you want to create just an empty .m file, you can do so following these steps (Xcode 5.1.1):
In your Xcode project, go to File --> New --> File...
Select Other --> Empty
Click on Next
Type a name for your file and be sure to finish it with extension .m
Click on Create to save it

Related

Detect Xcode project development language

We can create a new Xcode project using Objective-C or Swift.
I want to detect, which language was selected when a project was created?
Projects can be a mix of Objective-C and Swift, but I am concerned about the language, selected during project creation.
May be it can be through pbxproj file or other better way.
Thanks.
ObjC project always contains the entry point that is main function.
Usually it is inside main.m file but it is not necessary. Developer can replace it to any name.
I tried 2 simple projects. One is in ObjC and second one is in Swift.
When I was tried to add new file the Xcode offered Swift language for Swift project and Obj-C for Obj-C project file.
Next I removed in Swift project AppDelegate.swift file and added AppDelegate.h and AppDelegate.m and (sic!) main.m files. of course I had to create bridging file for obj-c.
Which contains
#import "AppDelegate.h"
I was able to compile this project but when I tried to add new file Xcode offered me to add Objective-C file. But initially the project was created as Swift project.
I did similar manipulation for Obj-C project.
I removed AppDelegate.h and .m file as well as main.m and added AppDelegate.swift
I was asked to create bridge file and did empty file.
Next I went to «Build Settings» and switched Define Module parameter to YES value. (Without this I got linker error).
After it I was able to build and run this initially obj-c project which has AppDelegate in Swift now.
When I tried to add a new file the Xcode offered me to add new Obj-C file too.
So. It looks like you cannot detect initial language based on a parameter in Xcode because project can be always corrected. I think that rarely the developer will try to replace AppDelegate in a project and add\remove main entry point.
Hope this helps you.

Adding objective c class that uses swift classes to bridging header Projectname_swift.h not found

I have an objective-c class that uses swift classes. It all works fine.
I wanted to import the objective-c class into a swift class, so I added its header file to the bridging header. All the sudden I got an error the Projectname_swift.h file is not found.
Any ideas how to resolve this issue?
Is it actually possible?
a circular reference has been created, making it so the Swift code is unable to compile (which leads to the canary error stating that the _Swift.h file is not found).
i have provided a more in depth answer to a similar questions here and here.
long story short, the documentation explicitly says not to this:
To avoid cyclical references, don’t import Swift code into an Objective-C header (.h) file. Instead, you can forward declare a Swift class or protocol to reference it in an Objective-C interface.
Forward declarations of Swift classes and protocols can only be used as types for method and property declarations.
in order to make your code compile again you will need to remove the #import "Projectname_Swift.h" line from the offending Objective-C header. ideally you can simply move the import statement into your .m file, however if you need to publicly expose the Swift class in your ObjC header, then you must forward declare it using #class SomeSwiftClass;.
Let the Xcode build the bridge file from Objective-C to Swift.
Create a temporary directory elsewhere. In there, you create a dummy Xcode Swift project, give the project name the same as your existing Current Project Name.
Then add new file, Objective-C (.m file). The XCode will prompt you to create a bridge header file, click on the create bridge file (the right most button).
Now you locate the header file location in Finder. Then drag into your Current Project of Interest, don't forget to checked the copy file if necessary option. Add necessary #import '.....' in the header file.
You should be good. If everything works fine, delete the dummy project.
Clean derived data. and then #import "ProjectName-Swift.h" in your objective c files.
Go to
Build Settings->Objective-C Generated Interface Header Name
and set the value to YourModule-Swift.h (this is usually already set, this is the filename you need to import on .m file #import "YourModule-Swift.h"
Go to Build Settings and search for "Defines Module", set both values to YES
Create a class in swift with prefix of #objc for example
#objc class mySwiftClass{...}
Build the project again
it will be better if you use error syntax or screen shot. you can simply try this
1. Goto your project on top of right navigation
2. select build settings from middle pain.
3. search for Objective-C bridging header
4. just below this you will find "Generated interface HeaderName"
5. add correct address of your swift file
6. clean and build the project.

How to use .swift file in Objective-C?

I have added (by drag n drop) a .swift file in my objective-C code, Now I want to use it into my Objective-C code. I tried importing like:
#import "MyProductModuleName-Swift.h"
But file not found appears.
You need to go to your target Build Settings and change Define Module to Yes and also you need to change Product Module Name to some value.
Now it will show the YourProductModuleName but if you click on it it shows $(PRODUCT_NAME:c99extidentifier), replace it with your module name and use it in import as in the example in your question.
Hope this help
The file is created automatically (talking about Xcode 6.3.2 here). But you won't see it, since it's in your Derived Data folder. After marking your swift class with #objc, compile, then search for Swift.h in your Derived Data folder. You should find the Swift header there.
I had the problem, that Xcode renamed my my-Project-Swift.h to my_Project_Swift.h Xcode doesn't like "." "-" etc. symbols. With the method above you can find the filename and import it to a Objective-C class.
And you should also need to change the target of your project to 10.9
Try this hope it helps.

I need to create separate file for all the constants of my project

In my project I have a requirement to create separate file for all constants that i am using in separate classes in the same project.
I seen some examples but they are saying about creating in '.h' file and again they are implementing them in '.m' files. But i need only '.h'file to create all constants and i have to use all those constants by importing that '.h' file in every class of my project.
ADD a new file.
Right click on the file inspector
choose New File
The pop up window select ios>C and C++>HeaderFile[Figure]
Give name Constants
Add #define OK #"OK"
Go to View Controller include file in header #import "Constants.h"
OR Define in pch file ,so that all View controllers can access the file
In viewDidLoad NSLog(#"%#",OK);
You can create .h file and use #define to create constants and then include your file to prefix file of your project. Though I prefer to use extern constants which you declare in .h file and define in .m file. This help to track possible warnings in your code at compilation time.
You've pretty much answered your own question, to the point where I'm not sure exactly what you're asking - you can just create a header file (.h) with your constants, and import it into your other classes. You don't need to create a corresponding implementation (.m) file. If you're using your constants throughout your code you could import them in your prefix header and have them automatically available.

Xcode weird naming convention for ViewController

So I'm creating a new file, a subclass of a UIViewController via the Xcode wizard that pops up through new > file.
I'm naming the file "747ViewController" and it spits out the .h/.m files fine.
However inside the files, the interface and implementation names are odd:
They show up as #interface _47ViewController : UIViewController and #implementation _47ViewController
Are you not allowed to start with a number or something? Should I leave it or rename it to what I want it? Thanks
That's correct: just like in C and C++ your identifiers can not start with a number. The workaround from Xcode is correct: Unlike Java, the source file name and the class name do not have to be the same.