This is a newbie C/Objective-C question 🙂
Let say I want a CGRectOne and a CGRectTwo constants.
How can I declare that?
Thanks,
Jérémy
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The other answers are fine –in some cases-.
A) declaring it
staticwill emit a copy per translation. That is fine if it is visible to exactly one translation (i.e. its definition is in your .m/.c file). Otherwise, you end up with copies in every translation which includes/imports the header with the static definition. This can result in an inflated binary, as well as an increase to your build times.B)
const CGRect CGRectOne = {...};will emit a symbol in the scope it is declared. if that happens to be a header visible to multiple translations you’ll end up with link errors (becauseCGRectOneis defined multiple times — e.g. once per .c/.m file which directly or indirectly includes the header where the constant is defined).Now that you know the context to use those 2 declarations in, let cover the
externway. Theexternway allows you to:The
externapproach is ideal for reusing the constant among multiple files. Here’s an example:File.h
File.c/m
Note: Omitting the
constwould just make it a global variable.