Storing an iOS Application Constant in Objective-C - 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

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.

Class with defined static constant values in Objective-C

I want to create a class that will contains static values accessable from all project.
Pseudocode:
class Constants:
constant String API_URL : "http://api.service.com"
constant Integer SOME_VALUE : 7
How can I do this with Objective-C ?
Answer for your question is extern keyword . I will explain it to you using an example . Add objective c classes your project and name them Common , Now in Common.h
#interface Common : NSObject
extern NSString *SiteApiURL;
#end
After you defined an instance of NSString Class using the extern keyword what you need to do is switch to Common.m class and initialize the value for NSString (SiteApiURL)
#import "Common.h"
#implementation Common
NSString *SiteApiURL = #"http://api.service.com";
#end
Import the Common.h class within the project-Prefix.pch file like this
#import <Availability.h>
#ifndef __IPHONE_3_0
#warning "This project uses features only available in iOS SDK 3.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "Common.h"
#endif
All done , now you can use the object "SiteApiURL" anywhere in the whole project and you need not to import any class anywhere i.e. You can use this variable anywhere in the project directly.
You could do it using preprocessors:
#define API_URL #"http://api.service.com"
#define SOME_VALUE (7)
Accessing defines would be simple: [object do:API_URL];
Or you could use constants
NSString * const apiURL = #"http://api.service.com";
NSNumber * const someValue = #7;
Accessing consts would be like accessing variables, So the string would just be a simple call. The NSNumber is an object wrapper for primitives so you'd need to access it like: someValue.intValue
You can create a Singleton with all necessary constants Here is a sample
If you do not want to create the class than you can use static private variables and static getters.
#interface
+(NSString*) getValue;
#end
#implementation
static NSString *_value = #"....";
+(NSString*) getValue {
return _value;
}
#end

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

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!

How to #import <NSObjCRuntime.h> to use objc_msgSend

I'd like to import runtime's header to use objc_msgSend but I'm getting:
error: NSObjCRuntime.h: No such file or directory
Should I add something to the header search path?
You need to include <objc/message.h> (you'll find the related headers in /usr/include/objc) and link to the objc (/usr/lib/libobjc.dylib) library.
#import <Foundation/NSObjCRuntime.h> does work
but you probably need
#import <objc/runtime.h>
like this Apple example does
upd: since iOS 7 #import <Foundation/NSObjCRuntime.h> replaced to #import <objc/NSObjCRuntime.h> but i recommend to use #import <objc/runtime.h> anyway
When using Xcode 6 and later, you will get an error after #include<objc/message.h>. It can be resolved like this
#include <objc/message.h>
void foo(void *object) {
typedef void (*send_type)(id, SEL, int);
send_type func = (send_type)objc_msgSend;
func(object, sel_getUid("foo:"), 5);
}
http://devstreaming.apple.com/videos/wwdc/2014/417xx2zsyyp8zcs/417/417_whats_new_in_llvm.pdf

Using static .lib (c) in iOS Project - Expected '=',',',';','asm' or 'attribute' before

I am trying to use static libraries that are written in c in an iOS Project. I included the .lib files and the .h into the iOS project. When I try to import the header files in one of my objective-C classes I get a lot of Expected '=',',',';','asm' or 'attribute' before... errors in the .h file of my static library.
I am using xCode4 for Development which seems to have added the libs correctly. When I open the project with Xcode 3 the libs are added to the Target Group "link binary with libraries" as stated in How to resolve linking error - static lib iPhone.
I got the static libs from a company that actually uses these libraries so I guess the header file is not at fault. I could not find any errors myself.
Is there a way to use .lib files with correct header files in an ios project? Or do I have to do anything besides adding the lib files to the target group in order to use them in my project?
Best Regards,
Mike
edit
the actual error message:
Expected * before *
Expected '=',',',';','asm' or 'attribute' before _far _pascal
The actual code where the header is imported:
#import <Foundation/Foundation.h>
#import "SomethingDll.h"
#interface AccountingEntry : NSObject {
NSString *entryDescription;
NSDate *entryDate;
double entryAmount;
NSString *entryType;
}
#property (nonatomic, retain) NSString *entryDescription;
#property (nonatomic, retain) NSDate *entryDate;
#property (nonatomic) double entryAmount;
#property (nonatomic, retain) NSString *entryType;
//class methods go here
//instance methods go here
-(id)initWithDescription:(NSString *)eDesc date:(NSDate*)eDate amount:(double)eAmount type:(NSString *)eType;
#end
The .h file of the lib.
#ifndef __SOMETHING_DLL
#define __SOMETHING_DLL
// constants for a function
#define FIRST_ERRTEXT 0
#define NEXT_ERRTEXT 1
/*
...
some other #define of constants
*/
// Callback-Pointer Definitionen
#define INFO_FUNC_DECL BOOL (CALLBACK *lpInfoFunc)(int)
#define FILETRANS_FUNC_DECL void (CALLBACK *lpFileTransFunc)(int,long)
// Funktionsdeklarationen
#ifdef WIN32
#define IMPORTAPI WINAPI
#else
#define IMPORTAPI _far _pascal
#endif
#ifdef __cplusplus
extern "C" {
#endif
void IMPORTAPI Something_Config( int iLogLevel, char *szLogFile,
long lTimeOut_Connect, long lTimeOut,
long lTimeout_GetFile, long lTime_Info,
int iSSLVersion, char *szSSLCipher,
char *szVerifyCertificateFile, char *szVerifyCertificatePath);
/*
...
a lot of other functions
...
*/
#ifdef __cplusplus
}
#endif
#endif // End
It seams that this lib is for Win32, the _far _pascal directive is not available on gcc and the other errors may come from missing definitions.
Maybe you have to look for another lib to do the job.