Why can you only #define an NSString constant in a header file? - objective-c

A curious objective-c newbie question. I noticed that I can #define an NSString in the .h file, but not in the .m file... why?
The declaration in question is:
#define kSomeString #"This is a string"
That declaration fails with an error if its in the .m file.

you CAN define them also in the implementation files.
#import "SAiPadHomeViewController.h"
#define hugo #"Test"
#interface SAiPadHomeViewController ()
#end
#implementation SAiPadHomeViewController
#end
This example works - try it.
Greetz!

Related

NSImage forward declaration error

I'm trying to convert a NSURL to a NSImage but when I try to do so with the following code:
NSURL *URL = [NSURL URLWithString:[myArray objectAtIndex:i]];
NSImage *myImage = [[NSImage alloc] initWithContentsOfURL:URL];
xCode gives me the error "Receiver type 'NSImage' for instance message is a forward declaration. According to other SO posts, it's a problem with importing. However, I'm already importing my .h file as well as CoreData and I've tried using this:
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#endif
from Receiver type is forward declaration. I have no idea what to do at this point, I'm sure it's obvious but I cannot figure it out. My header for my .m file is
#import "myClass.h"
#import <Foundation/Foundation.h>
and CoreData is being imported in the .h file.
Thank you!
For Mac, you want AppKit rather than UIKit.

Headerdoc, Objective-C, anonymous type, ERROR

I have to generate a doc for Xcode's project Objective-C.
I have an error anonymous type. I think headerdoc doesn't like #interface section. Can someone resolve that?
My apologies for some grammar mistakes, I have a Xcode's project Objective-C, I want generate a doc with a script (I'm not at my work so I will try explain the problem). In my project I have a class x
.h
/*! headerdoc comment */
#interface x: NSObject
#end
.m
#import x.h
#interface x(){
/*! headerdoc comment */
NSString *_xID;
}
#end
#implementation x
/*! headerdoc comment */
-(void)aMethod{
}
#end
And when I run, I have an error "anonymous type", and when I removed #interface section in .m it's working.

How to call Objective-C++ (.mm) from Objective-C (.m)

Is there a say to do this without changing every .m file to .mm?
OK. I am trying to implement the answer but having trouble. Take a look at my Objective C++ .h and .mm below
Objective-C++ - IDCaptureTemplateCommand.h:
#include "Template.h"
#interface IDCaptureTemplateCommand : NSObject
#property (nonatomic, strong) IDCaptureTemplateCommand *IDCaptureTemplateCommand;
#end
Objective-C++ - IDCaptureTemplateCommand.mm:
#include "IDCaptureTemplateCommand.h"
#implementation IDCaptureTemplateCommand
- (id)init
{
self = [super init];
if (self) {
self.captureTemplateCommand = [[IDCaptureTemplateCommand alloc] init];
}
return self;
}
#end
Objective-C - IDCameraViewController.h
#import <UIKit/UIKit.h>
#import "IDCaptureTemplateCommand.h"
#interface IDCameraViewController : UIViewController <UINavigationControllerDelegate>
#property (nonatomic) IDCaptureTemplateCommand *captureCommand; //ERROR - Unknown type name 'IDCaptureTemplateCommand'
#end
You can do so in the same way as you can use C++ from C or whatever. You need to be able to declare the interface using pure Objective-C and then the implementation can be written using Objective-C++.
If your header file uses C++, e.g. your class has an std::string instance variable, then to make the functionality accessible from Objective-C you have to write a wrapper or otherwise hide the C++ at the interface, so that your Objective-C files don't need to see any of the C++ declarations.

Use and Access Global/Extern Variables in Objective C

I know this question has been asked a billion times, but my particular question hasn't been answered. So sorry about the repetitiveness.
So I know how to declare and define extern variables (correct me if I'm wrong):
in foo.h file:
extern NSString *foo;
in foo.m file:
NSString *foo = #"fooey";
But then say I want to access/change the variable in the hoo.m file. How would I do that?
In .h
#interface SOViewController : UIViewController{
NSString * variable;
}
in .m you can set it wherever.
For example, viewDidLoad.
You can also declare this in the .m file by putting the declaration
#interface SOViewController(){
NSString * variable;
}
// #property (strong, nonatomic) NSString * myString; might be appropriate here instead
#end
Before the #implementation.
Ideally, since this is object-oriented programming, though, best practice would be to make the string a property of the class.
If you are really set on the extern keyword here is a stackoverflow post on how to use it Objective C - How to use extern variables?
EDIT
The question came down to how to pass variables around. You can look at this article How to pass prepareForSegue: an object to see an example of how to do that with seguing.

Storing an iOS Application Constant in Objective-C

I have a few unicode characters (musical flat and sharp symbols) that I currently have defined in a class:
#import <Foundation/Foundation.h>
#define kSongsSharpSymbol [NSString stringWithFormat:#"\U0000266F"]
#define kSongsFlatSymbol [NSString stringWithFormat:#"\U0000266D"]
#interface Song : NSObject {
//...
}
As the application grows I think these types of constants would be better off placed someplace that is available to the application files without having to include the class.
Questions
Am I defining the unicode characters correctly? It works but that doesn't mean it's right
Where, ideally, should I be placing these types of constants?
Cheers and Thanks!
I solved this by creating a custom class like so, and then adding it to my precompiled header file:
//Constants.h
#import <Foundation/Foundation.h>
FOUNDATION_EXPORT NSString *const kSongsSharpSymbol;
FOUNDATION_EXPORT NSString *const kSongsFlatSymbol;
//Constants.m
#import "Constants.h"
NSString *const kSongsSharpSymbol = #"\U0000266F";
NSString *const kSongsFlatSymbol = #"\U0000266D";
//.pch file
#import <Availability.h>
#ifndef __IPHONE_4_0
#warning "This project uses features only available in iOS SDK 4.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "Constants.h"
#endif