Re: Proper way to set up constants when building an iOS framework
Re: Proper way to set up constants when building an iOS framework
- Subject: Re: Proper way to set up constants when building an iOS framework
- From: Alex Zavatone <email@hidden>
- Date: Fri, 15 Apr 2016 16:34:14 -0400
Cool. I was thinking of the standard of adding a lowercase k in front of the constants, but wasn't sure if this was canon law or not and wanted to stick with whatever the standard is.
One thing though. I did do a #import of "Constants.h" into my framework's header file and that's not filling the role of what a .pch would fill in a standalone app, even though I thought that someone said it would.
If I put the #import there, the app immediately gets build errors.
Am I missing something on where I should import the constants' header?
Thanks
On Apr 13, 2016, at 12:29 PM, Charles Jenkins wrote:
> Alex,
>
> I suddenly had big fires to put out yesterday and couldn’t respond, but Jens is right. In the .m file but outside of any implementation, define the constant string and assign its value. In the header file just declare the same thing, but with the extern keyword and no value assignment.
>
> This creates exactly one copy of the variable but allows other source files to use it; the problem with what you were doing before was that it created a totally new variable with the same name in every source file where you imported the .h, which prevented the program from linking.
>
> Not to contradict Jens, but I recommend a constant name like kMyImportantConstant, not an all-uppercase name. All-uppercase names signify macro constants, not actual variables. You don’t want to trick a maintainer, not even your future self.
>
> Also, because of my C++ background, I’d see if the compiler would accept NSString const * const, because you want a constant pointer to an NSString that is constant; but I recognize that may not be the way things are done in Obj-C.
>
> You could look in old Apple Obj-C example code declaring NSString constants beginning with ‘k’ to see the right syntax.
>
> --
>
> Charles
>
> On April 12, 2016 at 15:54:49, Jens Alfke (email@hidden) wrote:
>
>>
>>> On Apr 12, 2016, at 10:30 AM, Alex Zavatone <email@hidden> wrote:
>>>
>>> How should they be initialized in the .m? Within an init method?
>>
>> No, just
>> NSString * const ABC_MY_IMPORTANT_CONSTANT = @"abc";
>>
>>> Also, I'm quite familiar with the .pch for iOS apps, but are frameworks allowed a file like this or is that the myAwesomeFramework.h file?
>>
>> That’s what the framework header is.
>>
>>> Thank you sir. Searching for instructions on how to do this is quite challenging.
>>
>> The ‘extern’ stuff is basic C, although it’s not stuff you deal with much using Obj-C. You might want to grab a book on C and read up on global variables.
>>
>> —Jens
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden