1

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.

Aastha
  • 23
  • 1
  • 4

2 Answers2

2
  • 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

Community
  • 1
  • 1
thatzprem
  • 4,697
  • 1
  • 33
  • 41
  • I cannot rename `old.h` since its a third-party library file. Also, I could not completely understand your first point. Could you explain why you are suggesting to remove `old.h`? – Aastha Sep 25 '13 at 11:11
  • @Aastha he never suggested removing `old.h`. When directions don't make sense, reading them again (perhaps more than once) sometimes uncovers the reason. – mah Sep 25 '13 at 11:16
  • because there is no way to add single `.m` file. You need to add both `.h` and `.m` file, after that you can change newly created `.h` file. – Hemant Singh Rathore Sep 25 '13 at 11:27
  • Sure. But, actually the .h file is a file from `MAC sdk`, which I cannot change. What should be the workaround for this case? Also, I think that I will not be able to replace the old file with the newly created one. – Aastha Sep 25 '13 at 11:47
  • what is the file name from MAC sdk? – thatzprem Sep 25 '13 at 12:08
  • `NSURL.h` is the name of the file. – Aastha Sep 25 '13 at 12:10
  • Why do you want to create a .m for this class? Its from the SDK framework. Looks like you can consider using categories. – thatzprem Sep 25 '13 at 12:13
  • https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html – thatzprem Sep 25 '13 at 12:13
  • @thatzprem I want to create a .m file for this class since the constants declared at the end of the file need to be defined. Those constants are the `common keys` and the Apple APIs make use of the values of those constants. – Aastha Sep 25 '13 at 12:44
  • For e.g., there is a constant: `FOUNDATION_EXPORT NSString * const NSURLNameKey AVAILABLE_MAC_OS_X_VERSION_10_6_AND_LATER;` declared in that file which is required to de defined. Is it possible that they be defined outside the .m file? – Aastha Sep 25 '13 at 12:47
  • @thatzprem I think I got it. Since the file is from MAC SDK, so, they have only provided us the interface. The .m file which is the implementation of that interface is hidden from us. Also, before I was thinking that the method implementation might be requiring the constant definitions from my side. – Aastha Sep 25 '13 at 13:21
1

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
camilomq
  • 3,021
  • 1
  • 17
  • 10