Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS #define or static const for global strings [duplicate]

Tags:

ios

I have a constants.h file that declares a bunch of strings that I use throughout my app. I only have strings and nothing else. Am I supposed to use #define or static NSString const? #define works but I hear its not best practice.

like image 378
user1802143 Avatar asked Aug 17 '13 00:08

user1802143


People also ask

What does an iOS mean?

(1) (iOS) (IDevice Operating System) The primary control program in Apple's iPhone and iPad until 2019 when the iPad began to use iPadOS. iOS apps are programmed in Objective-C and Swift. All of the current operating systems from Apple evolved from Unix, including iOS, iPadOS and macOS (formerly OS X).

Is iOS an iPhone or Android?

There are two primary types of touchscreen cell phones based on their operating systems. All iPhones use Apple's proprietary iPhone Operating System, or iOS. Android uses a different operating system based on the open-source Linux, an earlier operating system model.

What is current iOS on iPhone?

Keeping your software up to date is one of the most important things you can do to maintain your Apple product's security. The latest version of iOS and iPadOS is 16.1.1. Learn how to update the software on your iPhone, iPad, or iPod touch. The latest version of macOS is 13.0.1.

What is the iOS system?

Apple iOS is a proprietary mobile operating system that runs on mobile devices such as the iPhone, iPad and iPod Touch. Apple iOS is based on the Mac OS X operating system for desktop and laptop computers. The iOS developer kit provides tools that allow for iOS app development.


1 Answers

The #define is a pre-processor macro. That means that it basically goes through your code and replace your macro with what you've defined.

If you use a const, it's going to be a pointer to the string in memory. It's way more efficient than having the same string being allocated wherever/whenever it is used.

To do that, you'll need both .h and .m files. Your .h file will look something like:

extern NSString * const YOUR_STRING;

And your .m file:

NSString * const YOUR_STRING = @"your string";
like image 182
Bruno Koga Avatar answered Oct 18 '22 15:10

Bruno Koga