Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 396 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Constants in Objective-C

#11
The accepted (and correct) answer says that "you can include this [Constants.h] file... in the pre-compiled header for the project."

As a novice, I had difficulty doing this without further explanation -- here's how: In your YourAppNameHere-Prefix.pch file (this is the default name for the precompiled header in Xcode), import your Constants.h *inside the `#ifdef __OBJC__` block*.

#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "Constants.h"
#endif

Also note that the Constants.h and Constants.m files should contain absolutely nothing else in them except what is described in the accepted answer. (No interface or implementation).
Reply

#12
If you like namespace constant, you can leverage struct, [Friday Q&A 2011-08-19: Namespaced Constants and Functions](

[To see links please register here]

)

// in the header
extern const struct MANotifyingArrayNotificationsStruct
{
NSString *didAddObject;
NSString *didChangeObject;
NSString *didRemoveObject;
} MANotifyingArrayNotifications;

// in the implementation
const struct MANotifyingArrayNotificationsStruct MANotifyingArrayNotifications = {
.didAddObject = @"didAddObject",
.didChangeObject = @"didChangeObject",
.didRemoveObject = @"didRemoveObject"
};
Reply

#13
If you want something like global constants; a quick an dirty way is to put the constant declarations into the `pch` file.
Reply

#14
If you want to call something like this `NSString.newLine;` from objective c, and you want it to be static constant, you can create something like this in swift:

public extension NSString {
@objc public static let newLine = "\n"
}

And you have nice readable constant definition, and available from within a type of your choice while stile bounded to context of type.
Reply

#15
You should create a header file like:

// Constants.h
FOUNDATION_EXPORT NSString *const MyFirstConstant;
FOUNDATION_EXPORT NSString *const MySecondConstant;
//etc.

(You can use `extern` instead of `FOUNDATION_EXPORT` if your code will not be used in mixed C/C++ environments or on other platforms.)

You can include this file in each file that uses the constants or in the pre-compiled header for the project.

You define these constants in a `.m` file like:

// Constants.m
NSString *const MyFirstConstant = @"FirstConstant";
NSString *const MySecondConstant = @"SecondConstant";

`Constants.m` should be added to your application/framework's target so that it is linked in to the final product.

The advantage of using string constants instead of `#define`'d constants is that you can test for equality using pointer comparison (`stringInstance == MyFirstConstant`) which is much faster than string comparison (`[stringInstance isEqualToString:MyFirstConstant]`) (and easier to read, IMO).
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through